简体   繁体   English

Drools在Spark中用于流媒体文件

[英]Drools In Spark for Streaming File

We were able to successfully Integrate drools with spark, When we try to apply rules from Drools we were able to do for Batch file, which is present in HDFS, But we tried to use drools for Streaming file so that we can make decision instantly, But we couldn't figure out how to do it.Below is the snippet of the code what we are trying to achieve. 我们能够成功地将drools与spark集成在一起当我们尝试应用来自Drools的规则时,我们能够为批处理文件做到这一点,它存在于HDFS中,但我们尝试使用drools进行流式文件,以便我们可以立即做出决定,但我们无法弄清楚如何去做.Below是我们正在努力实现的代码片段。
Case1: . 案例1 : .

    SparkConf conf = new SparkConf().setAppName("sample");
    JavaSparkContext sc = new JavaSparkContext(conf);

    JavaRDD<String> javaRDD = sc.textFile("/user/root/spark/sample.dat");
    List<String> store = new ArrayList<String>();
    store = javaRDD.collect();

Case 2: when we use streaming context 案例2:当我们使用流式上下文时

SparkConf sparkconf = new SparkConf().setAppName("sparkstreaming");
    JavaStreamingContext ssc = 
              new JavaStreamingContext(sparkconf, new Duration(1));

    JavaDStream<String> lines = ssc.socketTextStream("xx.xx.xx.xx", xxxx);

In the first case we were able apply our rules on the variable store, but in the second case we were not able to apply rules on the dstream lines. 在第一种情况下,我们可以在变量存储上应用我们的规则,但在第二种情况下,我们无法在dstream行上应用规则。

If someone has some idea, how it can be done, will be a great help. 如果有人有一些想法,怎么做,将是一个很大的帮助。

Here is one way to get it done. 这是完成它的一种方法。

  1. Create your knowledge session with business rules first. 首先使用业务规则创建知识会话。

     //Create knowledge and session here KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newFileResource( "rulefile.drl"), ResourceType.DRL ); Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages(); kbase.addKnowledgePackages( pkgs ); final StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); 
  2. Create JavaDStream using StreamingContext. 使用StreamingContext创建JavaDStream。

     SparkConf sparkconf = new SparkConf().setAppName("sparkstreaming"); JavaStreamingContext ssc = new JavaStreamingContext(sparkconf, new Duration(1)); JavaDStream<String> lines = ssc.socketTextStream("xx.xx.xx.xx", xxxx); 
  3. Call DStream's foreachRDD to create facts and fire your rules. 调用DStream的foreachRDD来创建事实并触发你的规则。

     lines.foreachRDD(new Function<JavaRDD<String>, Void>() { @Override public Void call(JavaRDD<String> rdd) throws Exception { List<String> facts = rdd.collect(); //Apply rules on facts here ksession.execute(facts); return null; } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM