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

功能介绍

将数据格式从 Csv 转成 Triple三元组
一条数据数据可能会产生多条三元组数据

参数说明

名称 中文名称 描述 类型 是否必须? 取值范围 默认值
csvCol CSV列名 CSV列的列名 String 所选列类型为 [STRING]
schemaStr Schema Schema。格式为”colname coltype[, colname2, coltype2[, …]]”,例如”f0 string, f1 bigint, f2 double” String
tripleColumnValueSchemaStr 三元组结构中列信息和数据信息的Schema 三元组结构中列信息和数据信息的Schema String
csvFieldDelimiter 字段分隔符 字段分隔符 String “,”
handleInvalid 解析异常处理策略 解析异常处理策略,可选为ERROR(抛出异常)或者SKIP(输出NULL) String “ERROR”, “SKIP” “ERROR”
quoteChar 引号字符 引号字符 Character “””
reservedCols 算法保留列名 算法保留列 String[] []

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. ['1', '{"f0":"1.0","f1":"2.0"}', '$3$0:1.0 1:2.0', 'f0:1.0,f1:2.0', '1.0,2.0', 1.0, 2.0],
  6. ['2', '{"f0":"4.0","f1":"8.0"}', '$3$0:4.0 1:8.0', 'f0:4.0,f1:8.0', '4.0,8.0', 4.0, 8.0]])
  7. data = BatchOperator.fromDataframe(df, schemaStr="row string, json string, vec string, kv string, csv string, f0 double, f1 double")
  8. op = CsvToTripleBatchOp()\
  9. .setCsvCol("csv")\
  10. .setSchemaStr("f0 double, f1 double")\
  11. .setReservedCols(["row"])\
  12. .setTripleColumnValueSchemaStr("col string, val double")\
  13. .linkFrom(data)
  14. op.print()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.dataproc.format.CsvToTripleBatchOp;
  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 CsvToTripleBatchOpTest {
  9. @Test
  10. public void testCsvToTripleBatchOp() throws Exception {
  11. List <Row> df = Arrays.asList(
  12. Row.of("1", "{\"f0\":\"1.0\",\"f1\":\"2.0\"}", "$3$0:1.0 1:2.0", "f0:1.0,f1:2.0", "1.0,2.0", 1.0, 2.0),
  13. Row.of("2", "{\"f0\":\"4.0\",\"f1\":\"8.0\"}", "$3$0:4.0 1:8.0", "f0:4.0,f1:8.0", "4.0,8.0", 4.0, 8.0)
  14. );
  15. BatchOperator <?> data = new MemSourceBatchOp(df,
  16. "row string, json string, vec string, kv string, csv string, f0 double, f1 double");
  17. BatchOperator <?> op = new CsvToTripleBatchOp()
  18. .setCsvCol("csv")
  19. .setSchemaStr("f0 double, f1 double")
  20. .setReservedCols("row")
  21. .setTripleColumnValueSchemaStr("col string, val double")
  22. .linkFrom(data);
  23. op.print();
  24. }
  25. }

运行结果

| row | col | val | | —- | —- | —- |

| 1 | f0 | 1.0 |

| 1 | f1 | 2.0 |

| 2 | f0 | 4.0 |

| 2 | f1 | 8.0 |