HDFS文件内容追加(Append)

 

2014年10月10日 11:58:29 阅读数:7940

 HDFS设计之处并不支持给文件追加内容,这样的设计是有其背景的(如果想了解更多关于HDFS的append的曲折实现,可以参考《File Appends in HDFS》:http://blog.cloudera.com/blog/2009/07/file-appends-in-hdfs/),但从HDFS2.x开始支持给文件追加内容,可以参见https://issues.apache.org/jira/browse/HADOOP-8230。可以再看看http://www.quora.com/HDFS/Is-HDFS-an-append-only-file-system-Then-how-do-people-modify-the-files-stored-on-HDFS。正如HADOOP-8230所述,只需要将hdfs-site.xml中的以下属性修改为true就行。

1<property>
2    <name>dfs.support.append</name>
3    <value>true</value>
4</property>

  目前如何在命令行里面给HDFS文件中追加内容我还没找到相应的方法。但是,我们可以通过Hadoop提供的API实现文件内容追加,如何实现?这里我写了一个简单的测试程序:

01package com.wyp;
02 
03import org.apache.hadoop.conf.Configuration;
04import org.apache.hadoop.fs.FileSystem;
05import org.apache.hadoop.fs.Path;
06import org.apache.hadoop.io.IOUtils;
07 
08import java.io.*;
09import java.net.URI;
10 
11/**
12 * blog: http://www.iteblog.com/
13 * Date: 14-1-2
14 * Time: 下午6:09
15 */
16public class  AppendContent {
17    public static  void  main(String[] args) {
18        String hdfs_path = "hdfs://mycluster/home/wyp/wyp.txt";//文件路径
19        Configuration conf = new Configuration();
20        conf.setBoolean("dfs.support.append"true);
21 
22        String inpath = "/home/wyp/append.txt";
23        FileSystem fs = null;
24        try {
25            fs = FileSystem.get(URI.create(hdfs_path), conf);
26            //要追加的文件流,inpath为文件
27            InputStream in = new
28                  BufferedInputStream(new FileInputStream(inpath));
29            OutputStream out = fs.append(new Path(hdfs_path));
30            IOUtils.copyBytes(in, out, 4096true);
31        catch (IOException e) {
32            e.printStackTrace();
33        }
34    }
35}

将上面的代码打包成jar(这里我取名为hdfs.jar)文件,然后上传到机器中,比如我上传到我的home目录,在程序运行前,我们来看看HDFS中wyp.txt文件中的内容有什么

1[wyp@l-datalogm1.data.cn1 ~]$ /home/q/hadoop-2.2.0/bin/hadoop fs   \
2                                           -cat /home/wyp/wyp.txt
3123456
4[wyp@l-datalogm1.data.cn1 ~]$

好,我们再来看看/home/wyp/append.txt文件中的内容:

1[wyp@l-datalogm1.data.cn1 ~]$ vim append.txt
2wyp  append test

看完代码中所涉及到的两个文件之后,我们再运行hdfs.jar

1[wyp@l-datalogm1.data.cn1 ~]$ /home/q/hadoop-2.2.0/bin/hadoop jar \
2                                       hdfs.jar com.wyp.AppendContent

运行完之后,看看wyp.txt内容

1[wyp@l-datalogm1.data.cn1 ~]$ /home/q/hadoop-2.2.0/bin/hadoop fs \
2                                         -cat /home/wyp/wyp.txt
3123456
4wyp  append test

好了,wyp.txt文件已经追加了append.txt文件中的内容了。

原文出自:http://www.iteblog.com/archives/881

 

========================================================================

hadoop 测试例子:

========================================================================

 

public static void main(String[] args) {
// TODO Auto-generated method stub

String newFilePath = args[0];
String totalFilePath = args[1];

System.out.println("new file path:"+newFilePath);
System.out.println("total file path:"+totalFilePath);


Configuration conf = new Configuration();
conf.setBoolean("dfs.support.append", true);

FileSystem fs_new = null;
FileSystem fs_total = null;

InputStream in = null;
OutputStream out = null;

try {
fs_new = FileSystem.get(URI.create(newFilePath), conf);
fs_total = FileSystem.get(URI.create(totalFilePath), conf);

in = new BufferedInputStream(fs_new.open(new Path(newFilePath)));
out = fs_total.append(new Path(totalFilePath));

IOUtils.copyBytes(in, out, conf, true);


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}

=======================================================================

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值