hadoop2.2.0追加文件发生AlreadyBeingCreatedException

2024-05-04

我遇到了一个关于hadoop2.2.0追加操作的问题。我通过 HDFS java API 将一些字节附加到 hdfs 文件。首先,如果在附加操作之前文件不存在,我将创建目标文件,代码如下:

String fileUri = "hdfs://hadoopmaster:9000/in/append_test.txt";
// create the hdfs file, if not exists
HdfsClient.createPathIfNotExist(fileUri);
// do 5 times append operation
for (int i=0; i<5; i++){
    HdfsClient.appendTo(fileUri, ("append content"+i).getBytes("UTF-8"));
}

The createPathIfNotExist功能:

Path p = null;
FileSystem fs = null;
try {
    fs = FileSystem.get(URI.create(uri), conf);
    p = new Path(uri);
    if (!fs.exists(p)) {
    if (uri.charAt(uri.length() - 1) == '/'){ //create a directory
        if(fs.mkdirs(p)){
               // create successfully
            }
        }else{ //create a file
        FSDataOutputStream fos = fs.create(p);
            fos.close();
        }
    } else{
        System.out.println(uri + "existing");
    }
} catch (IOException e) {
    e.printStackTrace();
} finally{
    if (fs != null)
    try {
       fs.close();
       fs = null;
    } catch (IOException e) {
       e.printStackTrace();
        }
}

The appendTo功能:

ByteArrayInputStream in = null;
OutputStream out = null;
FileSystem fs = null;
try {
   in = new ByteArrayInputStream(bytes);
   fs = FileSystem.get(URI.create(uri), conf);
   out = fs.append(new Path(uri)); //get append outputstream
   IOUtils.copyBytes(in, out, bufferSize, false);
} catch(Exception e){
   e.printStackTrace();
} finally{
   if (in != null) IOUtils.closeStream(in);
   if (out != null) IOUtils.closeStream(out);
   if (fs != null){
    try {
           fs.close();
           fs = null;
    } catch (IOException e) {
       e.printStackTrace();
    }
   }
}

结果是创建了append_test.txt,但是内容只有:

append content0

并出现异常:

org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException): Failed to create file [/in/append_test.txt] for [DFSClient_NONMAPREDUCE_-1148656837_1] on client [192.168.141.1], because this file is already being created by [DFSClient_NONMAPREDUCE_2099912242_1] on [192.168.141.1]
        at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.recoverLeaseInternal(FSNamesystem.java:2320)
        at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInternal(FSNamesystem.java:2153)
        at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFileInt(FSNamesystem.java:2386)
        at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.appendFile(FSNamesystem.java:2347)
        at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.append(NameNodeRpcServer.java:508)
        at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolServerSideTranslatorPB.append(ClientNamenodeProtocolServerSideTranslatorPB.java:320)
        at org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos$ClientNamenodeProtocol$2.callBlockingMethod(ClientNamenodeProtocolProtos.java:59572)
        at org.apache.hadoop.ipc.ProtobufRpcEngine$Server$ProtoBufRpcInvoker.call(ProtobufRpcEngine.java:585)
        at org.apache.hadoop.ipc.RPC$Server.call(RPC.java:928)
        at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2048)
        at org.apache.hadoop.ipc.Server$Handler$1.run(Server.java:2044)
        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:1491)
        at org.apache.hadoop.ipc.Server$Handler.run(Server.java:2042)

        at org.apache.hadoop.ipc.Client.call(Client.java:1347)
        at org.apache.hadoop.ipc.Client.call(Client.java:1300)
        at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:206)
        at com.sun.proxy.$Proxy10.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:186)
        at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:102)
        at com.sun.proxy.$Proxy10.append(Unknown Source)
        at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB.append(ClientNamenodeProtocolTranslatorPB.java:245)
        at org.apache.hadoop.hdfs.DFSClient.callAppend(DFSClient.java:1480)
        at org.apache.hadoop.hdfs.DFSClient.append(DFSClient.java:1520)
        at org.apache.hadoop.hdfs.DFSClient.append(DFSClient.java:1508)
        at org.apache.hadoop.hdfs.DistributedFileSystem$4.doCall(DistributedFileSystem.java:310)
        at org.apache.hadoop.hdfs.DistributedFileSystem$4.doCall(DistributedFileSystem.java:306)
        at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)
        at org.apache.hadoop.hdfs.DistributedFileSystem.append(DistributedFileSystem.java:306)
        at org.apache.hadoop.fs.FileSystem.append(FileSystem.java:1160)
        at org.lh.blog.message.hadoop.HdfsClient$2.run(HdfsClient.java:130)
        at org.lh.blog.message.hadoop.HdfsClient$2.run(HdfsClient.java:1)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.security.auth.Subject.doAs(Subject.java:356)
        at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1471)
        at org.lh.blog.message.hadoop.HdfsClient.appendTo(HdfsClient.java:121)
        at org.lh.blog.message.hadoop.HdfsClient.appendTo(HdfsClient.java:110)
        at org.lh.blog.message.test.HdfsClientTests.testCreateFileBeforeAppend(HdfsClientTests.java:26)
        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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

也就是说,它在创建不存在的文件后只做了一次追加操作,其他4次追加操作失败,出现上述错误。 我在附加之前创建了文件,但它说AlreadyBeingCreatedException,我有些困惑。

我也有一些尝试。我发现java API创建的hdfs文件,都不能进行append操作。但是通过 hdfs 命令(等等,“hdfs dfs -put”)创建的 hdfs 文件可以进行追加操作。

你能帮助我,给我一些建议吗?

感谢和问候。


为了解决这个问题,

  1. 读取文件内容并将其存储到变量中。
  2. 添加您想要附加到此变量的新内容。
  3. 重新创建该文件并将内容写回其中。

这个过程对我来说效果很好并解决了问题。

APPEND 操作的成本很高,如果您尝试并行尝试它,那么就会出现这个问题。因此,重新创建文件并将内容重新写入其中而不是附加。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hadoop2.2.0追加文件发生AlreadyBeingCreatedException 的相关文章

随机推荐

  • 如何在外部 .js 文件中分离 .vue 组件的方法?

    我的组件留下了许多行代码 因此我决定将这些方法放在一个名为functions js 的单独文件中 我无法调用这些方法 我试过这个 函数 js function sendList function getLists function dele
  • Jsoup遍历DOM树时节点哈希码冲突

    我正在使用 java jsoup 构建 HTML DOM 树 其中Node hashCode 用来 但我发现在遍历DOM树时存在很多哈希码冲突 使用以下代码 doc traverse new NodeVisitor Override pub
  • 如何保存具有多个缩进设置的XmlDocument?

    我需要保存一个XmlDocument以适当的缩进归档 Formatting Indented 但有些节点及其子节点必须排在一行 Formatting None 从那时起如何实现这一目标XmlTextWriter接受整个文档的设置 在 Ahm
  • 设置正则表达式中的最小和最大字符

    我写了一个正则表达式 http en wikipedia org wiki Regular expression匹配任意数量的字母 字母之间有任意数量的单个空格 我希望该正则表达式也强制执行最小和最大字符数 但我不确定如何做到这一点 或者是
  • 如何使用 Objective-C 在 Mac Os X 中模拟 Unicode Char“按键”?

    我想在 Mac OS X 中模拟 unicode 字符发送到前台应用程序 我的意思是我有一个像 a 这样的unicode char 可以包含阿拉伯语 中文等 我想输入它 请注意 我并不是尝试使用虚拟按键或按键代码 只有一个角色 您忠诚的 佩
  • 悬停时连续 CSS 旋转动画,悬停时动画回到 0deg

    我有一个元素 当你将鼠标悬停在它上面时 它会无限旋转 当您将鼠标悬停在外面时 动画就会停止 简单的 webkit keyframes rotate from webkit transform rotate 0deg to webkit tr
  • 在 ios 7 设备上获取多个应用程序图标

    我在 ios7 iPhone5 设备 上遇到一个奇怪的错误 我在ios7上使用xcode5安装了iPhone应用程序我的问题是每当我在设备上安装应用程序时 我都会收到多个应用程序图标 多次使用相同的名称 我还尝试通过我的系统使用不同的应用程
  • FacebookCallback#onCancel() 在 Android 的 Facebook Sdk 4.2.0 中登录一次后调用

    我已经在 Android 应用程序中成功实现了 Facebook 登录 但现在 令人惊讶的是 当我使用 facebook 登录时 如果我尝试使用 facebook 库 4 2 0 登录 facebook 它的 onCancel 方法就会被调
  • 如何使用 pandas groupby 函数根据 groupby 值应用公式

    我的问题可能有点令人困惑 所以让我解释一下 我有一个信息数据框 我想按唯一订单 ID 对其进行分组 该 ID 将生成以下列 sum qty 每个订单 ID 执行的总金额 csv 这是每个订单 ID 的 csv 列的总和除以订单 ID 的已执
  • 使用 JavaScript 检测硬重新加载

    为了澄清 I am not试图区分刷新和重新加载 因此这不是重复的刷新与重新加载 https stackoverflow com questions 5004978 check if page gets reloaded or refres
  • codeigniter core/model.php 未定义属性

    我从未接触过 model php 文件 但是我收到了此错误 Jobprocess 是我的控制器 lastname是在其中正确分配的变量 我不知道为什么会出现这个错误 这是使用 codeigniter 框架 Message Undefined
  • pyspark:计算窗口上的不同值

    我刚刚尝试做一个countDistinct越过一个窗口并得到这个错误 AnalysisException 不支持不同的窗口函数 计数 不同颜色 1926 有没有办法在 pyspark 的窗口上进行不同的计数 这是一些示例代码 from py
  • 在同一台服务器上运行两个 PHP 版本

    我在本地服务器上有两个项目 一个项目运行PHP5 6 另一个项目运行PHP7 0 现在可以根据项目启用这两个版本吗 我已经尝试添加AddHandler application x httpd php7 php在 htaccess 项目之一中
  • 我从 clojure 和 python 中得到的 hmac 签名略有不同

    我从 python 实现和 clojure 实现中获得的 HMAC SHA1 签名略有不同 我很困惑什么会导致这种情况 Python实现 import hashlib import hmac print hmac new my key my
  • org.json.JSONArray 类型的值无法转换为 JSONObject

    陷入了这个错误 3169 3190 com meisolsson app E JSON 解析器 解析数据时出错 org json JSONException Value type 0 can see custom stories true
  • 这叫什么类型的建筑?

    对于我当前正在开发的 Web 应用程序 ASP NET MVC 我们采用以下架构 Data Access Layer 将数据持久保存到任意数据库的逻辑 Domain 数据模型 Service Layer 业务逻辑 例如订单处理 账户管理等
  • 在 Codeigniter 中添加表前缀以加入

    我设置了 Codeigniter 将前缀 kms 添加到我的活动记录查询中 但是 我尝试使用两个 ON 条件进行连接 但它不会将它们放在前面 现在我必须像这样手动添加它们 this gt db gt join site items kms
  • 改造:无法为类创建 @Body 转换器

    我需要通过改造 2 发送下一个 json Inspection UUID name ModifiedTime 2016 03 09T01 13 CreatedTime 2016 03 09T01 13 ReviewedWith name2
  • getView() 和 getActivity() 有什么区别?

    有什么区别getView and getActivity 我已经使用了这两种方法 但不明白基本的区别 即使使用方法在android中也是相同的 ListView deliverItemList ListView getView findVi
  • hadoop2.2.0追加文件发生AlreadyBeingCreatedException

    我遇到了一个关于hadoop2 2 0追加操作的问题 我通过 HDFS java API 将一些字节附加到 hdfs 文件 首先 如果在附加操作之前文件不存在 我将创建目标文件 代码如下 String fileUri hdfs hadoop