无法将现有文件附加到hdfs

omtl5h9j  于 2021-06-04  发布在  Hadoop
关注(0)|答案(3)|浏览(417)

我在vm上运行了单节点hadoop1.2.1集群。
我的hdfs-site.xml如下所示:

<configuration>
<property>
  <name>dfs.replication</name>
  <value>1</value>
  <description>Default block replication.
 </description>
</property>
<property>
  <name>dfs.support.append</name>
  <value>true</value>
  <description>Does HDFS allow appends to files?
  </description>
</property>
</configuration>

现在,当我尝试从eclipse运行以下代码时,它总是返回false:

Configuration config = new Configuration();
    config.set("mapred.job.tracker","10.0.0.6:54311");
    config.set("fs.default.name","hdfs://10.0.0.6:54310");
    FileSystem fs = FileSystem.get(config);
    boolean flag = Boolean.getBoolean(fs.getConf().get("dfs.support.append"));
    System.out.println("dfs.support.append is set to be " + flag);

现在,如果我试图附加到现有文件,我将得到以下错误:

org.apache.hadoop.ipc.RemoteException: java.io.IOException: Append is not supported. Please see the dfs.support.append configuration parameter
    at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:1781)
    at org.apache.hadoop.hdfs.server.namenode.NameNode.append(NameNode.java:725)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:587)
    at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1432)
    at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:1428)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:415)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190)
    at org.apache.hadoop.ipc.Server$Handler.run(Server.java:1426)

    at org.apache.hadoop.ipc.Client.call(Client.java:1113)
    at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:229)
    at com.sun.proxy.$Proxy1.append(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocationHandler.java:85)
    at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:62)
    at com.sun.proxy.$Proxy1.append(Unknown Source)
    at org.apache.hadoop.hdfs.DFSClient.append(DFSClient.java:933)
    at org.apache.hadoop.hdfs.DFSClient.append(DFSClient.java:922)
    at org.apache.hadoop.hdfs.DistributedFileSystem.append(DistributedFileSystem.java:196)
    at org.apache.hadoop.fs.FileSystem.append(FileSystem.java:659)
    at com.vanilla.hadoop.AppendToHdfsFile.main(AppendToHdfsFile.java:29)

怎么了?我错过什么了吗?

o75abkj4

o75abkj41#

您应该尝试使用2.x.x版本或0.2x版本,因为在hadoop0.20.2之后在hdfs上附加一个文件。在此处和此处查看更多信息

liwlm1x9

liwlm1x92#

现在让我们从配置文件系统开始:

public FileSystem configureFileSystem(String coreSitePath, String hdfsSitePath) {
    FileSystem fileSystem = null;
    try {
        Configuration conf = new Configuration();
        conf.setBoolean("dfs.support.append", true);
        Path coreSite = new Path(coreSitePath);
        Path hdfsSite = new Path(hdfsSitePath);
        conf.addResource(coreSite);
        conf.addResource(hdfsSite);
        fileSystem = FileSystem.get(conf);
    } catch (IOException ex) {
        System.out.println("Error occurred while configuring FileSystem");
    }
    return fileSystem;
}

确保hdfs-site.xml中的属性dfs.support.append设置为true。
您可以通过编辑hdfs-site.xml文件手动设置,也可以通过以下方式编程设置:
conf.setboolean(“dfs.support.append”,true);
让我们从附加到hdfs中的文件开始。

public String appendToFile(FileSystem fileSystem, String content, String dest) throws IOException {
    Path destPath = new Path(dest);
    if (!fileSystem.exists(destPath)) {
        System.err.println("File doesn't exist");
        return "Failure";
    }
    Boolean isAppendable = Boolean.valueOf(fileSystem.getConf().get("dfs.support.append"));
    if(isAppendable) {
        FSDataOutputStream fs_append = fileSystem.append(destPath);
        PrintWriter writer = new PrintWriter(fs_append);
        writer.append(content);
        writer.flush();
        fs_append.hflush();
        writer.close();
        fs_append.close();
        return "Success";
    }
    else {
        System.err.println("Please set the dfs.support.append property to true");
        return "Failure";
    }
}

为了查看数据是否已正确写入hdfs,让我们编写一个从hdfs读取的方法,并将内容作为字符串返回。

public String readFromHdfs(FileSystem fileSystem, String hdfsFilePath) {
    Path hdfsPath = new Path(hdfsFilePath);
    StringBuilder fileContent = new StringBuilder("");
    try{
        BufferedReader bfr=new BufferedReader(new InputStreamReader(fileSystem.open(hdfsPath)));
        String str;
        while ((str = bfr.readLine()) != null) {
            fileContent.append(str+"\n");
        }
    }
    catch (IOException ex){
        System.out.println("----------Could not read from HDFS---------\n");
    }
    return fileContent.toString();
}

之后,我们成功地用hdfs编写和读取了文件。是时候关闭文件系统了。

public void closeFileSystem(FileSystem fileSystem){
    try {
        fileSystem.close();
    }
    catch (IOException ex){
        System.out.println("----------Could not close the FileSystem----------");
    }
}

在执行代码之前,应该在系统上运行hadoop。
您只需转到hadoop\u主页并运行以下命令:
./sbin/start-all.sh全部启动
供完全参考使用https://github.com/ksimar/hdfs_appendapi

moiiocjp

moiiocjp3#

从1.0.3开始不支持追加。无论如何,如果您真的需要前面的功能,要启用append功能,请将标志“dfs.support.breake.append”设置为true。
hadoop.apache.org/docs/r1.2.1/releasenotes.html

相关问题