如何在ApacheBeam中将json数据和sava重新生成为拼花地板

如何在ApacheBeam中将json数据和sava重新生成为拼花地板,json,apache-beam,parquet,Json,Apache Beam,Parquet,这是一个困扰我两天的问题 我可以从本地读取json数据,但当我将其写入拼花地板时会出错。 我的代码如下: public class parquet_save_convert { private static final Schema SCHEMA = new Schema.Parser().parse( "{ \n" + " \"namespace\": \"com.navteq.avr

这是一个困扰我两天的问题

我可以从本地读取json数据,但当我将其写入拼花地板时会出错。

我的代码如下:

public class parquet_save_convert {

private static final Schema SCHEMA = new Schema.Parser().parse(
        "{ \n" +
                " \"namespace\": \"com.navteq.avro\", \n" +
                " \"name\": \"FacebookUser\", \n" +
                " \"type\": \"record\",\n" +
                " \"fields\": [\n" +
                " {\"name\": \"event_level\", \"type\": \"string\"},\n" +
                " {\"name\": \"spm_page\", \"type\": \"string\"},\n" +
                " {\"name\": \"spm_module\", \"type\": \"string\"} ]\n" +
                "}");


public static void main(String[] args) {
    Gson gson=new  GsonBuilder().create();
    String outputPath = "./output/parquet";
    PipelineOptions options = PipelineOptionsFactory.create();
    Pipeline pipeline = Pipeline.create(options);
    pipeline.apply(TextIO.read().from("./input/event_type.json"))
            .apply(ParDo.of(new DoFn<String,GenericRecord>(){
                @ProcessElement
                public void processElement(ProcessContext c){
                    HashMap<String,String> map= gson.fromJson(c.element().toString(),HashMap.class);

                    GenericRecord osRecord = new GenericData.Record(SCHEMA);

                    map.forEach((k,v)->{
                        osRecord.put(k,v);
                    });

                    c.output(osRecord);
                }
            }))
            .setCoder(AvroCoder.of(GenericRecord.class,SCHEMA))
            .apply(FileIO.<GenericRecord>write()
                    .via(ParquetIO.sink(SCHEMA)).to(outputPath)
                    .withSuffix(".parquet"));
    pipeline.run().waitUntilFinish();}
例外情况是:

Exception in thread "main" java.lang.IllegalArgumentException: unable to serialize DoFnWithExecutionInformation{doFn=parquet_save_convert$1@5d10455d, mainOutputTag=Tag<output>, sideInputMapping={}, schemaInformation=DoFnSchemaInformation{elementConverters=[]}}
线程“main”java.lang.IllegalArgumentException中的异常:无法序列化DoFnWithExecutionInformation{doFn=parquet\u save\u convert$1@5d10455d,mainOutputTag=Tag,sideInputMapping={},schemaInformation=DoFnSchemaInformation{elementConverters=[]} 我也谷歌这个问题,但没有得到答案,这让我感到非常沮丧


提前谢谢。

我猜您的Gson对象不可序列化。解决方案可能是在DoFn的方法中将其初始化为局部变量。

我重写了代码,问题就解决了。但是,当我想保存一个嵌套的json,如“{”event_common“{”timestamp:1596946319063,“spm”:“City.popularity_LIST”}”,我得到一个新的prolem,如下:“线程中的异常”mainorg.apache.beam.sdk.Pipeline$PipelineExecutionException:java.lang.ClassCastException:java.util.LinkedHashMap不能强制转换为org.apache.avro.generic.IndexedRecord字段事件\u common“
Exception in thread "main" java.lang.IllegalArgumentException: unable to serialize DoFnWithExecutionInformation{doFn=parquet_save_convert$1@5d10455d, mainOutputTag=Tag<output>, sideInputMapping={}, schemaInformation=DoFnSchemaInformation{elementConverters=[]}}