yangyang12138

导航

alink

alink调用的两种模式

第一种直接调用对象本书

直接定义创建对象,fit数据,然后生成模型

public static void main(String[] args) throws Exception {
        Row[] data = new Row[]{
                Row.of(new Object[]{0.0,0.0,0.0}),
                Row.of(new Object[]{0.1,0.2,0.1}),
                Row.of(new Object[]{0.2,0.2,0.8}),
                Row.of(new Object[]{9.0,9.5,9.7}),
                Row.of(new Object[]{9.1,9.1,9.6}),
                Row.of(new Object[]{9.2,9.3,9.9})
        };

        String[] colnames = new String[] {"a","b","c"};
        MemSourceBatchOp inOp = new MemSourceBatchOp(Arrays.asList(data), colnames);

        PCA pca = new PCA();

        pca.setK(2).setSelectedCols("a","b","c").setPredictionCol("o");

        pca.fit(inOp).transform(inOp).print();
    }

 

第二种模式,BatchOperator对象,可以通过link的方式直接将所有的Operator连在一起

public static void main(String[] args){
        Row[] students = new Row[20];

        for(int i=0;i<20;i++){

            students[i] = Row.of( new Object[]{JSON.toJSONString(new Student(String.valueOf(i),i))});

        }

        String[] colnames = new String[] {"col1"};
        MemSourceBatchOp inOp = new MemSourceBatchOp(Arrays.asList(students), colnames);
        List<Row> result = inOp.link(new JsonValueBatchOp()
                .setSkipFailed(true)
                .setSelectedCol("col1").setOutputCols(new String[] {"name", "age"})
                .setJsonPath(new String[] {"$.name", "$.age"})).collect();

        for(Row r:result){
            System.out.println(r.toString());
        }
    }

    public static class Student{
        private String name;
        private Integer age;

        public Student(String name,Integer age){
            this.name = name;
            this.age = age;
        }


        public String getName() {
            return name;
        }

        public Integer getAge() {
            return age;
        }
    }

 

posted on 2020-03-27 02:31  杨杨09265  阅读(566)  评论(0编辑  收藏  举报