从文件创建对象-java

slsn1g29  于 2021-05-27  发布在  Hadoop
关注(0)|答案(0)|浏览(283)

我有一个问题,我应该向mapper(hadoop的map reduce)传递一个对象,所以我尝试将该对象放入json中,如下所示:

Object2ObjectOpenHashMap<OpenBitSet, CombinationHelper> level1 = ...;
String level1path="pathFile.txt";

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
String JsonLevel1 = gson.toJson(level1);

BufferedWriter writerContainTuples = new BufferedWriter(new FileWriter(level1path));
writerContainTuples.write(JsonLevel1);
writerContainTuples.close();

在mapper中,我通常以这种方式从文件中获取对象:

public void map(Object ke, Text val, Context context) throws IOException, InterruptedException {    
    level1 = new Gson().fromJson(val.toString(), new TypeToken<Object2ObjectOpenHashMap<OpenBitSet, CombinationHelper>>(){}.getType());
        ...       
    }
}

但当我运行程序时,它会给我一个错误:

java.lang.Exception: org.apache.hadoop.shaded.com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 3

因此,如果java以这种方式给我这个错误,我不知道应该如何传递我的对象。我尝试过序列化,但在Map器阶段,它给了我几乎相同的错误。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题