• HDFS的客户端操作


    命令行操作:

    -help             

    功能:输出这个命令参数手册

    -ls                  

    功能:显示目录信息

    示例: hadoop fs -ls hdfs://hadoop-server01:9000/

    备注:这些参数中,所有的hdfs路径都可以简写

    -->hadoop fs -ls /   等同于上一条命令的效果

    -mkdir              

    功能:在hdfs上创建目录

    示例:hadoop fs  -mkdir  -p  /aaa/bbb/cc/dd

    -moveFromLocal            

    功能:从本地剪切粘贴到hdfs

    示例:hadoop  fs  - moveFromLocal  /home/hadoop/a.txt  /aaa/bbb/cc/dd

    -moveToLocal              

    功能:从hdfs剪切粘贴到本地

    示例:hadoop  fs  - moveToLocal   /aaa/bbb/cc/dd  /home/hadoop/a.txt 

    --appendToFile  

    功能:追加一个文件到已经存在的文件末尾

    示例:hadoop  fs  -appendToFile  ./hello.txt  hdfs://hadoop-server01:9000/hello.txt

    可以简写为:

    Hadoop  fs  -appendToFile  ./hello.txt  /hello.txt

    -cat  

    功能:显示文件内容  

    示例:hadoop fs -cat  /hello.txt

    -tail                 

    功能:显示一个文件的末尾

    示例:hadoop  fs  -tail  /weblog/access_log.1

    -text                  

    功能:以字符形式打印一个文件的内容

    示例:hadoop  fs  -text  /weblog/access_log.1

    -chgrp

    -chmod

    -chown

    功能:linux文件系统中的用法一样,对文件所属权限

    示例:

    hadoop  fs  -chmod  666  /hello.txt

    hadoop  fs  -chown  someuser:somegrp   /hello.txt

    -copyFromLocal    

    功能:从本地文件系统中拷贝文件到hdfs路径去

    示例:hadoop  fs  -copyFromLocal  ./jdk.tar.gz  /aaa/

    -copyToLocal      

    功能:从hdfs拷贝到本地

    示例:hadoop fs -copyToLocal /aaa/jdk.tar.gz

    -cp              

    功能:从hdfs的一个路径拷贝hdfs的另一个路径

    示例: hadoop  fs  -cp  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

    -mv                     

    功能:在hdfs目录中移动文件

    示例: hadoop  fs  -mv  /aaa/jdk.tar.gz  /

    -get              

    功能:等同于copyToLocal,就是从hdfs下载文件到本地

    示例:hadoop fs -get  /aaa/jdk.tar.gz

    -         

    功能:合并下载多个文件

    示例:getmerge    如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...

    hadoop fs -getmerge /aaa/log.* ./log.sum

    -put                

    功能:等同于copyFromLocal

    示例:hadoop  fs  -put  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

    -rm                

    功能:删除文件或文件夹

    示例:hadoop fs -rm -r /aaa/bbb/

    -rmdir                 

    功能:删除空目录

    示例:hadoop  fs  -rmdir   /aaa/bbb/ccc

    -df               

    功能:统计文件系统的可用空间信息

    示例:hadoop  fs  -df  -h  /

    -du

    功能:统计文件夹的大小信息

    示例:

    hadoop  fs  -du  -s  -h /aaa/*

    -count         

    功能:统计一个指定目录下的文件节点数量

    示例:hadoop fs -count /aaa/

    -setrep                

    功能:设置hdfs中文件的副本数量

    示例:hadoop fs -setrep 3 /aaa/jdk.tar.gz

    补充:查看dfs集群工作状态的命令

    hdfs dfsadmin -report

    Java API操作:

    package com.study.hdfs;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.BlockLocation;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    import org.apache.hadoop.hdfs.DistributedFileSystem;
    import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.Iterator;
    import java.util.Map;
    
    /**
     * @author wangxu
     * @date 2016/12/17
     */
    public class HDFSTest {
        
        private static FileSystem fs;
        
        @BeforeClass
        public static void setup() throws URISyntaxException, IOException, InterruptedException {
            //方式1通过配置来获取fs
            /*Configuration conf = new Configuration();
            conf.set("fs.defaultFS", "hdfs://node1:9000");
            FileSystem fs = FileSystem.get(conf);
            System.setProperty("HADOOP_USER_NAME", "root");*/
            //方式2直接获取fs
            Configuration conf = new Configuration();
            conf.set("dfs.replication", "2");
            conf.set("dfs.block.size", "64m");
            fs = FileSystem.get(new URI("hdfs://node1:9000"), conf, "root");
        }
        
        
        @Test
        public void test01() throws IOException {
            fs.copyToLocalFile(new Path("/plans.txt"),new Path("C:/Users/wxisme/Desktop/bigdatatest/"));
        }
        
        @Test
        public void test02() throws IOException {
            FSDataOutputStream out = fs.create(new Path("/plans01.txt"));
            FileInputStream in = new FileInputStream("C:/Users/wxisme/Desktop/bigdatatest/plans.txt");
            
            IOUtils.copy(in, out);
        }
        
        @Test
        public void test03() throws IOException {
            DatanodeInfo[] dataNodeStats = ((DistributedFileSystem)fs).getDataNodeStats();
            for(DatanodeInfo dinfo: dataNodeStats){
                System.out.println(dinfo.getHostName());
            }
        }
        
        @Test
        public void test04() throws IOException {
    //        fs.mkdirs(new Path("/wangxu/study"));
    //        fs.rename(new Path("/wangxu/study"), new Path("/wangxu/play"));
            fs.delete(new Path("/wangxu/play"), true);
        }
        
        @Test
        public void test05() throws IOException {
            //返回迭代器,而不是List之类的容器,防止客户端内存溢出
            RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
            while (listFiles.hasNext()) {
                
                LocatedFileStatus fileStatus = listFiles.next();
                
                System.out.println(fileStatus.getPath().getName());
                System.out.println(fileStatus.getBlockSize());
                System.out.println(fileStatus.getPermission());
                System.out.println(fileStatus.getLen());
                BlockLocation[] blockLocations = fileStatus.getBlockLocations();
                for (BlockLocation bl : blockLocations) {
                    System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
                    String[] hosts = bl.getHosts();
                    for (String host : hosts) {
                        System.out.println(host);
                    }
                }
                
                System.out.println("--------------分割线--------------");
                
            }
        }
        
        @Test
        public void test06() throws IOException {
            FileStatus[] listStatus = fs.listStatus(new Path("/"));
            
            String flag = "";
            for (FileStatus fstatus : listStatus) {
                
                if (fstatus.isFile()) {
                    flag = "f-- ";
                } else {
                    flag = "d-- ";
                }
                System.out.println(flag + fstatus.getPath().getName());
                System.out.println(fstatus.getPermission());
                
            }
        }
        
        
        @Test
        public void test07() {
            Configuration conf = new Configuration();
            conf.addResource("test.xml");
            System.out.println(conf.get("xxx.uu"));
    
            Iterator<Map.Entry<String, String>> it = conf.iterator();
    
            while(it.hasNext()){
    
                System.out.println(it.next());
    
            }
        }
        
        @Test
        public void testDownLoadFileToLocal() throws IllegalArgumentException, IOException{
            
            //先获取一个文件的输入流----针对hdfs上的
            FSDataInputStream in = fs.open(new Path("/jdk-7u65-linux-i586.tar.gz"));
            
            //再构造一个文件的输出流----针对本地的
            FileOutputStream out = new FileOutputStream(new File("c:/jdk.tar.gz"));
            
            //再将输入流中数据传输到输出流
            org.apache.hadoop.io.IOUtils.copyBytes(in, out, 4096);
            
            
        }
        
        @Test
        public void testUploadByStream() throws Exception{
            
            //hdfs文件的输出流
            FSDataOutputStream fsout = fs.create(new Path("/aaa.txt"));
            
            //本地文件的输入流
            FileInputStream fsin = new FileInputStream("c:/111.txt");
            
            org.apache.hadoop.io.IOUtils.copyBytes(fsin, fsout,4096);
            
            
        }
        
        
        
        
        /**
         * hdfs支持随机定位进行文件读取,而且可以方便地读取指定长度
         * 用于上层分布式运算框架并发处理数据
         * @throws IllegalArgumentException
         * @throws IOException
         */
        @Test
        public void testRandomAccess() throws IllegalArgumentException, IOException{
            //先获取一个文件的输入流----针对hdfs上的
            FSDataInputStream in = fs.open(new Path("/iloveyou.txt"));
            
            
            //可以将流的起始偏移量进行自定义
            in.seek(22);
            
            //再构造一个文件的输出流----针对本地的
            FileOutputStream out = new FileOutputStream(new File("d:/iloveyou.line.2.txt"));
            
            org.apache.hadoop.io.IOUtils.copyBytes(in,out,19L,true);
            
        }
        
        
        
        /**
         * 读取指定的block
         * @throws IOException
         * @throws IllegalArgumentException
         */
        @Test
        public void testCat() throws IllegalArgumentException, IOException{
            
            FSDataInputStream in = fs.open(new Path("/weblog/input/access.log.10"));
            //拿到文件信息
            FileStatus[] listStatus = fs.listStatus(new Path("/weblog/input/access.log.10"));
            //获取这个文件的所有block的信息
            BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(listStatus[0], 0L, listStatus[0].getLen());
            
            
            //第一个block的长度
            long length = fileBlockLocations[0].getLength();
            //第一个block的起始偏移量
            long offset = fileBlockLocations[0].getOffset();
            
            System.out.println(length);
            System.out.println(offset);
            
            //获取第一个block写入输出流
    //        IOUtils.copyBytes(in, System.out, (int)length);
            byte[] b = new byte[4096];
            
            FileOutputStream os = new FileOutputStream(new File("d:/block0"));
            while(in.read(offset, b, 0, 4096)!=-1){
                os.write(b);
                offset += 4096;
                if(offset>length) return;
            };
            
            os.flush();
            os.close();
            in.close();
        }
        
        
        @AfterClass
        public static void shutdown() throws IOException {
            fs.close();
        }
        
    }
  • 相关阅读:
    rsync+inotify-tools实时备份脚本
    rsync+inotify实现实时同步
    linux 上安装部署python
    rsync全网备份low方法
    rsync 参数说明及使用参数笔记好文摘抄
    rsync 参数说明及使用参数笔记
    js DOM
    导出Excel
    Linux(CentOS 8)安装docker
    Win10安装虚拟机
  • 原文地址:https://www.cnblogs.com/wxisme/p/6279141.html
Copyright © 2020-2023  润新知