• 大数据学习——hdfs客户端操作


    package cn.itcast.hdfs;
    
    
    import org.apache.commons.io.IOUtils;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.*;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URI;
    
    
    public class HdfsClient {
        FileSystem fs = null;
    
        @Before
        public void init() throws Exception {
            // 构造一个配置参数对象,设置一个参数:我们要访问的hdfs的URI
            // 从而FileSystem.get()方法就知道应该是去构造一个访问hdfs文件系统的客户端,以及hdfs的访问地址
            // new Configuration();的时候,它就会去加载jar包中的hdfs-default.xml
            // 然后再加载classpath下的hdfs-site.xml
            // conf.set("fs.defaultFS", "hdfs://mini1:9000");
            /**
             * 参数优先级: 1、客户端代码中设置的值 2、classpath下的用户自定义配置文件 3、然后是服务器的默认配置
             */
            /*conf.set("dfs.replication", "2");
            conf.set("dfs.block.size", "64m");*/
    
            // 获取一个hdfs的访问客户端,根据参数,这个实例应该是DistributedFileSystem的实例
            // fs = FileSystem.get(conf);
    
            // 如果这样去获取,那conf里面就可以不要配"fs.defaultFS"参数,而且,这个客户端的身份标识已经是root用户
            Configuration conf = new Configuration();
            fs = FileSystem.get(new URI("hdfs://mini1:9000"), conf, "root");
        }
    
        /**
         * 往hdfs上传文件
         */
    
        @Test
        public void testAddFileToHdfs() throws Exception {
            //要上传的文件所在的本地路径
            //要上传到hdfs的目标路径*/
            Path src = new Path("e:/hello1.txt");
            Path dst = new Path("/");
            fs.copyFromLocalFile(src, dst);
            fs.close();
    
        }
        /**
         * 从hdfs中复制文件到本地文件系统
         *
         * @throws IOException
         * @throws IllegalArgumentException
         */
        @Test
        public void testDownloadFileToLocal() throws IllegalArgumentException, IOException {
    
            fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:/"), true);
            fs.close();
    
        }
    
        /**
         * 通过流的形式从hdfs下载数据
         * @throws Exception
         */
        @SuppressWarnings("resource")
        @Test
        public void testDownloadFileToLocal2() throws Exception {
    
            FSDataInputStream in = fs.open(new Path("/hello1.txt"));
    
            FileOutputStream out = new FileOutputStream(new File("e:/1.txt"));
    
            IOUtils.copy(in, out);
    
            fs.close();
        }
        /**
         * 目录操作
         *
         * @throws IllegalArgumentException
         * @throws IOException
         */
        @Test
        public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {
    
            // 创建目录
    //        fs.mkdirs(new Path("/nihao/henhao/yeah"));
    
    //        // 删除文件夹 ,如果是非空文件夹,参数2必须给值true
    //        fs.delete(new Path("/nihao/henhao"), true);
    //
    //        // 重命名文件或文件夹
            fs.rename(new Path("/nihao"), new Path("/ni"));
    
        }
        /**
         * 查看目录信息,只显示文件
         *
         * @throws IOException
         * @throws IllegalArgumentException
         * @throws FileNotFoundException
         */
        @Test
        public void testListFiles() throws FileNotFoundException, IllegalArgumentException, 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("--------------为allen打印的分割线--------------");
    
            }
    
        }
    
        /**
         * 查看文件及文件夹信息
         *
         * @throws IOException
         * @throws IllegalArgumentException
         * @throws FileNotFoundException
         */
        @Test
        public void testListAll() throws FileNotFoundException, IllegalArgumentException, 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());
    
            }
    
        }
    }
  • 相关阅读:
    UIButton的遍历
    UIbutton 圆角和边线
    UIbutton 和UIview 切单角
    为运行Microsoft Dynamics CRM 异步处理服务指定账户没有性能计数器权限
    转:细说ASP.NET Windows身份认证
    CRM 模拟用户
    CRM 2016 Get IOrganizationService
    模拟SQL用户 EXECUTE AS USER
    Calling Custom Actions from JavaScript
    processjs Documentation
  • 原文地址:https://www.cnblogs.com/feifeicui/p/10216724.html
Copyright © 2020-2023  润新知