hadoop-HDFS操作-shell端-java端需要的依赖-简单案例-修改idea格式化代码快捷键

1 shell端

就是在linux虚拟机上操作HDFS
常用命令如下:

hdfs -help--->查看帮助
不记得的查看帮助就可以了

2 java端

在IDEA中编写代码操作HDFS
依赖

<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-common</artifactId>
            <version>3.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
            <version>3.2.1</version>
        </dependency>
    </dependencies>

2.1 上传数据

/**
         * 1. 获取配置对象
         * 2. 获取文件系统对象
         * 3. 使用对象调用指定方法
         * 4. 关闭文件系统对象
         */
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        //上传多个路径下的文件
        fs.copyFromLocalFile(false,false,new Path[]{new Path("D://user.txt"),new Path("J://word.txt")},new Path("/hdfsjava"));
        //上传单个文件
        //fs.copyFromLocalFile(new Path(""),new Path(""));
        fs.close();

在这里插入图片描述

2.2 下载数据

		Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        //下载文件/文件夹
        fs.copyToLocalFile(new Path("/hdfsjava"),new Path("H://"));
        fs.close();

2.3 mkdir

Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        //也可以在创建文件夹的时候添加权限
        fs.mkdirs(new Path("/newpath"));
        fs.close();

2.4 rename

Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        fs.rename(new Path("/newpath"),new Path("/oldpath"));
        fs.close();

2.5 listFiles和listStatus

public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        /**
         * 参数一      路径
         * 参数二      是否递归获取--->递归就和linux命令中的ll一样 , 可以查看目录下的所有内容
         * 返回一个迭代器--->迭代器中的是目录下的文件和文件夹
         */
        RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/hdfsjava"), false);
        //查看目录下所有文件和文件夹的状态--->也可以通过只获取部分文件/文件夹的部分属性
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus);
        }
        System.out.println("-------------------------------");
        while (iterator.hasNext()){
            LocatedFileStatus file = iterator.next();
            if(file.isFile()){
                //如果是文件---获取文件所在路径
                Path filePath = file.getPath();
                //获取文件名
                String fileName = filePath.getName();
                //查看数据块的大小
                long blockSize = file.getBlockSize();
                //查看文件的权限
                FsPermission permission = file.getPermission();
                //获取数据所在的块
                BlockLocation[] blockLocations = file.getBlockLocations();
                for (BlockLocation blockLocation : blockLocations) {
                    //偏移量
                    long offset = blockLocation.getOffset();
                    //长度
                    long length = blockLocation.getLength();
                    //获取数据块存储的所有主机名
                    String[] hosts = blockLocation.getHosts();
                    //打印文件名
                    System.out.println(fileName);
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                    System.out.println("------------------");
                }
            }
        }
    }

在这里插入图片描述

2.6 读写数据

		Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        //读取数据
        FSDataInputStream open = fs.open(new Path(""));
        //第一行数据 a b c
        //跳过a
        //open.seek(1);
        //跳过一个,读取第二个
        //int i = open.read();
        //包在缓冲字符流中逐行读取
        BufferedReader reader = new BufferedReader(new InputStreamReader(open));
        String line = null;
        while ((line = reader.readLine()) != null){
            System.out.println(line);
        }
        open.close();
        reader.close();
        fs.close();


在这里插入图片描述
在这里插入图片描述

Configuration conf = new Configuration();
        FileSystem fs = FileSystem.newInstance(new URI("hdfs://linux01:8020"), conf, "root");
        //hdfs 不支持并发写---随机写---随机修改
        //在文件末尾追加内容
        FSDataOutputStream append = fs.append(new Path("/hdfsjava/word.txt"));
        append.writeUTF("hello");
        //覆盖文件内容
        FSDataOutputStream fos = fs.create(new Path("/hdfsjava/user.txt"), true);
        fos.writeUTF("world");
        fos.close();
        append.close();
        fs.close();

在这里插入图片描述
在这里插入图片描述

修改idea格式化代码快捷键

File—>Settings—>搜索reformat—>ReFormat Code—>右键Add Keyboard Shortcut

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值