我想在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中写入?谢谢。
3 回答

30秒到达战场
TA贡献1828条经验 获得超6个赞
HADOOP_CONF_DIR在您的Hadoop配置文件夹中定义环境变量,或者在代码中添加以下两行:
config.addResource(new Path("/HADOOP_HOME/conf/core-site.xml"));
config.addResource(new Path("/HADOOP_HOME/conf/hdfs-site.xml"));
如果不添加它,则客户端将尝试写入本地FS,从而导致权限被拒绝的异常。

慕森卡
TA贡献1806条经验 获得超8个赞
请尝试以下方法。
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();
添加回答
举报
0/150
提交
取消