我想在HDFS中创建文件并在其中写入数据。我使用以下代码:
Configuration config = new Configuration(); FileSystem fs = FileSystem.get(config); Path filenamePath = new Path("input.txt"); try { if (fs.exists(filenamePath)) { fs.delete(filenamePath, true); } FSDataOutputStream fin = fs.create(filenamePath); fin.writeUTF("hello"); fin.close(); }
它创建文件,但不写入任何内容。我搜索了很多,但没有找到任何东西。我怎么了 我是否需要任何权限才能在HDFS中写入?
@Tariq的替代方法,你可以在获取文件系统时传递URI
@Tariq
Configuration configuration = new Configuration(); FileSystem hdfs = FileSystem.get( new URI( "hdfs://localhost:54310" ), configuration ); Path file = new Path("hdfs://localhost:54310/s2013/batch/table.html"); if ( hdfs.exists( file )) { hdfs.delete( file, true ); } OutputStream os = hdfs.create( file, new Progressable() { public void progress() { out.println("...bytes written: [ "+bytesWritten+" ]"); } }); BufferedWriter br = new BufferedWriter( new OutputStreamWriter( os, "UTF-8" ) ); br.write("Hello World"); br.close(); hdfs.close();