Hadoop 读写IO 程序解析

本文所涉及的Hadoop的文件系统主要是HDFS,通过JavaAPI的方式和HDFS进行交互。

创建一个空的Maven工程

通过Idea创建一个空的Maven工程,创建完毕以后,由于pom中未定义任何的组件,没有编写相关的代码,所以工程没有实质的内容。 读取HDFS需要使用org.apache.hadoop的相关jar包,具体使用那些包和程序代码相关,但是需要留意的是下载的包的版本,最好和需要访问的hdfs不要间隔太远,否则会提示版本不兼容的问题。
下面为项目中使用的pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lst</groupId>
    <artifactId>hadoop</artifactId>
    <version>1.0</version>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

增加完毕pom文件后,Idea将自动下载对应的jar包,然后在src->java中增加对应的IO读写类。
既然加载了那么多包,当然是要使用,因此在文件的开始,先将这些文件import 进来:

package com.lst.hdfs;

import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;

编写读写方法

写方法

public void WriteFile(String hdfs) throws IOException {
        /*
        *
        * */
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
        FSDataOutputStream hdfsOutStream = fs.create(new Path(hdfs));
        hdfsOutStream.writeChars("hello");
        hdfsOutStream.close();
        fs.close();
    }

上述代码的第一句:Configuration conf = new Configuration(); 主要为加载hdfs的配置文件,在本文的代码中,使用的是读取默认位置(/etc/hadoop/)的配置文件(core-site.xml,hdfs-site.xml)。
第2句:FileSystem fs = FileSystem.get(URI.create(hdfs),conf); FileSystem 是一个通用的文件系统API,这里主要是获取需要使用的文件系统的实例。获取了文件系统实例之后,就可以进行第三步,调用open函数啊来获取文件的输入流了。这里的hdfsOutStream就是返回的一个写入数据的输出流对象,这里的create方法可以自动创建当前不存在的文件。
获取到了写入流以后就很简单了,直接写入数据即可。写完毕以后关输出流。

public void ReadFile(String hdfs) throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(hdfs),conf);
        FSDataInputStream hdfsInStream = fs.open(new Path(hdfs));

        byte[] ioBuffer = new byte[1024];
        int readLen = hdfsInStream.read(ioBuffer);
        while(readLen!=-1)
        {
            System.out.write(ioBuffer, 0, readLen);
            readLen = hdfsInStream.read(ioBuffer);
        }
        hdfsInStream.close();
        fs.

与写文件类似,这里唯一的区别就是wirte变成了read,然后将读到的内容显示出来。
最后添加一个main函数:

public static void main(String[] args) throws IOException {
        String hdfs = "hdfs://192.168.56.112:9000/test/hello.txt";
        hdfs_rw t = new hdfs_rw();
        t.WriteFile(hdfs);
        t.ReadFile(hdfs);
    }

转载于:https://www.cnblogs.com/angellst/p/7536254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值