zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【Hadoop】Hadoop生态系列之HDFS常用Shell命令实践及Java API操作HDFS

2023-09-11 14:22:06 时间

上一篇:Hadoop生态系列之HDFS架构简述

HDFS实践

HDFS Shell 命令(常用)

打印hadoop类路径

[root@CentOS ~]# hdfs classpath

格式化NameNode

[root@CentOS ~]# hdfs namenode -format

dfsadmin命令

①可以使用-report -live或者-dead查看集群中dataNode节点状态

[root@CentOS ~]# hdfs dfsadmin -report  -live 

②使用-safemode enter|leave|get等操作安全模式

[root@CentOS ~]# hdfs dfsadmin -safemode get
Safe mode is OFF

③查看集群网络拓扑

[root@CentOS ~]# hdfs dfsadmin -printTopology
Rack: /default-rack
   192.168.73.130:50010 (CentOS)

更多请参考: HDFS常用命令

检查某个目录状态

[root@CentOS ~]# hdfs fsck /

DFS命令

[root@CentOS ~]# hdfs dfs -命令 选项 
# 或者老版本写法
[root@CentOS ~]# hadoop fs -命令 选项  
-appendToFile

anaconda-ks.cfg追加到aa.log

[root@CentOS ~]# hdfs dfs -appendToFile /root/anaconda-ks.cfg /aa.log
[root@CentOS ~]# hdfs dfs -appendToFile /root/anaconda-ks.cfg /aa.log
[root@CentOS ~]#
-cat

查看文件内容

[root@CentOS ~]# hdfs dfs -cat /aa.log
# 等价
[root@CentOS ~]# hdfs dfs -cat hdfs://CentOS:9000/aa.log
-chmod

修改文件权限

[root@CentOS ~]# hdfs dfs -chmod -R u+x  /aa.log
[root@CentOS ~]# hdfs dfs -chmod -R o+x  /aa.log
[root@CentOS ~]# hdfs dfs -chmod -R a+x  /aa.log
[root@CentOS ~]# hdfs dfs -chmod -R a-x  /aa.log
-copyFromLocal / -copyToLocal

copyFromLocal本地上传到HDFS;copyToLocal从HDFS上下载文件

[root@CentOS ~]# hdfs dfs -copyFromLocal jdk-8u191-linux-x64.rpm /
[root@CentOS ~]# rm -rf jdk-8u191-linux-x64.rpm
[root@CentOS ~]# hdfs dfs -copyToLocal /jdk-8u191-linux-x64.rpm /root/
[root@CentOS ~]# ls
anaconda-ks.cfg  hadoop-2.9.2.tar.gz  jdk-8u191-linux-x64.rpm
-mvToLocal/mvFromLocal

mvToLocal先下载文件,然后删除远程数据;
mvFromLocal:先上传,再删除本地

[root@CentOS ~]# hdfs dfs -moveFromLocal jdk-8u191-linux-x64.rpm /dir1
[root@CentOS ~]# ls
anaconda-ks.cfg  hadoop-2.9.2.tar.gz
[root@CentOS ~]# hdfs dfs -moveToLocal /dir1/jdk-8u191-linux-x64.rpm /root
moveToLocal: Option '-moveToLocal' is not implemented yet.
-put/get

文件上传/下载

[root@CentOS ~]# hdfs dfs -get /dir1/jdk-8u191-linux-x64.rpm /root
[root@CentOS ~]# ls
anaconda-ks.cfg  hadoop-2.9.2.tar.gz  jdk-8u191-linux-x64.rpm
[root@CentOS ~]# hdfs dfs -put hadoop-2.9.2.tar.gz /dir1

更多命令请使用

[root@CentOS ~]# hdfs dfs -help 命令

例如,想知道touchz该如何使用

[root@CentOS ~]# hdfs dfs -touchz /dir1/Helloworld.java
[root@CentOS ~]# hdfs dfs -ls /dir1/
Found 5 items
-rw-r--r--   1 root supergroup          0 2020-09-25 23:47 /dir1/Helloworld.java
drwxr-xr-x   - root supergroup          0 2020-09-25 23:07 /dir1/d1
drwxr-xr-x   - root supergroup          0 2020-09-25 23:09 /dir1/d2
-rw-r--r--   1 root supergroup  366447449 2020-09-25 23:43 /dir1/hadoop-2.9.2.tar.gz
-rw-r--r--   1 root supergroup  176154027 2020-09-25 23:41 /dir1/jdk-8u191-linux-x64.rpm

Java API操作HDFS(了解)

搭建开发步骤:

①IDEA创建一个Maven的项目(不选任何模板),在pom.xml文件中添加如下依赖

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.9.2</version>
</dependency>

②配置windows开发环境(必配

  • 需要将hadoop-2.9.2.tar.gz解压在window的指定目录下,比如这里我们解压在E:/目录下

  • 在Windows系统的环境变量中添加HADOOP_HOME环境变量,值为Hadoop的根目录在这里插入图片描述

  • hadoop-window-master.zip中的bin目录下的文件全部拷贝到%HADOOP_HOME%/bin目录下进行覆盖

  • 重启IDEA否则集成开发环境不识别配置HADOOP_HOME环境变量

③建议将Hadoop压缩包中的core-site.xmlhdfs-site.xml文件拷贝到项目的resources目录下

④在Windows上配置主机名和IP的映射关系(略)

⑤新建一个class文件,创建FileSystemConfiguration对象

public static FileSystem fs=null;
public static Configuration conf=null;
static {
    try {
        conf= new Configuration();
        conf.addResource("core-site.xml");
        conf.addResource("hdfs-site.xml");
        fs=FileSystem.get(conf);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

文件上传

Path src = new Path("file://xx路径");
Path dst = new Path("/");
fs.copyFromLocalFile(src,dst);
-
    InputStream in=new FileInputStream("file://xx路径");
Path dst = new Path("/xx路径");
OutputStream os=fs.create(dst);
IOUtils.copyBytes(in,os,1024,true);

文件下载

Path dst = new Path("file://xx路径");
Path src = new Path("/xx路径");
fs.copyToLocalFile(src,dst);
- 
Path dst = new Path("/xx路径");
InputStream in= fs.open(dst);
OutputStream os=new FileOutputStream("file://xx路径");
IOUtils.copyBytes(in,os,1024,true);    

删除文件

Path dst = new Path("/system");
fs.delete(dst,true);

回收站

Path dst = new Path("/aa.log");
Trash trash=new Trash(fs,conf);
trash.moveToTrash(dst);

所有文件

Path dst = new Path("/");
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(dst, true);
while (listFiles.hasNext()){
    LocatedFileStatus fileStatus = listFiles.next();
    System.out.println(fileStatus.getPath()+" "+fileStatus.isDirectory()+" "+fileStatus.getLen());
}

所有文件或文件夹

Path dst = new Path("/");
FileStatus[] fileStatuses = fs.listStatus(dst);
for (FileStatus fileStatus : fileStatuses) {
    System.out.println(fileStatus.getPath()+" "+fileStatus.isDirectory());
}

下一篇:Hadoop生态系列之MapReduce概述及MapReduce任务开发与发布