2017-03-20 28 views
0

我试图将JSON文件转换为CSV。HDS上的JSON转换为CSV

我有一个JAVA代码,它能够在UNIX文件系统和本地文件系统上完美实现它。 我已经在主类下面写了在HDFS上执行这个转换。

public class ClassMain { 

    public static void main(String[] args) throws IOException { 


     String uri = args[1]; 
     String uri1 = args[2]; 
     Configuration conf = new Configuration(); 
     FileSystem fs = FileSystem.get(URI.create(uri), conf); 
     FSDataInputStream in = null; 
     FSDataOutputStream out = fs.create(new Path(uri1)); 
     try{ 

      in = fs.open(new Path(uri)); 

      JsonToCSV toCSV = new JsonToCSV(uri); 
      toCSV.json2Sheet().write2csv(uri1); 
      IOUtils.copyBytes(in, out, 4096, false); 

      } 
     finally{ 
      IOUtils.closeStream(in); 
      IOUtils.closeStream(out); 
     } 
    } 

} 

json2sheet和write2csv是其中执行转换和写入操作的方法。

我使用下面的命令运行这个jar:

hadoop jar json-csv-hdfs.jar com.nishant.ClassMain /nishant/large.json /nishant/output 

的问题是,它并没有写在/ NISHANT /输出任何东西。它创建一个0大小/ nishant /输出文件。

也许在这里使用copyBytes不是一个好主意。

如何在HDFS上实现这一点,如果它在unix FS和本地FS上工作正常。

在这里,我试图JSON文件转换为CSV,而不是试图JSON对象映射到它们的值

+0

不,它不是类似的问题。这里我试图将JSON文件转换为CSV。在你提到的链接中,它试图将JSON对象映射到它们的值。 – earl

+0

为什么你不像Spark SQL,Hive这样的SQL工具? – mrsrinivas

回答

0

文件系统只需要一个配置项成功连接到HDFS。

conf.set(key, "hdfs://host:port"); // where key="fs.default.name"|"fs.defaultFS"