我在 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>