Java 类名:com.alibaba.alink.operator.stream.dataproc.format.ColumnsToTripleStreamOp
Python 类名:ColumnsToTripleStreamOp

功能介绍

将数据格式从 Columns 转成 Triple
按照列展开样本,因此一条数据可能输出多条样本

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| tripleColumnValueSchemaStr | 三元组结构中列信息和数据信息的Schema | 三元组结构中列信息和数据信息的Schema | String | ✓ | | |

| handleInvalid | 解析异常处理策略 | 解析异常处理策略,可选为ERROR(抛出异常)或者SKIP(输出NULL) | String | | “ERROR”, “SKIP” | “ERROR” |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | [] |

| selectedCols | 选中的列名数组 | 计算列对应的列名列表 | String[] | | | null |

代码示例

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 = StreamOperator.fromDataframe(df, schemaStr="row string, json string, vec string, kv string, csv string, f0 double, f1 double")
  8. op = ColumnsToTripleStreamOp()\
  9. .setSelectedCols(["f0", "f1"])\
  10. .setReservedCols(["row"])\
  11. .setTripleColumnValueSchemaStr("col string, val double")\
  12. .linkFrom(data)
  13. op.print()
  14. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.stream.StreamOperator;
  3. import com.alibaba.alink.operator.stream.dataproc.format.ColumnsToTripleStreamOp;
  4. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  5. import org.junit.Test;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. public class ColumnsToTripleStreamOpTest {
  9. @Test
  10. public void testColumnsToTripleStreamOp() 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. );
  14. StreamOperator <?> data = new MemSourceStreamOp(df,
  15. "row string, json string, vec string, kv string, csv string, f0 double, f1 double");
  16. StreamOperator <?> op = new ColumnsToTripleStreamOp()
  17. .setSelectedCols("f0", "f1")
  18. .setReservedCols("row")
  19. .setTripleColumnValueSchemaStr("col string, val double")
  20. .linkFrom(data);
  21. op.print();
  22. StreamOperator.execute();
  23. }
  24. }

运行结果

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

| 1 | f0 | 1.0 |

| 1 | f1 | 2.0 |

| 2 | f0 | 4.0 |

| 2 | f1 | 8.0 |