栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

使用Java程序创建Hadoop-Client操作Hadoop

使用Java程序创建Hadoop-Client操作Hadoop

1.创建一个Maven工程

2.在Example的pom文件中添加依赖管理
    
        
            
            
                org.apache.hadoop
                hadoop-client
                ${hadoop.version}
            
        
    

为了方便依赖版本的修改在properties中添加版本参数

    
        11
        11
        2.7.7
    

最后在Xu_Hadoop添加依赖,因为在管理中已经声明了版本因此在这里就不用再设置版本


    
        org.apache.hadoop
        hadoop-client
    

3.实现代码
	
    public static FileSystem getfileSystem(){
        //HDFS关键类FileSystem
        //连接的URI
        URI uri = URI.create("hdfs://192.168.1.200:8020");
        //相关配置
        Configuration conf = new Configuration();
        //可以设置副本个数如:conf.set("dfs.replication","3");
        //客户端名称
        String user = "root";
        FileSystem fileSystem = null;
        try {
            fileSystem = FileSystem.get(uri, conf, user);
        } catch (IOException e) {
            logger.error("连接HDFS失败",e);
        } catch (InterruptedException e) {
            logger.error("连接HDFS失败",e);
        }
        return fileSystem;
    }

3.1文件上传
    Path src = new Path("C:\Users\xpf\Desktop\笔记\Java.txt");
    Path dst = new Path("/newFolder/");
    
    public static void upFile(FileSystem fileSystem,Path src,Path dst){
        try {
            fileSystem.copyFromLocalFile(src, dst);
        } catch (IOException e) {
            logger.error("上传文件失败");
        }
    }
3.2文件下载
    Path src = new Path("/newFolder/java.txt");
    Path dst = new Path("C:\Users\xpf\Desktop\笔记\");
    
    public static void downFile(FileSystem fileSystem,Path src,Path dst){
        try {
            fileSystem.copyToLocalFile(src, dst);
        } catch (IOException e) {
            logger.error("下载文件失败");
        }
    }
3.3创建空文件
    Path src = new Path("/newFolder/python.txt");
    
    public static void createFile(FileSystem fileSystem,Path src){
        try {
            fileSystem.createNewFile(src);
        } catch (IOException e) {
            logger.error("文件创建失败");
        }
    }java
3.4删除文件
    Path src = new Path("/newFolder/");
    
    public static void deleteFile(FileSystem fileSystem,Path src){
        try {
            fileSystem.delete(src,true);
        } catch (IOException e) {java
            logger.error("文件删除失败");
        }
    }
3.4修改文件名称
    Path src = new Path("/newFolder/python.txt");
    Path dst = new Path("/newFolder/html.txt");
    
    public static void alterFile(FileSystem fileSystem,Path src,Path dst){
        try {
            fileSystem.rename(src,dst);
        } catch (IOException e) {java
            logger.error("修改名称失败");
        }
    }
3.5查看文件信息
	
    private static void printAsLS(FileStatus fileStatus) {
        StringBuilder builder = new StringBuilder();
        // 添加文件类型
        builder.append(fileStatus.isFile() ? "-" : "d");
        // 添加权限
        FsPermission permission = fileStatus.getPermission();
        builder.append(permission.toString());
        // 添加副本数
        short replication = fileStatus.getReplication();
        builder.append("t")
                .append(replication);
        //添加用户
        builder.append("t")
                .append(fileStatus.getOwner());
        //添加组
        builder.append("t")
                .append(fileStatus.getGroup());
        // 添加文件长度
        builder.append("t")
                .append(fileStatus.getLen());
        // 添加修改时间
        long time = fileStatus.getModificationTime();
        builder.append("t")
                .append(DateFormatUtils.formatYMDHM(time));
        // 添加文件路径java
        builder.append("t")
                .append(fileStatus.getPath().toString().substring(18));
        System.out.println(builder);
    }
4.客户端与HDFS之间的文件上传与下载的本质

观察源码不难发现,其实文件上传的下载都是运用了流的形式将文件转化成字节流进行传输,实现原理如下:

Path hdfsPath = new Path("/python.txt");
    String upFile = "C:\Users\xpf\Desktop\笔记\pyton\";
    String localDown = "C:\Users\xpf\Desktop\笔记\Java\";

    
    private static void get(FileSystem fileSystem, Path hdfsPath, String localDown) throws IOException {
        // HDFS  ->  输入流 -> Java程序 -> 输出流  -> 本地文件
        FSDataInputStream inputStream1 = fs.open(hdfsPath);
        FileOutputStream outputStream1 = new FileOutputStream(localDown);
        IOUtils.copyBytes(inputStream1, outputStream1, 4096);
    }
    
    
    private static void put(FileSystem fileSystem, Path hdfsPath, String upFile) throws IOException {
        //  文件  -> 输入流  -> Java程序(客户端) -> 输出流 -> HDFS
        // 1. 创建输入流读取本地文件
        FileInputStream inputStream = new FileInputStream(localFile);
        // 2. 创建输出流 用于将文件写到HDFS
        FSDataOutputStream outputStream = fs.create(hdfsPath);
        // 3. 拼接流
        IOUtils.copyBytes(inputStream, outputStream, 4096);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/327082.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号