远程HDFS文件的操作

  因为手头项目涉及到远程HDFS文件的操作,所以打算学习一下相关操作。目前,网络上有很多操作HDFS文件的代码,但是它们基本上都没有描述清楚Configuration相关问题。经过摸索,终于实现远程HDFS文件读写的完整流程,在此希望各位看官交流学习。

  HDFS文件操作依赖的jar包是通过maven下载的,其相关依赖如下:

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>${cdh.hadoop.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>${cdh.hadoop.version}</version>
    </dependency>
</dependencies>

以上依赖中version处根据实际情况填写。

HDFS文件的操作用到类有以下3个:
org.apache.hadoop.conf.Configuration;
org.apache.hadoop.fs.FileSystem;
org.apache.hadoop.fs.Path;

Configuration

  hadoop既没有使用java.util.Properties管理配置文件,也没有使用Apache Jakarta Commons Configuration管理配置文件,而是使用其独有的配置文件管理系统org.apache.hadoop.conf.Configuration,该配置管理系统提供自已的API。博文http://blog.csdn.net/hadoop_/article/details/9365075对Configuration描述较为清晰,此处不再描述。另外,操作HDFS文件需要用到hadoop目录中的core-site.xml和hdfs-site.xml文件。
 

代码片段

  • 配置:

    Configuration conf = new Configuration();

    FileSystem hdfs = FileSystem.get(URI.create(“hdfs://nameservice/path”), conf, “username”);

  • HDFS文件读写操作:

    public void writerContentToHdfs(String hdfsFile, String content) {
        OutputStream os = null;
        OutputStreamWriter osw = null;
        try {
            Path file = new Path(hdfsFile);
            if (!this.hdfs.exists(file)) { os = this.hdfs.create(file); }
            else { os = this.hdfs.append(file); }
            osw = new OutputStreamWriter(os);
            osw.write(content);
            osw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null) { os.close(); }
                if (osw != null) { osw.close(); }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
    public String readContentFromHdfs(String hdfsFile) {
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            Path file = new Path(hdfsFile);
            if (!this.hdfs.exists(file)) { return null; }
    
            is = this.hdfs.open(file);
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            StringBuffer sb = new StringBuffer();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (is != null) { is.close(); }
                if (isr != null) { isr.close(); }
                if (br != null) { br.close(); }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        return null;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值