三、HDFS操作文件


hadoop包:链接:https://pan.baidu.com/s/1bPlkKnYLXsfOjMtcK1Nq8g 密码:nzqg
demo地址:https://github.com/chenjy512/bigdata_study/tree/master/hdfs-clientAPI

一、HDFS操作文件方式

HDFS操作文件方式有两种:
1.shell命令操作服务器本地文件至HDFS中
2.客户端api方式自己编写代码操作文件

二、HDFS的shell操作

1.基本命令
bin/hadoop fs 具体命令 OR bin/hdfs dfs 具体命令

dfs是fs的实现类。

2.查旬可用命令,如下会列出可以使用的所有命令及参数

bin/hadoop fs

3.常用命令,也就是操作文件可用命令

(0)启动Hadoop集群
方式一
[root@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
[root@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
方式二yarn所在节点使用
[root@hadoop103 hadoop-2.7.2]$ sbin/start-all.sh

(1)-help:输出这个命令参数
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -help rm

(2)-ls: 显示目录信息
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -ls /
递归显示所有文件及文件夹目录信息
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -lsr /

(3)-mkdir:在HDFS上创建目录
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir -p /sanguo/shuguo

(4)-moveFromLocal:从本地剪切粘贴到HDFS
[root@hadoop102 hadoop-2.7.2]$ touch kongming.txt 
[root@hadoop102 hadoop-2.7.2]$ hadoop fs  -moveFromLocal  ./kongming.txt  /sanguo/shuguo

(5)-appendToFile:追加一个文件到已经存在的文件末尾
[root@hadoop102 hadoop-2.7.2]$ touch liubei.txt
[root@hadoop102 hadoop-2.7.2]$ vi liubei.txt
san gu mao lu
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt

(6)-cat:显示文件内容
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -cat /sanguo/shuguo/kongming.txt

(7)-chgrp 、-chmod、-chown:Linux文件系统中的用法一样,修改文件所属权限
[root@hadoop102 hadoop-2.7.2]$ hadoop fs  -chmod  666  /sanguo/shuguo/kongming.txt
[root@hadoop102 hadoop-2.7.2]$ hadoop fs  -chown  root:root   /sanguo/shuguo/kongming.txt

(8)-copyFromLocal:从本地文件系统中拷贝文件到HDFS路径去
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -copyFromLocal README.txt /

(9)-copyToLocal:从HDFS拷贝到本地
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./

(10)-cp :从HDFS的一个路径拷贝到HDFS的另一个路径
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt

(11)-mv:在HDFS目录中移动文件
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/

(12)-get:等同于copyToLocal,就是从HDFS下载文件到本地
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./

(13)-getmerge:合并下载多个文件,比如HDFS的目录 /user/root/test下有多个文件:log.1, log.2,log.3,...
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -getmerge /user/root/test/* ./zaiyiqi.txt

(14)-put:等同于copyFromLocal
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -put ./zaiyiqi.txt /user/root/test/

(15)-tail:显示一个文件的末尾
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -tail /sanguo/shuguo/kongming.txt

(16)-rm:删除文件或文件夹
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -rm /user/root/test/jinlian2.txt

(17)-rmdir:删除空目录
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir /test
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -rmdir /test

(18)-du统计文件夹的大小信息
[root@hadoop102 hadoop-2.7.2]$ hadoop fs -du -s -h /user/root/test
2.7 K  /user/root/test

[root@hadoop102 hadoop-2.7.2]$ hadoop fs -du  -h /user/root/test
1.3 K  /user/root/test/README.txt
15     /user/root/test/jinlian.txt
1.4 K  /user/root/test/zaiyiqi.txt

三、客户端操作

1.本地环境配置

跟配置java_home 一个道理,mack与linux环境使用的压缩包是一样的。

2.工程搭建
	2.1创建hdfs-clientAPI maven工程
	
	2.2导入依赖
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>

3. 操作api

事前先启动hadoop集群


3.1 文件夹创建

    @Before
    public void before(){
        //设置访问用户
        Properties pro = System.getProperties();
        pro.setProperty("HADOOP_USER_NAME","root");
        // 权限不足引起,设置指定用户即可 https://www.cnblogs.com/413xiaol/p/9949936.html
        // org.apache.hadoop.security.AccessControlException: Permission denied: user=chenjunying, access=WRITE, inode="/test":root:supergroup:drwxr-xr-x
    }

    //1. 创建文件夹
    @Test
    public void testMkdir() throws URISyntaxException, IOException {


        //创建配置
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), conf);
        //行动
        boolean b = fs.mkdirs(new Path("/test2"));
        if(b)
            System.out.println("succes");
        else
            System.out.println("error");

        fs.close();
         }

3.2 文件上传与设置优先级测试

//1.先将hdfs-site.xml复制一份放在工程根目录下
  @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        configuration.set("dfs.replication", "2");
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");
        // 2 上传文件
        fs.copyFromLocalFile(new Path("/Users/chenjunying/Downloads/1.txt"), new Path("/test2/"));
        // 3 关闭资源
        fs.close();
        System.out.println("over");
    }

参数优先级排序: (1)客户端代码中设置的值 >(2)classpath下的用户自定义配置文件 >(3)然后是服务器的默认配置

3.3 文件下载
	@Test
    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 执行下载操作
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(false, new Path("/test2/1.txt"), new Path("/Users/chenjunying/Downloads/1.txt"), true);
        // 3 关闭资源
        fs.close();
    }

3.4 文件夹及文件移除
    //文件夹删除--包含文件夹下的文件整个移除
    @Test
    public void testDelete() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 执行删除
        fs.delete(new Path("/test/"), true);

        // 3 关闭资源
        fs.close();
    }


3.5 文件名修改
    //文件名修改
    @Test
    public void testRename() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 修改文件名称
        fs.rename(new Path("/test2/1.txt"), new Path("/test2/2.txt"));

        // 3 关闭资源
        fs.close();
    }


3.6 查看文件名称、权限、长度、块信息
//查看文件名称、权限、长度、块信息
    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
        // 1获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 获取文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while(listFiles.hasNext()){
            LocatedFileStatus status = listFiles.next();

            // 输出详情
            // 文件名称
            System.out.println(status.getPath().getName());
            // 长度
            System.out.println(status.getLen());
            // 权限
            System.out.println(status.getPermission());
            // z组
            System.out.println(status.getGroup());

            // 获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                // 获取块存储的主机节点
                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("----------------数据分割线-----------");
        }
    }


3.7 文件与文件夹判断
    //文件与文件夹判断

    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件配置信息
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 判断是文件还是文件夹
        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : listStatus) {

            // 如果是文件
            if (fileStatus.isFile()) {
                System.out.println("f:"+fileStatus.getPath().getName());
            }else {
                System.out.println("d:"+fileStatus.getPath().getName());
            }
        }

        // 3 关闭资源
        fs.close();
    }




3.8 IO流操作文件上传
    //文件上传
    @Test
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 创建输入流
        FileInputStream fis = new FileInputStream(new File("/Users/chenjunying/Downloads/1.txt"));

        // 3 获取输出流
        FSDataOutputStream fos = fs.create(new Path("/test2/2.txt")); //需要指定文件名称及路径

        // 4 流对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }


3.9 IO流操作文件下载
    // 文件下载
    @Test
    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 获取输人流
        FSDataInputStream fis = fs.open(new Path("/test2/2.txt"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("/Users/chenjunying/Downloads/3.txt"));

        // 4 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
        fs.close();
    }


3.10 文件定位下载----可将大文件分两步下载
    //下载第一块
    @Test
    public void readFileSeek1() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/test2/hadoop-2.7.2.tar.gz"));

        // 3 创建输出流
        FileOutputStream fos = new FileOutputStream(new File("/Users/chenjunying/Downloads/hadoop-2.7.2.tar.gz.part1"));

        // 4 流的拷贝
        byte[] buf = new byte[1024];

        for(int i =0 ; i < 1024 * 128; i++){
            fis.read(buf);
            fos.write(buf);
        }

        // 5关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }

    //下载第二块数据
    @Test
    public void readFileSeek2() throws IOException, InterruptedException, URISyntaxException{
        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "root");

        // 2 打开输入流
        FSDataInputStream fis = fs.open(new Path("/test2/hadoop-2.7.2.tar.gz"));

        // 3 定位输入数据位置
        fis.seek(1024*1024*128);

        // 4 创建输出流
        FileOutputStream fos = new FileOutputStream(new File("/Users/chenjunying/Downloads/hadoop-2.7.2.tar.gz.part2"));

        // 5 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 6 关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }

    //合并文件
    /**
     * 1。在window命令窗口中执行
     * type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1
     * 2。linux下分割/合并文件 https://blog.csdn.net/wq3028/article/details/80775626
     * 3。mack下:https://blog.csdn.net/qq_45036710/article/details/100163689
     */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值