[英]Weird behavior while parsing Json with a DoFn
我正在嘗試解析一些我已經存儲在Google雲存儲中的Json。 我正在使用Apache Beam創建一個管道,該管道讀取存儲的json,然后將其寫入雲sql數據庫。 在編寫我的解析方法時,我遇到了一些奇怪的行為。
這是我的傑森:
[{
"projectid": "Reminder101",
"reminderkey": "001",
"localid": "01",
"timestamp": "2018-01-24 12:00"
},
{
"projectid": "Reminder101",
"reminderkey": "002",
"localid": "02",
"timestamp": "2018-01-25 9:00"
},
{
"projectid": "Reminder101",
"reminderkey": "003",
"localid": "03",
"timestamp": "2018-02-01 18:00"
},
{
"projectid": "Reminder101",
"reminderkey": "004",
"localid": "04",
"timestamp": "2018-02-6 15:35"
},
{
"projectid": "USReminder101",
"reminderkey": "001",
"localid": "01",
"timestamp": "2018/01/30 21:00"
}
]
這是我的json解析方法(JsonHolder只是一個pojo):
static class ParseJsonDoFn extends DoFn<String, List<JsonHolder>> {
@ProcessElement
public void processElement(ProcessContext context) {
String incomingInfo = context.element();
Gson gson = new Gson();
System.out.println(incomingInfo.toString());
Type type = new TypeToken<List<JsonHolder>>(){}.getType();
List<JsonHolder> jsonholders = gson.fromJson(incomingInfo, type);
context.output(jsonholders);
}
}
現在,當我像這樣運行管道時,我得到了錯誤:
Exception in thread "main"
org.apache.beam.sdk.Pipeline$PipelineExecutionException:
com.google.gson.JsonSyntaxException:
com.google.gson.stream.MalformedJsonException: Expected value at line 1
column 2 path $
System.out.println(); 顯示:
"timestamp": "2018-02-01 18:00"
},
"reminderkey": "004",
[{
"localid": "01",
},
"timestamp": "2018-01-24 12:00"
"reminderkey": "002",
但是,如果我創建的Json文件是未格式化的單行,則可以很好地解析。 我可以推斷出,Json文件在每一行結束后都會被拆分,但是我一生都無法弄清楚為什么或如何更正它。
干杯。
TextIO.read()讀取文本文件,將每一行作為PCollection的單獨元素返回。 這樣做是為了使它可以處理大小不受限制的文件,而無需將文件內容加載到內存中。
如果您的輸入格式不是行分隔符,則可能需要更靈活的FileIO:match()來查找與您感興趣的文件模式匹配的文件,read()來自動解壓縮它們並以ReadableFile。 然后使用DoFn以任意方式解析文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.