HDFS的一些操作

有三种操作方式

  1. webConsole
  2. 命令行
  3. java API

常用的操作命令如下:
管理命令
hdfs dfsadmin

-safemode enter 手动进入安全模式
-safemode leave 手动退出安全模式
-safemode get 查看当前的模式状态
-safemode wait
-report 打印报告

hdfs balancer 手动平衡数据块在各个DataNode之间的分布

普通命令
hdfs dfs

-mkdir 创建目录

上传

-put
-copyFromLocal 拷贝粘贴
-moveFromLocal 剪切粘贴

下载

-get
-copyToLocal

删除(需要注意回收站开启的情况,回收站默认是关闭的)

-rm
-rmr
-rm -R

合并下载

-getmerge
hdfs dfs -getmerge /students /root/a.txt
-cp
-mv
-count 统计HDFS目录下目录个数、文件个数、文件总的大小
[root@bigdata111 ~]# hdfs dfs -count /students
                       1            2                 29 /students
-du
hdfs dfs -du /students
19  /students/student01.txt
10  /students/student02.txt
-cat 查看文本
-text
javaApi
初始化
conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://192.168.23.114:9000");
        fs = FileSystem.get(conf);
        
        
创建目录
//指定当前的Hadoop的用户
//        System.setProperty("HADOOP_USER_NAME","root");   //--------------------第一种方式:设置环境变量  HADOOP_USER_NAME = root
        fs.mkdirs(new Path("/input/workSpace"));
        fs.close();
        

上传
Path destPath = new Path("/input");
        Path srcPath = new Path("D:\\pratise_tmp_space\\hadoop-2.7.1.tar.gz");
        fs.copyFromLocalFile(srcPath,destPath);
        fs.close();
        
        
Path destPath = new Path("/input/tmpSpace/mm.jpg");
        OutputStream out = fs.create(destPath);
        InputStream in = new FileInputStream("D:\\pratise_tmp_space\\mm.jpeg");
        IOUtils.copyBytes(in,out,1024);
        
        
下载
OutputStream out = new FileOutputStream("D:\\pratise_tmp_space\\a.txt");
        InputStream in = fs.open(new Path("/input/data.txt"));
        IOUtils.copyBytes(in,out,1024);
        
Path destPath = new Path("D:\\pratise_tmp_space\\a.txt");
        Path srcPath = new Path("/input/data.txt");
        fs.copyToLocalFile(srcPath,destPath);
        fs.close();
        
        
获取DataNode的信息
 //创建一个HDFS客户端
        DistributedFileSystem dfs = (DistributedFileSystem) fs;
        //获取数据节点的信息: Stats ---> 统计信息
        DatanodeInfo[] dataNodeStats = dfs.getDataNodeStats();
        for (DatanodeInfo dataNodeInfo:dataNodeStats) {
            System.out.println(dataNodeInfo.getHostName()+"-----"+dataNodeInfo.getName());
        }
        dfs.close();
        
        
        
获取数据块的信息
//获取文件的status信息
        FileStatus fileStatus = fs.getFileStatus(new Path("/input/hadoop-2.7.1.tar.gz"));
        //获取文件的数据块信息(数组)
        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
        /*
         * 伪分布的环境,数据块的冗余度是:1
         */
        for (BlockLocation location:fileBlockLocations) {
            System.out.println(Arrays.toString(location.getHosts())+"-----"+Arrays.toString(location.getNames()));
        }
        fs.close();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值