我能够处理来自 xml 的两个节点。我得到以下输出:
bin/hadoop fs -text /user/root/t-output1/part-r-00000
name:ST17925 currentgrade 1.02
name:ST17926 currentgrade 3.0
name:ST17927 currentgrade 3.0
但我需要这样的输出:
studentid curentgrade
ST17925 1.02
ST17926 3.00
ST17927 3.00
我怎样才能做到这一点?
我的完整源代码:https://github.com/studhadoop/xml/blob/master/XmlParser11.java
编辑:解决方案
protected void setup(Context context) throws IOException, InterruptedException {
context.write(new Text("studentid"), new Text("currentgrade"));
}
最佳答案
我认为很难与您的 MapReduce 代码一起执行此操作。原因是
- 标题可能不是相同的数据类型
- 如果类型相同,您可以从 Reducer 代码的 setup() 方法编写 header ,但不保证 header 将出现在输出的第一行。
您最多可以做的是,在第一次遇到列限定符时,使用 map 代码中的标题创建一个单独的 HDFS/本地文件。您需要使用适当的文件操作 API 来创建此文件。稍后当作业完成时,您可以在其他程序中使用这些 header 或将它们合并为一个文件。
关于hadoop - 如何将第一行输出为列限定符名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330413/