Search in sources :

Example 46 with CollectSinkStreamOp

use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.

the class TensorToVectorStreamOpTest method testTensorToVectorStreamOp.

@Test
public void testTensorToVectorStreamOp() throws Exception {
    List<Row> data = Collections.singletonList(Row.of("DOUBLE#6#0.0 0.1 1.0 1.1 2.0 2.1"));
    CollectSinkStreamOp collectSinkStreamOp = new MemSourceStreamOp(data, "tensor string").link(new TensorToVectorStreamOp().setSelectedCol("tensor")).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> ret = collectSinkStreamOp.getAndRemoveValues();
    Assert.assertEquals(1, ret.size());
    Assert.assertTrue(ret.get(0).getField(0) instanceof DenseVector);
    Assert.assertArrayEquals(new double[] { 0.0, 0.1, 1.0, 1.1, 2.0, 2.1 }, ((DenseVector) ret.get(0).getField(0)).getData(), 1e-6);
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) Row(org.apache.flink.types.Row) DenseVector(com.alibaba.alink.common.linalg.DenseVector) Test(org.junit.Test)

Example 47 with CollectSinkStreamOp

use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.

the class TypeConvertStreamOpTest method test.

@Test
public void test() throws Exception {
    Row[] testArray = new Row[] { Row.of("1", 1, 1.1, 1.0, true), Row.of("2", -2, 0.9, 2.0, false), Row.of("3", 100, -0.01, 3.0, true), Row.of("4", -99, null, 4.0, false), Row.of("5", 1, 1.1, 5.0, true), Row.of("6", -2, 0.9, 6.0, false) };
    String[] colNames = new String[] { "group", "col2", "col3", "col4", "col5" };
    MemSourceStreamOp inOp = new MemSourceStreamOp(Arrays.asList(testArray), colNames);
    TypeConvertStreamOp typeConvertStreamOp = new TypeConvertStreamOp().setSelectedCols(new String[] { colNames[0], colNames[1], colNames[2], colNames[3], colNames[4] }).setTargetType("bigint");
    TypeConvertStreamOp typeConvertStreamOp1 = new TypeConvertStreamOp().setTargetType("string");
    CollectSinkStreamOp collectSinkStreamOp = new CollectSinkStreamOp();
    CollectSinkStreamOp collectSinkStreamOp1 = new CollectSinkStreamOp();
    inOp.link(typeConvertStreamOp).link(collectSinkStreamOp);
    inOp.link(typeConvertStreamOp1).link(collectSinkStreamOp1);
    StreamOperator.execute();
    MTable ret = new MTable(collectSinkStreamOp.getAndRemoveValues(), typeConvertStreamOp.getSchema());
    MTable ret1 = new MTable(collectSinkStreamOp1.getAndRemoveValues(), typeConvertStreamOp1.getSchema());
    Assert.assertEquals(6, ret.getNumRow());
    Assert.assertEquals(6, ret1.getNumRow());
    ret.orderBy(0);
    ret1.orderBy(0);
    Assert.assertEquals(Row.of(1L, 1L, 1L, 1L, 1L), ret.getTable().get(0));
    Assert.assertEquals(Row.of("1", "1", "1.1", "1.0", "true"), ret1.getTable().get(0));
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) MTable(com.alibaba.alink.common.MTable) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 48 with CollectSinkStreamOp

use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.

the class VectorBiFunctionStreamOpTest method testBiVectorFunctionDense.

@Test
public void testBiVectorFunctionDense() throws Exception {
    List<Row> df = new ArrayList<>();
    df.add(Row.of("1 2 3", "2 3 4"));
    StreamOperator<?> data = new MemSourceStreamOp(df, "vec1 string, vec2 string");
    VectorBiFunctionStreamOp vecBiFunc = new VectorBiFunctionStreamOp().setSelectedCols("vec1", "vec2").setBiFuncName("minus").setReservedCols(new String[] {}).setOutputCol("vec_minus");
    StreamOperator<?> res = vecBiFunc.linkFrom(data);
    CollectSinkStreamOp sop = res.link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> list = sop.getAndRemoveValues();
    for (Row row : list) {
        Assert.assertEquals(row.getField(0), VectorUtil.parseDense("-1.0 -1.0 -1.0"));
    }
}
Also used : MemSourceStreamOp(com.alibaba.alink.operator.stream.source.MemSourceStreamOp) CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) ArrayList(java.util.ArrayList) Row(org.apache.flink.types.Row) Test(org.junit.Test)

Example 49 with CollectSinkStreamOp

use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.

the class SvmTest method pipelineTest.

@Test
public void pipelineTest() throws Exception {
    String[] xVars = new String[] { "f0", "f1", "f2", "f3" };
    String yVar = "labels";
    String vectorName = "vec";
    String svectorName = "svec";
    LinearSvm svm = new LinearSvm().setLabelCol(yVar).setFeatureCols(xVars).setOptimMethod("gd").setPredictionCol("svmpred");
    LinearSvm vectorSvm = new LinearSvm().setLabelCol(yVar).setVectorCol(vectorName).setPredictionCol("vsvmpred").enableLazyPrintModelInfo().enableLazyPrintTrainInfo();
    LinearSvm sparseVectorSvm = new LinearSvm().setLabelCol(yVar).setVectorCol(svectorName).setOptimMethod("sgd").setMaxIter(10).setPredictionCol("svsvmpred").setPredictionDetailCol("detail");
    Pipeline plSvm = new Pipeline().add(svm).add(vectorSvm).add(sparseVectorSvm);
    BatchOperator<?> trainData = (BatchOperator<?>) getData(true);
    PipelineModel model = plSvm.fit(trainData);
    BatchOperator<?> result = model.transform(trainData).select(new String[] { "labels", "svmpred", "vsvmpred", "svsvmpred" });
    List<Row> d = result.collect();
    for (Row row : d) {
        for (int i = 1; i < 3; ++i) {
            Assert.assertEquals(row.getField(0), row.getField(i));
        }
    }
    // below is stream test code.
    CollectSinkStreamOp sop = model.transform((StreamOperator<?>) getData(false)).select(new String[] { "labels", "svmpred", "vsvmpred", "svsvmpred" }).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    List<Row> rows = sop.getAndRemoveValues();
    for (Row row : rows) {
        for (int i = 1; i < 3; ++i) {
            Assert.assertEquals(row.getField(0), row.getField(i));
        }
    }
}
Also used : CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) Row(org.apache.flink.types.Row) BatchOperator(com.alibaba.alink.operator.batch.BatchOperator) Pipeline(com.alibaba.alink.pipeline.Pipeline) PipelineModel(com.alibaba.alink.pipeline.PipelineModel) Test(org.junit.Test)

Example 50 with CollectSinkStreamOp

use of com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp in project Alink by alibaba.

the class BisectingKMeansTest method test.

@Test
public void test() throws Exception {
    BisectingKMeans bisectingKMeans = new BisectingKMeans().setVectorCol("vector").setPredictionCol("pred").setK(2).setMaxIter(10);
    PipelineModel model = new Pipeline().add(bisectingKMeans).fit(inputBatchOp);
    BatchOperator<?> batchPredOp = model.transform(inputBatchOp).select(new String[] { "id", "pred" });
    verifyPredResult(batchPredOp.collect());
    CollectSinkStreamOp streamPredOp = model.transform(inputStreamOp).select(new String[] { "id", "pred" }).link(new CollectSinkStreamOp());
    StreamOperator.execute();
    verifyPredResult(streamPredOp.getAndRemoveValues());
}
Also used : CollectSinkStreamOp(com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp) PipelineModel(com.alibaba.alink.pipeline.PipelineModel) Pipeline(com.alibaba.alink.pipeline.Pipeline) Test(org.junit.Test)

Aggregations

CollectSinkStreamOp (com.alibaba.alink.operator.stream.sink.CollectSinkStreamOp)80 Test (org.junit.Test)76 Row (org.apache.flink.types.Row)72 MemSourceStreamOp (com.alibaba.alink.operator.stream.source.MemSourceStreamOp)60 MemSourceBatchOp (com.alibaba.alink.operator.batch.source.MemSourceBatchOp)30 RowComparator (com.alibaba.alink.operator.common.dataproc.SortUtils.RowComparator)25 StreamOperator (com.alibaba.alink.operator.stream.StreamOperator)25 BatchOperator (com.alibaba.alink.operator.batch.BatchOperator)20 Pipeline (com.alibaba.alink.pipeline.Pipeline)9 PipelineModel (com.alibaba.alink.pipeline.PipelineModel)9 Timestamp (java.sql.Timestamp)8 SparseVector (com.alibaba.alink.common.linalg.SparseVector)6 StringNearestNeighborBatchOpTest (com.alibaba.alink.operator.batch.similarity.StringNearestNeighborBatchOpTest)6 TextApproxNearestNeighborBatchOpTest (com.alibaba.alink.operator.batch.similarity.TextApproxNearestNeighborBatchOpTest)6 OverCountWindowStreamOp (com.alibaba.alink.operator.stream.feature.OverCountWindowStreamOp)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 MTable (com.alibaba.alink.common.MTable)3 DenseVector (com.alibaba.alink.common.linalg.DenseVector)3 TableSchema (org.apache.flink.table.api.TableSchema)3