分享

chang_quanyou@163.com

chang 发表于 2016-3-7 14:56:22 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 7880
最近因为项目需求;需要在hdfs文件上追加日志,需要调用append()方法,版本hadoop 2.6;本次就总结一下遇到的问题以及解决方案:
1:不修改hdfs-site.xml
            我的测试集圈是我用虚拟机搭建的,一个namenode,一个datanode;此时我只是在代码中加入啦一行设置副本数:
        参照文章:  http://stackoverflow.com/questions/24262362/hadoop-2-2-0-recoveryinprogressexception-while-appending-content-to-an-existing
       Even though the dfs.replication is set to 1 in hdfs-site.xml,the replication value will still be set to 3(Please suggest as to why this                                happens,restarted couple of times).I had to set the value of replication to 1 to make it work ,from the code using,
        
       fs.setReplication(hdfs_path, (short)1);
        这时联我的测试集群是可以的;没有任何问题;但是在我公司的集圈上进行测试;确是不可以的。
2:修改hdfs-site.xml      参照帖子:http://www.aboutyun.com/forum.php?mod=viewthread&tid=10305
      添加以下配置:

          <property>
             <name>dfs.support.append</name>
             <value>true</value>
          </property>
         代码:
         static {
                try {
                        Configuration conf = new Configuration();
                //        conf.setBoolean("dfs.support.append", true);注释掉也是可以的
                        fs = FileSystem.get(new URI(ReadConfigUtil.getValue("hadoop.master")), conf);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
      文件追加:
         
public static void writeFile(String data, String resultLogDownloadPath) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
                Path path = new Path(resultLogDownloadPath);
                FSDataOutputStream fsDataOutputStream = null;
                try {
                        if (!fs.exists(path)) {
                                System.err.println("不存在路径---->创建" + resultLogDownloadPath);
                                fsDataOutputStream = fs.create(path);
                                fsDataOutputStream
                                .writeUTF(df.format(new Date()) + "\t" + data + System.getProperty("line.separator", "\n"));
                        }else{
                                System.err.println("存在路径----->进行追加" + resultLogDownloadPath);
                                        fsDataOutputStream = fs.append(path);
                                fsDataOutputStream
                                                .writeUTF(df.format(new Date()) + "\t" + data + System.getProperty("line.separator", "\n"));
                        }
                        if(null!=fsDataOutputStream)
                                 fsDataOutputStream.close();
                        } catch (Exception e) {
                        System.err.println("写日志失败!!!");
                        e.printStackTrace();
                }

        }
这样做是没有任何问题的,在公司的集圈上也都测试通过。
这样我有1个问题:
1):为什么连我的虚拟机(single data node),通过设置副本数;就可以实现追加的操作,但是在多个datanode的集圈上却是不适应的








已有(1)人评论

跳转到指定楼层
atsky123 发表于 2016-3-8 09:57:35
本帖最后由 atsky123 于 2016-3-8 09:59 编辑

应该跟具体节点没有关系。楼主可以看看他们之间的区别
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条