zoukankan      html  css  js  c++  java
  • hadoop hdfs的java操作

    访问hdfs上的文件并写出到输出台

       /**
         * 访问hdfs上的文件并写出到输出台
         * @param args
         */
        public static void main(String[] args) {
            try {
                //将hdfs格式的url转换成系统能够识别的
                URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
                URL url = new URL("hdfs://hadoop1:9000/hello");
                InputStream in = url.openStream();
                /**
                 * 将读取到的数据写入到文件,不需要自己控制缓冲区,也不需要自己去读取输入流
                 * @param in 输入流
                 * @param out 输出流
                 * @param bufferSize 换成区大小 
                 * @param close 是否关闭流,如果是false,需要在finally中关闭
                 *           IOUtils.closeStream(in);
                 */
                IOUtils.copyBytes(in, System.out, 1024, true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    获取FileSystem

       /**
         * 获取FileSystem
         * 使用Hadoop的fileSystem读取文件
         */
        public static FileSystem getFileSystem() throws IOException,
                URISyntaxException {
            FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop1:9000"), new Configuration());
            return fileSystem;
        }

    创建文件夹

       /**
         * 创建文件夹
         * @throws URISyntaxException 
         */
        public static void mkdir() throws IOException, URISyntaxException {
            FileSystem fileSystem = getFileSystem();
            //在hdfs上创建文件夹,并返回创建是否成功的标示
            boolean successful = fileSystem.mkdirs(new Path("/dir1"));
            if(successful){
                System.out.println("创建文件夹成功");
            }
        }

    上传

       /**
         * 上传
         * @throws URISyntaxException 
         */
        public static void putdata() throws IOException,
                FileNotFoundException, URISyntaxException {
            FileSystem fileSystem = getFileSystem();
            //创建一个上传路径,返回输出流
            FSDataOutputStream os = fileSystem.create(new Path("/dir1/readme"));
            FileInputStream in = new FileInputStream("D:\Program Files\others\2345Soft\HaoZip\2345好压免责声明.txt");
            IOUtils.copyBytes(in, os, 1024, true);
        }

    下载

       /**
         * 下载
         * @throws URISyntaxException 
         */
        public static void download() throws IOException, URISyntaxException {
            FileSystem fileSystem = getFileSystem();
            FSDataInputStream in = fileSystem.open(new Path("hdfs://hadoop1:9000/hello"));
            //关闭流需要手动关闭,System.out也是一个输出流,如果是true 下面就不会输出了
            IOUtils.copyBytes(in, System.out, 1024, false);
            in.close();
        }

    删除文件或文件夹

       /**删除文件或文件夹
         * true:表示是否递归删除,如果是文件,这里是true,false都是无所谓,
         *         文件夹必须是true,否则报错
         * @throws URISyntaxException 
         */
        public static void delete() throws IOException, URISyntaxException {
            FileSystem fileSystem = getFileSystem();
            boolean isDeleted = fileSystem.delete(new Path("/dir1"), true);
            if(isDeleted){
                System.out.println("删除成功");
            }
        }

    遍历目录

        /**遍历目录
         * 调用FileSystem的listStatus方法
         * 查看file的状态 使用FileStatus
         * @throws URISyntaxException 
         */
        public static void list() throws IOException, URISyntaxException {
            FileSystem fileSystem = getFileSystem();
            FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
            for (FileStatus fileStatus : listStatus) {
                String isDir = fileStatus.isDir()?"目录":"文件";
                String name = fileStatus.getPath().getName().toString();
                System.out.println(isDir+"-->"+name);
            }
        }
  • 相关阅读:
    JavaScript讲义(三)
    jQuery学习(五)
    jQuery学习(七)
    JavaScript讲义(一)
    jQuery学习(六)
    这些东西不宜空腹吃[转]
    pexpect实现的ssh连接(pexpect可从sourceforge下载)
    Using the commandline connection tool Plink
    【转】系统管理类DOS命令汇总
    挺有意思的QQ资料
  • 原文地址:https://www.cnblogs.com/xiaolong1032/p/4391561.html
Copyright © 2011-2022 走看看