SparK-Drools-Java:如何将forEach函数转换为平面图

时间:2018-06-20 06:45:56

标签: java apache-spark drools

我在public static void main(String[] args)中有以下代码:

finalJoined.foreach(row -> droolprocess(broadcastRules.value(), row));

这里finalJoined的类型为Dataset<Row>

下面是droolsprocess 函数

public static void droolprocess(KieBase base, Row row) {
        StatelessKieSession session = base.newStatelessKieSession();
        RuleParams ruleParams = new RuleParams();
        String outputString = (String) row.get(1);
        // setting up parameters
        System.out.println("Value of TXN DTTM is : " + row.getString(0));
        ruleParams.setTxnDttm(row.getString(0));
        ruleParams.setCisDivision(row.getString(1));
        System.out.println("Division is  : " + ruleParams.getCisDivision());
        ruleParams.setTxnVol(row.getInt(2));
        System.out.println("TXN Volume is  : " + ruleParams.getTxnVol());
        ruleParams.setTxnAmt(row.getInt(3));
        System.out.println("TXN Amount is  : " + ruleParams.getTxnAmt());
        ruleParams.setCurrencyCode(row.getString(4));
        ruleParams.setAcctNumberTypeCode(row.getString(5));
        ruleParams.setAccountNumber(row.getLong(6));
        ruleParams.setUdfChar1(row.getString(7));
        System.out.println("UDF Char1 is : " + ruleParams.getUdfChar1());
        ruleParams.setUdfChar2(row.getString(8));
        ruleParams.setUdfChar3(row.getString(9));
        ruleParams.setAccountId(row.getLong(10));

        session.execute(CommandFactory.newInsert(ruleParams));

        System.out.println("After firing  rules");
        System.out.println(ruleParams.getPriceItemParam1());
        System.out.println(ruleParams.getCisDivision());
        // generating output objects depending on the size of price items
        // derived.
        System.out.println("No. of priceitems derived : " + ruleParams.getPriceItemCd().size());
        for (int index = 0; index < ruleParams.getPriceItemCd().size(); index++) {

            System.out.println("Inside a for loop");

            RuleOutputParams ruleOutputParams = new RuleOutputParams();

            ruleOutputParams.setTxnDttm(ruleParams.getTxnDttm());
            ruleOutputParams.setCisDivision(ruleParams.getCisDivision());
            ruleOutputParams.setTxnVol(ruleParams.getTxnVol());
            ruleOutputParams.setTxnAmt(ruleParams.getTxnAmt());
            ruleOutputParams.setCurrencyCode(ruleParams.getCurrencyCode());
            ruleOutputParams.setAcctNumberTypeCode(ruleParams.getAcctNumberTypeCode());
            ruleOutputParams.setAccountNumber(ruleParams.getAccountNumber());
            ruleOutputParams.setAccountId(ruleParams.getAccountId());
            ruleOutputParams.setPriceItemCd(ruleParams.getPriceItemCd().get(index));
            System.out.println(ruleOutputParams.getPriceItemCd());
            ruleOutputParams.setPriceItemParam(ruleParams.getPriceItemParams().get(index));
            System.out.println(ruleOutputParams.getPriceItemParam());
            ruleOutputParams.setPriceItemParamCode(ruleParams.getPriceItemParamCodes().get(index));
            ruleOutputParams.setProcessingDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
            ruleOutputParams.setUdfChar1(ruleParams.getUdfChar1());
            ruleOutputParams.setUdfChar2(ruleParams.getUdfChar2());
            ruleOutputParams.setUdfChar3(ruleParams.getUdfChar3());
        }
    }

我正在调用流口水,并根据其输出创建 RuleOutputParams 对象。 我希望此对象以main()的形式在Dataset<Row>方法中返回。

我曾尝试在类级别和对象级别创建一个ArrayList<RuleOutputParams>,但是由于foreach在独立集群模式下并行运行在多个节点上而无法更新。

那我该如何运行droolprocess的代码? flatMap的{​​{1}}方法可以在这里使用,但我不确定如何实现。

任何人都可以提供一些相同的指针吗?

0 个答案:

没有答案