【发布时间】:2020-02-21 16:28:37
【问题描述】:
我正在尝试将 JSON 文件读入我的 hadoop mapreduce 算法。 我怎样才能做到这一点?我已将文件“testinput.json”放入 HDFS 内存的 /input 中。
当调用 mapreduce 时,我执行 hadoop jar popularityMR2.jar populariy input output,输入说明 dhfs 内存中的输入目录。
public static class PopularityMapper extends Mapper<Object, Text, Text, Text>{
protected void map(Object key, Text value,
Context context)
throws IOException, InterruptedException {
JSONParser jsonParser = new JSONParser();
try {
JSONObject jsonobject = (JSONObject) jsonParser.parse(new FileReader("hdfs://input/testinput.json"));
JSONArray jsonArray = (JSONArray) jsonobject.get("votes");
Iterator<JSONObject> iterator = jsonArray.iterator();
while(iterator.hasNext()) {
JSONObject obj = iterator.next();
String song_id_rave_id = (String) obj.get("song_ID") + "," + (String) obj.get("rave_ID")+ ",";
String preference = (String) obj.get("preference");
System.out.println(song_id_rave_id + "||" + preference);
context.write(new Text(song_id_rave_id), new Text(preference));
}
}catch(ParseException e) {
e.printStackTrace();
}
}
}
我的映射器函数现在看起来像这样。我从 dhfs 内存中读取文件。但它总是返回错误,找不到文件。
有人知道我如何将这个 json 读入 jsonobject 吗?
谢谢
【问题讨论】:
-
或者我需要在这里使用 InputstreamReader 吗?如果有,怎么做?
标签: json hadoop mapreduce reader