In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
This passage discusses how to integrate a provided drools package into datastream application.
Packaging:
If a maven project is provided by customer. In this case, you need to ensure that the pom file contains the following:
Org.drools drools-bom pom xxx import org.kie kie-api org.drools drools-compiler runtime other dependencies org.kie kie-maven-plugin xxx true
In addition, a file kmodule.xml must be added to src\ main\ resources\ META-INF folder. A minimum kmodule.xml likes like the following.
The default stateless ksession is mandatory.
Rule files can be put in main/resources as normal
The command to create jar file is still mvn package as normal. However, the jar created is a bit different. Here is a screenshot
Note that there is knowledge base cache file and kmodule file in META-INF. Two rule files in main/resources are shifted out into the root folder.
What if the customer does not provide a maven project? I guess the best strategy is to create a maven project by ourselves. If source code is provided, we just import source code into the maven project, otherwise, use customer provided jar as a maven dependency?
Note that kie module is introduced only after drools 6. So I don't think this will work for drools 5 and below. Also, for drools integration in streamtau, we are using the latest version 7.2.1. So whether earlier version like 6.x is fully compatible still remains a question.
Invocation:
Load rules:
First create a KieServices singleton instance.
Private final KieServices kieServices = KieServices.Factory.get ()
Load the drools package into system:
Protected DroolsDataHolder doLoadDroolsModule (DroolsLoadParam droolsLoadParam) {DroolsParameters origParams = droolsLoadParam.getDroolsParam (); String moduleName = origParams.getModuleName (); try {InputStream is = droolsDataLoader.getDroolsModuleAsStream (droolsLoadParam); KieContainer curContainer = DroolsUtils.buildContainer (kieServices, is); return new DroolsDataHolder (curContainer);} catch (Exception ex) {logger.error ("Error loading drools" + moduleName, ex) } return null;}
DroolsDataLoader is an interface that is designed to loads drools package as stream (via either file system or restful interface)
DroolsUtils is the utility class that builds a KieContainer from stream.
Public static KieContainer buildContainer (KieServices kieServices, InputStream stream) throws Exception {Resource wrapped = kieServices.getResources (). NewInputStreamResource (stream); KieModule curModule = kieServices.getRepository (). AddKieModule (wrapped); ReleaseId releaseId = curModule.getReleaseId (); logger.info ("Release id generated for module: {}", releaseId); KieContainer kContainer = kieServices.newKieContainer (releaseId, DroolsUtils.class.getClassLoader ()); return kContainer;}
The returned DroolsDataHolder is merely a wrapper of KieContainer
Public class DroolsDataHolder {private final KieContainer kieContainer; public DroolsDataHolder (KieContainer kieContainer) {this.kieContainer = kieContainer;} public KieContainer getKieContainer () {return kieContainer;} public void destroy () {kieContainer.dispose ();}}
The loaded DroolsDataHolder will be cached unless rule is changed, which triggers a reload operation
Public DroolsDataHolder getOrLoadDroolsModule (DroolsLoadParam droolsLoadParam) {DroolsParameters origParams = droolsLoadParam.getDroolsParam (); String moduleName = origParams.getModuleName (); dataLock.readLock (). Lock (); try {DroolsDataHolder curHolder = containers.get (moduleName); if (curHolder! = null) {return curHolder;} dataLock.readLock (). Unlock () DataLock.writeLock () .lock (); try {return doUpdateDroolsModule (droolsLoadParam);} finally {dataLock.readLock () .lock (); dataLock.writeLock () .unlock ();}} finally {dataLock.readLock () .unlock ();}}
Invoke the drools module:
In stream environment, only stateless drools knowledge session is supported for now. The main reason is that stream is executed in a distributed environment. The session will be created on multiple JVMS, so it is virtually hard to share all the facts globally. Evaluating the rule is quite simple, it is composed of 3 steps:
Convert stream data to rule input pojopublic Class getRulePojoClass (DroolsLoadParam droolsLoadParam, String inputPojoClassName) {DroolsParameters origParams = droolsLoadParam.getDroolsParam (); String moduleName = origParams.getModuleName (); DroolsDataHolder curDataHolder = this.getOrLoadDroolsModule (droolsLoadParam); if (curDataHolder = = null) {throw new IllegalArgumentException ("No drools module found by name:" + moduleName);} try {ClassLoader cl = curDataHolder.getKieContainer (). GetClassLoader (); Class inputPojoClass = cl.loadClass (inputPojoClassName); return inputPojoClass } catch (Exception e) {throw RtException.from (e);}}
The good thing about drools module is that it provides a self contained class loading environment. So third party jar dependencies are unlikely to cause conflict with the outside runtime environment. However, when we build an input event to drools engine, we need to use the KieContainer's class loader to find the input event class referenced in rule.
Build a stateless kie session and invoke the rule
Public List evaluate (DroolsLoadParam droolsLoadParam, List facts) {if (logger.isDebugEnabled ()) {logger.debug ("Start evaluating drools, input is: {}, module name is: {}", Arrays.asList (facts), droolsLoadParam.getDroolsParam (). GetModuleName ());} DroolsParameters origParams = droolsLoadParam.getDroolsParam (); String moduleName = origParams.getModuleName (); DroolsDataHolder curDataHolder = this.getOrLoadDroolsModule (droolsLoadParam) If (curDataHolder = = null) {throw new IllegalArgumentException ("No drools module found by name:" + moduleName);} StatelessKieSession curSession = curDataHolder.getKieContainer () .newStatelessKieSession (); curSession.execute (facts); return facts;} convert rule evaluation result back to stream data
Under the hood:
Drools class relations
Drools package loading
Things to note:
Drools package can be large and the current approach caches all loaded drools package in memory. The loading time and memory consumption might be a bottleneck of scalability. A better approach will be building a standalone rule server, where it manages rules and exposes a rest api to stream application.
Find out input metadata for rule: it is possible to find out java class of each rule variable. This is useful as a hint to map stream data to rule input.
Public static Map > result = new HashMap
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.