使用hadoop的xml到csv

时间:2014-02-05 19:40:49

标签: csv hadoop xml-parsing

我正在尝试使用hadoop将我的xml文件转换为csv,所以我在mapper类中使用以下代码

   protected void map(LongWritable key, Text value,
                 @SuppressWarnings("rawtypes") Mapper.Context context)
  throws
  IOException, InterruptedException {
String document = value.toString();
System.out.println("‘" + document + "‘");
    try {
  XMLStreamReader reader =
      XMLInputFactory.newInstance().createXMLStreamReader(new
          ByteArrayInputStream(document.getBytes()));
  String propertyName = "";
  String propertyValue = "";
  String currentElement = "";
  while (reader.hasNext()) {
    int code = reader.next();
    switch (code) {
      case XMLStreamConstants.START_ELEMENT: //START_ELEMENT:
        currentElement = reader.getLocalName();
        break;
      case XMLStreamConstants.CHARACTERS:  //CHARACTERS:
        if (currentElement.equalsIgnoreCase("author")) {
          propertyName += reader.getText();
         } else if (currentElement.equalsIgnoreCase("price"))
        {
            String name=reader.getText();
            name.trim();
          propertyName += name;
          propertyName.trim();
         }
 }
        console.write(null,new Text(propertyName));
 }
 }

但我得到的输出是这种形式

Gambardella, Matthew
      XML Developer's Guide
      44.95
      2000-10-01


Ralls, Kim
      Midnight Rain
      5.95
      2000-12-16

你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

程序的输出取决于您从mapper收集/写入的方式。

在这种情况下,您应该使用TextOutputFormat& KeyOut将为NullWritable,ValueOut将为Text。值out应该是您从CSV中提取的值的串联。

从您的代码中看起来,您在从XML读取每个值后编写输出。