Java 类名:com.alibaba.alink.operator.batch.dataproc.format.JsonToTripleBatchOp
Python 类名:JsonToTripleBatchOp
将数据格式从 Json 转成 Triple,Json中的key赋值为一列,value赋值为一列。
setTripleColumnValueSchemaStr 分别设置为key值和value值的列名和列类型。一条输入数据可能产生多条输出。
名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 |
---|---|---|---|---|---|---|
jsonCol | JSON列名 | JSON列的列名 | String | ✓ | 所选列类型为 [STRING] | |
tripleColumnValueSchemaStr | 三元组结构中列信息和数据信息的Schema | 三元组结构中列信息和数据信息的Schema | String | ✓ | ||
handleInvalid | 解析异常处理策略 | 解析异常处理策略,可选为ERROR(抛出异常)或者SKIP(输出NULL) | String | “ERROR”, “SKIP” | “ERROR” | |
reservedCols | 算法保留列名 | 算法保留列 | String[] | [] |
from pyalink.alink import * import pandas as pd useLocalEnv(1) df = pd.DataFrame([ ['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], ['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]]) data = BatchOperator.fromDataframe(df, schemaStr="row string, json string, vec string, kv string, csv string, f0 double, f1 double") op = JsonToTripleBatchOp()\ .setJsonCol("json")\ .setReservedCols(["row"])\ .setTripleColumnValueSchemaStr("col string, val double")\ .linkFrom(data) op.print()
import org.apache.flink.types.Row; import com.alibaba.alink.operator.batch.BatchOperator; import com.alibaba.alink.operator.batch.dataproc.format.JsonToTripleBatchOp; import com.alibaba.alink.operator.batch.source.MemSourceBatchOp; import org.junit.Test; import java.util.Arrays; import java.util.List; public class JsonToTripleBatchOpTest { @Test public void testJsonToTripleBatchOp() throws Exception { List <Row> df = Arrays.asList( 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), 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) ); BatchOperator <?> data = new MemSourceBatchOp(df, "row string, json string, vec string, kv string, csv string, f0 double, f1 double"); BatchOperator <?> op = new JsonToTripleBatchOp() .setJsonCol("json") .setReservedCols("row") .setTripleColumnValueSchemaStr("col string, val double") .linkFrom(data); op.print(); } }
row | col | val |
---|---|---|
1 | f0 | 1.0 |
1 | f1 | 2.0 |
2 | f0 | 4.0 |
2 | f1 | 8.0 |