[英]JSON to Java conversion using GSON throws error
在我的火花代碼中,我有gson,它存儲在字符串RDD中,我將其轉換為鍵和值。
JavaPairRDD<String, ArrayList<Guid>> docPairRDD = docRDD.mapToPair(SparkCustomFunctions.generateKeyValuePair());
/**
* @return new Tuple with key is of type String and value is of type ArrayList<DocId>
*/
public static PairFunction<String, String, ArrayList<DocId>> generateKeyValuePair() {
return new PairFunction<String, String, ArrayList<DocId>>(){
private static final long serialVersionUID = 9086064488812696200L;
public Tuple2<String, ArrayList<DocId>> call(String t)
throws Exception {
GsonBuilder gson = new GsonBuilder();
Document tempObj = gson.create().fromJson(t, Document.class);
return new Tuple2<String, ArrayList<DocId>>(tempObj.getKey(),(ArrayList<DocId>) tempObj.getEventFields());
}
};
}
}
(1)。
public class Document implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("key")
String key;
@SerializedName("eventFields")
List<DocId> eventFields= new ArrayList<DocId>();
public Document (String key, List<DocId> eventFields) {
super();
this.key = key;
this.eventFields= eventFields;
}
Its setter and getter
}
(2)。
public class DocId implements Serializable {
private static final long serialVersionUID = 1L;
String DocId = "";
String cType = "";
String events;
public DocId(String docId, String cType, String eType){
super();
this.DocId = docId;
this.cType = cType;
this.events = eType;
}
their setters and getters
}
但是我收到以下錯誤:com.google.gson.JsonSyntaxException:java.io.EOFException:com.google上com.google.gson.Gson.fromJson(Gson.java:813)第1行第2列的輸入結束。 gson.Gson.fromJson(Gson.java:768)at com.google.gson.Gson.fromJson(Gson.java:717)at com.google.gson.Gson.fromJson(Gson.java:689)
我是gsona和spark的新人。 那么請讓我知道我做錯了什么?
我將以下類型的json存儲到RDD中
{
"key": "01key",
"eventFields": [
{
"guid": "abc12",
"contentType": "dummyContent",
"events": "dummyEvents"
}
]
}
所以我發現了這個問題,在存儲我的響應之前,我正在做漂亮的印刷,所以json打破了多行,在閱讀這個json時,我假設每一行都是完整的json,所以我得到了錯誤。 我刪除了prettyprint,我的代碼開始正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.