0

我在 Flink 数据集中加载了一个 Avro 文件:

AvroInputFormat<GenericRecord> test = new AvroInputFormat<GenericRecord>(
        new Path("PathToAvroFile")
        , GenericRecord.class);
DataSet<GenericRecord> DS = env.createInput(test);

usersDS.print();

以下是打印 DS 的结果:

{"N_NATIONKEY": 14, "N_NAME": "KENYA", "N_REGIONKEY": 0, "N_COMMENT": " pending excuses haggle furiously deposits. pending, express pinto beans wake fluffily past t"}
{"N_NATIONKEY": 15, "N_NAME": "MOROCCO", "N_REGIONKEY": 0, "N_COMMENT": "rns. blithely bold courts among the closely regular packages use furiously bold platelets?"}
{"N_NATIONKEY": 16, "N_NAME": "MOZAMBIQUE", "N_REGIONKEY": 0, "N_COMMENT": "s. ironic, unusual asymptotes wake blithely r"}
{"N_NATIONKEY": 17, "N_NAME": "PERU", "N_REGIONKEY": 1, "N_COMMENT": "platelets. blithely pending dependencies use fluffily across the even pinto beans. carefully silent accoun"}
{"N_NATIONKEY": 18, "N_NAME": "CHINA", "N_REGIONKEY": 2, "N_COMMENT": "c dependencies. furiously express notornis sleep slyly regular accounts. ideas sleep. depos"}
{"N_NATIONKEY": 19, "N_NAME": "ROMANIA", "N_REGIONKEY": 3, "N_COMMENT": "ular asymptotes are about the furious multipliers. express dependencies nag above the ironically ironic account"}
{"N_NATIONKEY": 20, "N_NAME": "SAUDI ARABIA", "N_REGIONKEY": 4, "N_COMMENT": "ts. silent requests haggle. closely express packages sleep across the blithely"}

现在我想从 DS Dataset 创建一个与 Avro 文件模式完全相同的表,我的意思是列应该是 N_NATIONKEY、N_NAME、N_REGIONKEY 和 N_COMMENT。

我知道使用这条线:

tableEnv.registerDataSet("tbTest", DS, "field1, field2, ...");

我可以创建一个表并设置列,但我希望从数据中自动推断列。可能吗?另外,我试过

tableEnv.registerDataSet("tbTest", DS);

但它使用架构创建了一个表:

root
 |-- f0: GenericType<org.apache.avro.generic.GenericRecord>
4

1 回答 1

1

GenericRecord是 Table & SQL API 运行时的黑盒,因为字段的数量和它们的数据类型是未定义的。我建议使用 Avro 生成的扩展类SpecificRecord。Flink 的类型系统也可以识别这些特定类型,您可以使用适当的数据类型正确处理各个字段。

或者,您可以实现一个自定义 UDF,以提取具有适当数据类型的字段getAvroInt(f0, "myField")getAvroString(f0, "myField")

一些伪代码:

class AvroStringFieldExtract extends ScalarFunction {
    public String eval(GenericRecord r, String fieldName) {
        return r.get(fieldName).toString();
    }
}

tableEnv.registerFunction("getAvroFieldString", new AvroStringFieldExtract())
于 2019-01-28T11:12:09.203 回答