首页 > 解决方案 > 如何在 MapReduce 程序中将文件附加到 HDFS

问题描述

我正在使用CDH-5.14.2-1.cdh5.14.2.p0.3. 我试图将一些字符串附加到 HDFS 上的现有文件中。

然后我写如下:

FileSystem fs = FileSystem.get(conf);
String str = "testtest";
FSDataOutputStream out = fs.append(new Path("tmp/tmp_file"));
InputStream in = new ByteArrayInputStream(str.getBytes("utf-8"));
byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
   out.write(b, 0, numBytes);
}

没有错误,但没有写入任何内容tmp_filetmp_file是一个空白文件)。我tmp_file通过 using 创建了另一种方法fs.create(path),因此tmp_file创建了但 append 并不顺利。

我读过一些帖子,我意识到该属性dfs.support.append必须是true. 所以我搜索了Cloudera Manager,但我找不到。相反,我写了conf.set("dfs.support.append", "true"),但这毫无意义。

我用过IOUtils.copyBytes(in, out, 4096, true)out.writeBytes(String tmp)但什么也没写。

我搜索并找到以下设置: 环境

我像这个图一样设置,Deplor Client Configuration然后重新启动所有服务。但什么都没有改变……</p>

我弄错了吗?

标签: javahadoophdfs

解决方案


推荐阅读