HDFS的访问

1 通过Shell

官网api传送门
https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/FileSystemShell.html

1.1 创建目录

hdfs dfs -mkdir 文件夹名字

hdfs dfs -mkdir /installpkgs

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

1.2 上传文件到hdfs

hdfs dfs -put 文件名 新文件名

hdfs dfs -put jdk-7u79-linux-x64.rpm /installpkgs/jdk-7u79-linux-x64.rpm

在这里插入图片描述
在这里插入图片描述
或者使用
hdfs dfs -copyFromLocal 本地文件 远程文件

hdfs dfs -copyFromLocal /opt/software/hadoop-2.7.0.tar.gz /installpkgs/hadoop-2.7.0.tar.gz

在这里插入图片描述
put跟copyFromLocal区别
官网解释
在这里插入图片描述

1.3 列出HDFS上的文件

hdfs dfs -ls 目录名

hdfs dfs -ls /
hdfs dfs -ls /installpkgs 

在这里插入图片描述

1.4 查看HDFS下某个文件的内容

hdfs dfs -cat 文件名

hdfs dfs -cat /docs/test.txt

创建个测试文件
在这里插入图片描述

1.5 将hdfs中的文件复制到本地系统

hdfs dfs -get 文件名 新文件名

hdfs dfs -get /docs/test.txt ~/test.txt

在这里插入图片描述
或者使用
hdfs dfs -copyToLocal 远程文件 本地文件

hdfs dfs -copyToLocal /docs/test.txt ~/test2.txt

在这里插入图片描述

与get的区别
官网解释
在这里插入图片描述

1.6 删除hdfs下的文档

hdfs dfs -rmr 文件

hdfs dfs -rmr 

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

1.7 格式化hdfs

hdfs namenode -format

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

1.8 关闭HDFS

stop-dfs.sh

在这里插入图片描述

1.9 启动HDFS

start-dfs.sh

在这里插入图片描述
确认启动成功
在这里插入图片描述

2 通过Java api操作

官网api传送门
https://hadoop.apache.org/docs/stable/api/index.html

前置工作
a) 创建一个Java项目,用maven来管理

导入依赖jar包

   <properties>
        <hadoop.version>2.7.0</hadoop.version>
    </properties>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

没有用maven的自己得去手动加jar包,不推荐
在这里插入图片描述
b) 拷贝配置文件core-site.xml,hdfs-site.xml到resource目录下
在这里插入图片描述

2.1 创建目录

    @Test
    public void testMkdir() throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path("/app");
        fs.mkdirs(path);
    }

在这里插入图片描述

2.1.1 异常报错UnknownHostException: masternode

在这里插入图片描述
解决
把master换成具体的ip地址 或者修改主机的host文件
在这里插入图片描述

2.1.2 异常报错权限不够

在这里插入图片描述
解决
修改目录的访问权限

hdfs dfs -chmod 777 /

在这里插入图片描述
注:生产环境不要把根目录权限搞这么大
或者代码中加入

System.setProperty("HADOOP_USER_NAME", "root");

修改后测试通过
在这里插入图片描述
在这里插入图片描述

2.2 上传文件到hdfs

    @Test
    public void testCopyFromLocal() throws IOException {
	    Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        FileSystem fs = FileSystem.get(conf);
        Path srcPath = new Path("c://testCopyFromLocal.txt");
        Path destPath = new Path("/docs/testCopyFromLocal.txt");
        fs.copyFromLocalFile(srcPath, destPath);
    }

创建一个文件准备把它上传到hdfs
在这里插入图片描述
在这里插入图片描述
查看结果
成功上传到hdfs上
在这里插入图片描述

2.3 列出HDFS上的文件

    @Test
    public void testLs() throws IOException {
    	Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path("/");
        FileStatus[] fileStatuses = fs.listStatus(path);
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath());
        }
    }

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

2.3.1 拓展递归显示文件

    @Test
    public void testListFiles() throws IOException {
        System.setProperty("HADOOP_USER_NAME", "root");
        Path path = new Path("/");
        recursiveListFiles(path);
    }

    public void recursiveListFiles(Path path)throws IOException {
        FileSystem fs = FileSystem.get(conf);
        FileStatus[] fileStatuses = fs.listStatus(path);
        for (FileStatus fileStatus : fileStatuses) {
            if (fileStatus.isDirectory()) {
                System.out.println(fileStatus.getPath());
                recursiveListFiles(fileStatus.getPath());
            } else {
                System.out.println(fileStatus.getPath());
            }
        }
    }

在这里插入图片描述

2.4 查看HDFS下某个文件的内容

    @Test
    public void testCat() throws IOException {
    	Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        Path path = new Path("/docs/test.txt");
        FileSystem fs = FileSystem.get(conf);
        FSDataInputStream in = fs.open(path);
        int i = 0;
        while ((i = in.read()) != -1) {
            System.out.print(Character.toChars(i));
        }
    }

在这里插入图片描述

2.5 将hdfs中的文件复制到本地系统

    @Test
    public void testCopyToLocal() throws IOException {
    	Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        Path src = new Path("/docs/test.txt");
        Path dst = new Path("f://test.txt");
        FileSystem fs = FileSystem.get(conf);
        fs.copyToLocalFile(false,src,dst,true);
    }

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

2.6 删除hdfs下的文档

    @Test
    public void testRm() throws IOException {
        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        Path path = new Path("/installpkgs/test.txt");
        FileSystem fs = FileSystem.get(conf);
        boolean isDeleted = fs.delete(path, true);
        if (isDeleted) {
            System.out.println("删除成功");
        }
    }

准备一个替死鬼
在这里插入图片描述
在这里插入图片描述
送他去见佛祖
在这里插入图片描述
删除成功
在这里插入图片描述

2.7 格式化hdfs

暂时没找到,老老实实用shell吧

hdfs namenode -format

2.8 关闭hdfs

暂时没找到,老老实实用shell吧

stop-dfs.sh

2.9 启动hdfs

暂时没找到,老老实实用shell吧

start-dfs.sh
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值