有一个相当复杂的Avro架构(我无法修改)。
尝试在java中模拟JSON示例:
var arr[0]=
[
{
"startTime": "1300",
"endTime": "1700",
"eventName": "Sleep",
"end_datetime": "20180510M0100",
"start_datetime": "20180509M2300",
},
{
"startTime": "0800",
"endTime": "1200",
"eventName": "Breakfast",
"end_datetime": "20180507M1200",
"start_datetime": "20180507M0800",
},
{
"startTime": "1300",
"endTime": "1400",
"eventName": "Lesson",
"end_datetime": "20180507M1400",
"start_datetime": "20180507M1300",
},
{
"startTime": "1300",
"endTime": "1700",
"eventName": "Ski",
"end_datetime": "20180511M170000",
"start_datetime": "20180511M130000",
}
]
它一直都在失败:
GenericRecord genericRecord = AvroUtil.jsonToGenericRecord(jsonData, avroSchema);
是否有例如在线工具,它将为任何给定的Avro架构提供JSON数据的示例? (这样它可以正确匹配)
尝试嘲笑JSON数据几个小时但仍然没有成功..
答案 0 :(得分:5)
您可以使用trevni依赖关系和测试范围创建随机数据。这里有一个示例代码
import org.apache.avro.Schema;
import org.apache.trevni.avro.RandomData;
import java.util.Iterator;
public class JSONExample {
public static void main(String [] args){
Schema schema = new Schema.Parser().parse("{\n" +
" \"type\": \"record\",\n" +
" \"namespace\": \"com.acme\",\n" +
" \"name\": \"Test\",\n" +
" \"fields\": [\n" +
" { \"name\": \"name\", \"type\": \"string\" },\n" +
" { \"name\": \"age\", \"type\": \"int\" },\n" +
" { \"name\": \"sex\", \"type\": \"string\" },\n" +
" { \"name\": \"active\", \"type\": \"boolean\" }\n" +
" ]\n" +
"}");
Iterator<Object> it = new RandomData(schema, 1).iterator();
System.out.println(it.next());
}
}
输出
{"name": "cjnyvbmetf", "age": -1757126879, "sex": "", "active": false}
maven依赖
<dependencies>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>trevni-core</artifactId>
<classifier>tests</classifier>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>trevni-avro</artifactId>
<classifier>tests</classifier>
<version>1.8.2</version>
</dependency>
</dependencies>
答案 1 :(得分:0)
您需要导入以下软件包
compile "org.apache.avro:avro:1.8.2"
compile "org.apache.avro:avro-tools:1.8.2"
compile "org.apache.avro:trevni-core:1.8.2"
compile "org.apache.avro:trevni-avro:1.8.2"