动态为数值型列设置默认值0

public Dataset<Row> setDefaultOneZero(Dataset<Row> ds,String formName){
        StructType structType = ds.schema();
        int len = structType.fields().length;
        String keyStr = global.getMap().get("default.parameter").replaceAll("\\|",",");
        String tmpViewName = formName+"_zeroTable";
        tmpViewName = tmpViewName.replaceAll("-","_");
        boolean isExist = global.getSparkSession().catalog().tableExists(tmpViewName);
        if(isExist==true){
            Dataset<Row> defaultDs = global.getSparkSession().sql("select * from "+tmpViewName);
            Seq<String> keys = HadoopUtils.getNCellSchema(keyStr);
            defaultDs = defaultDs.join(ds,keys,"left");
            for(int i=0;i<len;i++) {
                String type = structType.fields()[i].dataType().toString().toLowerCase();
                String name = structType.fields()[i].name();
                if (type.startsWith("decimal")||type.startsWith("int")||
                        type.startsWith("double")||type.startsWith("long")||
                        type.startsWith("bigdecimal")) {

                    defaultDs = defaultDs.selectExpr("*","case when "+name+" is not NULL then "+name+" else 0 end as "+name+"_1")
                            .drop(col(name)).withColumnRenamed(name+"_1",name);
                }
            }
            return defaultDs;
        }else{
            return ds;
        }
    }
public void generateZeroDS(){
        String key = "default.parameter";
        String isOpen = Optional.ofNullable(global.getMap().get("default.zero.enable")).orElse("false");
        if("true".equalsIgnoreCase(isOpen)){
            String defaultStr[] = global.getMap().get(key).split("\\|");
            for(String formName : global.getFormNameList()){
               
                String strDs = String.format("{'%s':'%s','%s':'%s','%s':'%s'}",
                        defaultStr[0],
                        new DateUtils().getAsOfDate(global.getArgument().getCobDate()),
                        defaultStr[1],
                        global.getIdMap().get(formName),
                        defaultStr[2],
                        global.getBranchMap().get(formName)
                );
                ArrayList<String> tmpList = new ArrayList<>();
                tmpList.add(strDs);
                logger.info("generateZeroDS: strDs="+strDs);
                JavaRDD<String> tmpRdd = Global.sc.parallelize(tmpList);
                Dataset<Row> zeroDs = global.getSparkSession().read()
                        .option(Global.TIMESTAMP_FORMAT,Global.TIMESTAMPFORMATVALUE)
                        .json(tmpRdd);
                String s = formName+"_zeroTable";
                s = s.replaceAll("-","_");
                zeroDs.createOrReplaceTempView(s);
                logger.info("generateZeroDS: zeroTable generate in sparkSession !");
                zeroDs.show(false);
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值