flink1.9.1 之 sql 解析流中的数组

输入

{
  "a": 1,
  "b": [
    {
      "name": "name1",
      "age": 11
    },
    {
      "name": "name2",
      "age": 12
    },
    {
      "name": "name3",
      "age": 13
    }
  ]
}

期望输出

1 name1 11
1 name2 12
1 name3 13

知识点

unnest

--Unnesting WITH ORDINALITY is not supported yet.

SELECT users, tag
FROM Orders CROSS JOIN UNNEST(tags) AS t (tag)

demo

pom.xml



    4.0.0

    com.xp
    test-flink
    1.9.1

    
        2.11
        1.8
    

    
        
            org.apache.flink
            flink-table-api-java-bridge_${scala.binary.version}
            ${project.version}
        
        
            org.apache.flink
            flink-table-planner_${scala.binary.version}
            ${project.version}
        
    

    
        
            
                maven-compiler-plugin
                3.8.0
                
                    ${java.version}
                    ${java.version}
                
            
        
    

public class TestUnnested {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(environment);


        List rows = Arrays.asList(
                Row.of(1, new Row[]{Row.of(12, "sd"), Row.of(15, "sd")}),
                Row.of(2, new Row[]{Row.of(13, "sd"), Row.of(16, "sd")}),
                Row.of(3, new Row[]{Row.of(14, "sd"), Row.of(17, "sd")})
        );

        TypeInformation[] types = new TypeInformation[]{Types.INT, Types.OBJECT_ARRAY(Types.ROW(Types.INT,Types.STRING))};
//        TypeInformation[] types = new TypeInformation[]{Types.INT, ObjectArrayTypeInfo.getInfoFor(new RowTypeInfo(Types.INT, Types.STRING))};
        String[] typeNames = new String[]{"a", "b"};

        DataStream source = environment
                .fromCollection(rows)
                .returns(new RowTypeInfo(types, typeNames));

        tableEnvironment.registerDataStream("source", source);

        Table a = tableEnvironment.sqlQuery("select a,t.c,t.d from source,unnest(b) as t (c,d)");

        tableEnvironment.toAppendStream(a, Row.class).print();

        environment.execute();
    }
}

你可能感兴趣的:(flink1.9.1 之 sql 解析流中的数组)