HDFS编程实践(Hadoop3.1.3)

一、利用Shell命令与HDFS进行交互

1.目录操作

在HDFS中为hadoop用户创建一个用户目录

cd /usr/local/hadoop
./bin/hdfs dfs -mkdir -p /user/hadoop

显示HDFS中与当前用户hadoop对应的用户目录下的内容:

 ./bin/hdfs dfs -ls .

创建一个input目录:

 ./bin/hdfs dfs -mkdir input

可以使用rm命令删除一个目录

./bin/hdfs dfs -rm -r input

上面命令中,“-r”参数表示如果删除“input”目录及其子目录下的所有内容,如果要删除的一个目录包含了子目录,则必须使用“-r”参数,否则会执行失败。

2.文件操作

使用如下命令把本地文件系统的“/home/hadoop/myLocalFile.txt”上传到HDFS中的当前用户目录的input目录下,也就是上传到HDFS的“/user/hadoop/input/”目录下:

./bin/hdfs dfs -put /home/hadoop/myLocalFile.txt  input

Shell 命令

可以使用ls命令查看一下文件是否成功上传到HDFS中,具体如下:

./bin/hdfs dfs -ls input

下面使用如下命令查看HDFS中的myLocalFile.txt这个文件的内容:

./bin/hdfs dfs -cat input/myLocalFile.txt

下面把HDFS中的myLocalFile.txt文件下载到本地文件系统中的“/home/hadoop/下载/”这个目录下,命令如下:

./bin/hdfs dfs -get input/myLocalFile.txt  /home/hadoop/下载

最后,了解一下如何把文件从HDFS中的一个目录拷贝到HDFS中的另外一个目录。比如,如果要把HDFS的“/user/hadoop/input/myLocalFile.txt”文件,拷贝到HDFS的另外一个目录“/input”中(注意,这个input目录位于HDFS根目录下),可以使用如下命令:

./bin/hdfs dfs -cp input/myLocalFile.txt  /input

二、利用Web界面管理HDFS

在这里插入图片描述

三、利用Java API与HDFS进行交互

1. 在IDEA中创建项目

使用IDEA在/home/hadoop下创建工作区workspace

在这里插入图片描述

2. 为项目添加需要用到的JAR包

(1)“/usr/local/hadoop/share/hadoop/common”目录下的所有JAR包,包括hadoop-common-3.1.3.jar、hadoop-common-3.1.3-tests.jar、haoop-nfs-3.1.3.jar和haoop-kms-3.1.3.jar,注意,不包括目录jdiff、lib、sources和webapps;
(2)“/usr/local/hadoop/share/hadoop/common/lib”目录下的所有JAR包;
(3)“/usr/local/hadoop/share/hadoop/hdfs”目录下的所有JAR包,注意,不包括目录jdiff、lib、sources和webapps;
(4)“/usr/local/hadoop/share/hadoop/hdfs/lib”目录下的所有JAR包。

在这里插入图片描述

3. 编写Java应用程序

import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

/**
 * 过滤掉文件名满足特定条件的文件
 */
class MyPathFilter implements PathFilter {
    String reg = null;
    MyPathFilter(String reg) {
        this.reg = reg;
    }
    public boolean accept(Path path) {
        if (!(path.toString().matches(reg)))
            return true;
        return false;
    }
}
/***
 * 利用FSDataOutputStream和FSDataInputStream合并HDFS中的文件
 */
public class MergeFile {
    Path inputPath = null; //待合并的文件所在的目录的路径
    Path outputPath = null; //输出文件的路径
    public MergeFile(String input, String output) {
        this.inputPath = new Path(input);
        this.outputPath = new Path(output);
    }
    public void doMerge() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://localhost:9000");
        conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
        FileSystem fsSource = FileSystem.get(URI.create(inputPath.toString()), conf);
        FileSystem fsDst = FileSystem.get(URI.create(outputPath.toString()), conf);
        //下面过滤掉输入目录中后缀为.abc的文件
        FileStatus[] sourceStatus = fsSource.listStatus(inputPath,
                new MyPathFilter(".*\\.abc"));
        FSDataOutputStream fsdos = fsDst.create(outputPath);
        PrintStream ps = new PrintStream(System.out);
        //下面分别读取过滤之后的每个文件的内容,并输出到同一个文件中
        for (FileStatus sta : sourceStatus) {
            //下面打印后缀不为.abc的文件的路径、文件大小
            System.out.print("\n"+"路径:" + sta.getPath() + "    文件大小:" + sta.getLen()
                    + "   权限:" + sta.getPermission() + "   内容:");
            FSDataInputStream fsdis = fsSource.open(sta.getPath());
            byte[] data = new byte[1024];
            int read = -1;

            while ((read = fsdis.read(data)) > 0) {
                ps.write(data, 0, read);
                fsdos.write(data, 0, read);
            }
            fsdis.close();
        }
        ps.close();
        fsdos.close();
    }
    public static void main(String[] args) throws IOException {
        MergeFile merge = new MergeFile(
                "hdfs://localhost:9000/user/hadoop/",
                "hdfs://localhost:9000/user/hadoop/merge.txt");
        merge.doMerge();
    }
}

4. 编译运行程序

首先要确保HDFS的“/user/hadoop”目录下已经存在file1.txt、file2.txt、file3.txt、file4.abc和file5.abc,每个文件里面有内容。这里,假设文件内容如下:
file1.txt的内容是: this is file1.txt
file2.txt的内容是: this is file2.txt
file3.txt的内容是: this is file3.txt
file4.abc的内容是: this is file4.abc
file5.abc的内容是: this is file5.abc

开始运行程序。程序运行结束后,会在底部的面板中显示运行结果信息(如下图所示)。同时,面板中还会显示一些似“log4j:WARN…”的警告信息,可以不用理会。

在这里插入图片描述

如果程序运行成功,这时,可以到HDFS中查看生成的merge.txt文件,比如,可以在Linux终端中执行如下命令:

cd /usr/local/hadoop
./bin/hdfs dfs -ls /user/hadoop
./bin/hdfs dfs -cat /user/hadoop/merge.txt

5. 应用程序的部署

在IDEA-文件-项目结构-工件中添加工件

在这里插入图片描述

选择主类并点击确定

在这里插入图片描述

然后点击构建-构建工件,生成jar包,将jar包移动至usr/local/hadoop/myapp

由于之前已经运行过一次程序,已经生成了merge.txt,因此,需要首先执行如下命令删除该文件:

cd /usr/local/hadoop./bin/hdfs dfs -rm /user/hadoop/merge.txt

现在,就可以在Linux系统中,使用hadoop jar命令运行程序,命令如下:

cd /usr/local/hadoop./bin/hadoop jar ./myapp/HDFSExample.jar

上面程序执行结束以后,可以到HDFS中查看生成的merge.txt文件,比如,可以在Linux终端中执行如下命令:

cd /usr/local/hadoop./bin/hdfs dfs -ls /user/hadoop./bin/hdfs dfs -cat /user/hadoop/merge.txt

可以看到如下结果:

this is file1.txt
this is file2.txt
this is file3.txt
  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值