我想在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();
}
Run Code Online (Sandbox Code Playgroud)
它会创建文件,但不会在其中写入任何内容.我搜索了很多但没有找到任何东西.我的问题是什么?我是否需要获得HDFS写入权限?
谢谢.
Mig*_*ira 68
替换@Tariq的asnwer你可以在获取文件系统时传递URI
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();
Run Code Online (Sandbox Code Playgroud)
Tar*_*riq 22
将HADOOP_CONF_DIR环境变量定义到Hadoop配置文件夹,或在代码中添加以下2行:
config.addResource(new Path("/HADOOP_HOME/conf/core-site.xml"));
config.addResource(new Path("/HADOOP_HOME/conf/hdfs-site.xml"));
Run Code Online (Sandbox Code Playgroud)
如果您不添加此项,您的客户端将尝试写入本地FS,从而导致权限被拒绝的异常.
小智 -2
请尝试以下方法。
FileSystem fs = path.getFileSystem(conf);
SequenceFile.Writer inputWriter = new SequenceFile.Writer(fs, conf, path, LongWritable.class, MyWritable.class);
inputWriter.append(new LongWritable(uniqueId++), new MyWritable(data));
inputWriter.close();
Run Code Online (Sandbox Code Playgroud)