- 导入依赖
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.9.2</version>
</dependency>
- 构造scheam
{
\"type\": \"record\",
\"name\": \"hudi_rowkey_test_record\",
\"namespace\": \"hoodie.hudi_rowkey_test\",
\"fields\": [
{
\"name\": \"id\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"fk_id\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"lmdm\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"sldw\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"ugsa\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"kspc\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"siii\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"isdeleted\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"lastupdatedttm\",
\"type\": [
\"string\",
\"null\"
]
},
{
\"name\": \"rowkey\",
\"type\": [
\"string\",
\"null\"
]
}
]
}";
- 代码写入
private static String schemaFile2 = "{\"type\":\"record\",\"name\":\"hudi_rowkey_test_record\",\"namespace\":\"hoodie.hudi_rowkey_test\",\"fields\":[{\"name\":\"id\",\"type\":[\"string\",\"null\"]},{\"name\":\"fk_id\",\"type\":[\"string\",\"null\"]},{\"name\":\"lmdm\",\"type\":[\"string\",\"null\"]},{\"name\":\"sldw\",\"type\":[\"string\",\"null\"]},{\"name\":\"ugsa\",\"type\":[\"string\",\"null\"]},{\"name\":\"kspc\",\"type\":[\"string\",\"null\"]},{\"name\":\"siii\",\"type\":[\"string\",\"null\"]},{\"name\":\"isdeleted\",\"type\":[\"string\",\"null\"]},{\"name\":\"lastupdatedttm\",\"type\":[\"string\",\"null\"]},{\"name\":\"rowkey\",\"type\":[\"string\",\"null\"]},{\"name\":\"compsiteKey\",\"type\":[\"string\",\"null\"]}]}";
public static void main(String[] args) {
Schema schema = new Schema.Parser().parse(schemaFile);
int size = new Schema.Parser().parse(schemaFile).getFields().size();
Schema type = Schema.createUnion(Schema.create(Schema.Type.STRING), Schema.create(Schema.Type.NULL));
Schema.Field field = new Schema.Field("compsitekey", type, "", "");
schema.getFields().add(size, field);
GenericRecord my = new GenericData.Record(new Schema.Parser().parse(schema.toString()));
my.put("id", "xxxx");
my.put("fk_id", "xxxx");
my.put("lmdm", "xxxx");
my.put("sldw", "xxxx");
my.put("ugsa", "xxxx");
my.put("kspc", "xxxx");
my.put("siii", "xxxx");
my.put("isdeleted", "xxxx");
my.put("lastupdatedttm", "xxxx");
my.put("rowkey", "xxxx");
my.put("compsitekey", "xxxx");
System.out.println("---------------------------");
}