Java 类名:com.alibaba.alink.operator.batch.source.TsvSourceBatchOp
Python 类名:TsvSourceBatchOp

功能介绍

读Tsv文件,Tsv文件是以tab为分隔符。文件来源可以是本地,oss,http,hdfs等。

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
filePath 文件路径 文件路径 String
schemaStr Schema Schema。格式为”colname coltype[, colname2, coltype2[, …]]”,例如”f0 string, f1 bigint, f2 double” String
ignoreFirstLine 是否忽略第一行数据 是否忽略第一行数据 Boolean false
skipBlankLine 是否忽略空行 是否忽略空行 Boolean true

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ["0L", "1L", 0.6],
  6. ["2L", "2L", 0.8],
  7. ["2L", "4L", 0.6],
  8. ["3L", "1L", 0.6],
  9. ["3L", "2L", 0.3],
  10. ["3L", "4L", 0.4]
  11. ])
  12. source = BatchOperator.fromDataframe(df, schemaStr='uid string, iid string, label double')
  13. filepath = "/tmp/abc.tsv"
  14. tsvSink = TsvSinkBatchOp()\
  15. .setFilePath(filepath)\
  16. .setOverwriteSink(True)
  17. source.link(tsvSink)
  18. BatchOperator.execute()
  19. tsvSource = TsvSourceBatchOp().setFilePath(filepath).setSchemaStr("f string")
  20. tsvSource.print()

Java 代码

  1. package javatest.com.alibaba.alink.batch.source;
  2. import org.apache.flink.types.Row;
  3. import com.alibaba.alink.operator.batch.BatchOperator;
  4. import com.alibaba.alink.operator.batch.sink.TsvSinkBatchOp;
  5. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  6. import com.alibaba.alink.operator.batch.source.TsvSourceBatchOp;
  7. import org.junit.Test;
  8. import java.util.Arrays;
  9. import java.util.List;
  10. public class TsvSourceBatchOpTest {
  11. @Test
  12. public void testTsvSourceBatchOp() throws Exception {
  13. List <Row> df = Arrays.asList(
  14. Row.of("0L", "1L", 0.6),
  15. Row.of("2L", "2L", 0.8),
  16. Row.of("2L", "4L", 0.6),
  17. Row.of("3L", "1L", 0.6),
  18. Row.of("3L", "2L", 0.3),
  19. Row.of("3L", "4L", 0.4)
  20. );
  21. BatchOperator <?> source = new MemSourceBatchOp(df, "uid string, iid string, label double");
  22. String filepath = "/tmp/abc.tsv";
  23. BatchOperator <?> tsvSink = new TsvSinkBatchOp()
  24. .setFilePath(filepath)
  25. .setOverwriteSink(true);
  26. source.link(tsvSink);
  27. BatchOperator.execute();
  28. BatchOperator <?> tsvSource = new TsvSourceBatchOp().setFilePath(filepath).setSchemaStr("f string");
  29. tsvSource.print();
  30. }
  31. }

运行结果

| f | | —- |

| 0L |

| 2L |

| 3L |

| 3L |

| 2L |

| 3L |