Java 类名:com.alibaba.alink.operator.batch.dataproc.SplitBatchOp
Python 类名:SplitBatchOp

功能介绍

将输入数据按比例拆分为两部分。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
fraction 拆分到左端的数据比例 拆分到左端的数据比例 Double [0.0, 1.0]
randomSeed 随机数种子 随机数种子 Integer null

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df_data = pd.DataFrame([['Ohio', 2001, 1.7],
  5. ['Ohio', 2002, 3.6],
  6. ['Nevada', 2001, 2.4],
  7. ['Nevada', 2002, 2.9]])
  8. batch_data = BatchOperator.fromDataframe(df_data, schemaStr='f1 string, f2 bigint, f3 double')
  9. spliter = SplitBatchOp().setFraction(0.5)
  10. spliter.linkFrom(batch_data)
  11. spliter.lazyPrint(-1)
  12. spliter.getSideOutput(0).lazyPrint(-1)
  13. BatchOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.dataproc.SplitBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class SplitBatchOpTest {
  9. @Test
  10. public void testSplitBatchOp() throws Exception {
  11. List <Row> df_data = Arrays.asList(
  12. Row.of("Ohio", 2001, 1.7),
  13. Row.of("Ohio", 2002, 3.6),
  14. Row.of("Nevada", 2001, 2.4),
  15. Row.of("Nevada", 2002, 2.9)
  16. );
  17. BatchOperator <?> batch_data = new MemSourceBatchOp(df_data, "f1 string, f2 int, f3 double");
  18. BatchOperator <?> spliter = new SplitBatchOp().setFraction(0.5);
  19. spliter.linkFrom(batch_data);
  20. spliter.lazyPrint(-1);
  21. spliter.getSideOutput(0).lazyPrint(-1);
  22. BatchOperator.execute();
  23. }
  24. }

运行结果

| f1 | f2 | f3 | | —- | —- | —- |

| Ohio | 2001 | 1.7000 |

| Nevada | 2002 | 2.9000 |

| f1 | f2 | f3 | | —- | —- | —- |

| Ohio | 2002 | 3.6000 |

| Nevada | 2001 | 2.4000 |