• Hadoop编程实现之HDFS


    HDFS原理图:

    下面我们来写一个基于HDFS的demo,该demo主要实现的是将HDFS上的一个文件内容读取出来并保存到另一个文件上的功能。

    1.辅助类

    这个类主要是用来获取hdfs文件系统连接的

    public class HdfsUtils {
        
        /**
         * @return
         * @throws Exception
         */
        public static FileSystem getFileSystem() throws Exception{
            
            Configuration conf = new Configuration() ;
            conf.set("fs.defaultFS", "hdfs://192.168.1.109:8020");
            
            FileSystem fileSystem = FileSystem.get(conf) ;
            
            return fileSystem ;
        }
        /**
         * @param pOpenUri
         * @param pUser
         * @return
         * @throws Exception
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        public static FileSystem getFileSystemByUser(String pOpenUri,String pUser) throws Exception, InterruptedException, URISyntaxException{
            
            Configuration conf = new Configuration() ;
            conf.set("fs.defaultFS", "hdfs://192.168.1.109:8020");
            
            FileSystem fileSystem = FileSystem.get(new URI(pOpenUri), conf, pUser) ;
            
            return fileSystem ;
            
        }
        
        /**
         * @param pUser
         * @return
         * @throws Exception
         * @throws InterruptedException
         * @throws URISyntaxException
         */
        public static FileSystem getFileSystemByUser(String pUser) throws Exception, InterruptedException, URISyntaxException{
            
            String fileUri = "/home/test/test.txt" ;
            
            Configuration conf = new Configuration() ;
            conf.set("fs.defaultFS", "hdfs://192.168.1.109:8020");
            
            FileSystem fileSystem = FileSystem.get(new URI(fileUri), conf, pUser) ;
            
            return fileSystem ;
            
        }
        
        
    
    }

    2.主类

    这个类主要是用来进行文件读写和创建的

    public class HdfsFsTest {
    
        public static void main(String[] args) {
    
            String fileUri = "/home/test/test.txt";
            String fileOutputUrl = "/home/test/out.txt";
            try {
    
                writeFileToHdfs(fileUri, fileOutputUrl);
                System.out.println("DONE!");
    
            } catch (Exception e) {
    
                e.printStackTrace();
            }
    
        }
    
        public static void writeFileToHdfs(String pOpenUri, String pOutputUrl)
                throws Exception {
    
            FileSystem fileSystem = null;
            FSDataInputStream fileInputStream = null;
            FSDataOutputStream fileOutputStream = null;
            int buffSize = 4096;
    
            try {
    
                fileSystem = HdfsUtils.getFileSystem();
    
                fileInputStream = fileSystem.open(new Path(pOpenUri));
                fileOutputStream = fileSystem.create(new Path(pOutputUrl));
                IOUtils.copyBytes(fileInputStream, fileOutputStream, buffSize,
                        false);
    
            } catch (Exception e) {
                throw e;
            } finally {
                IOUtils.closeStream(fileInputStream);
    
                IOUtils.closeStream(fileOutputStream);
            }
    
        }
    
    }

    3.运行结果

    运行成功!

  • 相关阅读:
    Linux系统报错Fork Failed: cannot allocate memory
    RPA系统常见使用操作
    解决:安装composer时The openssl extension is missing, which means that secure HTTPS transfers are impossib
    centos7设置、查看、删除环境变量的方法
    asp.net core 用NPOI放word中写入数据 江南
    C# Decimal 去除小数点后无效的0 江南
    C# 日期格式化以及日期常用方法 江南
    容器技术基础
    github项目初始化
    uni微信小程序优化,删除打包后的import vue路径
  • 原文地址:https://www.cnblogs.com/stardjyeah/p/4643618.html
Copyright © 2020-2023  润新知