栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第3章 HDFS客户端操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第3章 HDFS客户端操作

3.1 HDFS客户端环境准备
  1. 在IDEA中创建一个Maven工程teaching
  2. 导入相应的依赖

	
		junit
		junit
		RELEASE
	
	
		org.apache.logging.log4j
		log4j-core
		2.8.2
	
	
		org.apache.hadoop
		hadoop-common
		2.7.2
	
	
		org.apache.hadoop
		hadoop-client
		2.7.2
	
	
		org.apache.hadoop
		hadoop-hdfs
		2.7.2
	

  1. 创建包名:com.test.hdfs
  2. 创建HdfsClient类
package com.test.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class HDFSClient {
    public static void main(String[] args){
        // 1 获取文件系统配置
        Configuration configuration = new Configuration();

        FileSystem fs = null;
        try {
            fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration,"hdfs");

            // 2 创建目录
            fs.mkdirs(new Path("/teaching/hdfs2"));
        } catch (IOException | InterruptedException | URISyntaxException e) {
            e.printStackTrace();
        } finally {
            if(fs != null){
                // 3 关闭资源
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  1. 执行程序
    客户端去操作hdfs时,设定默认用户:hdfs
  2. 注意:如果打印不出日志,在控制台上只显示
1.log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).  
2.log4j:WARN Please initialize the log4j system properly.  
3.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3.2 HDFS的文件操作 3.2.1.HDFS文件上传(测试参数优先级)
  1. 编写源代码
public static void upFile(String filePath, String dstPath){
    Configuration configuration = new Configuration();

    FileSystem fileSystem = null;

    try{
        // 设置副本数
        configuration.set("dfs.replication","2");
        fileSystem = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");

        fileSystem.copyFromLocalFile(new Path(filePath),new Path(dstPath));
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        if(fileSystem != null){
            try {
                fileSystem.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. 将hdfs-site.xml拷贝到项目的根目录下




	
		dfs.replication
        1
	

  1. 参数优先级
    客户端代码中设置的值 > classpath下的用户自定义配置文件 > 然后是服务器的默认配置(副本数3)
3.2.2.HDFS文件下载
public static void downloadFile(String hdfsFile, String dst){
    Configuration configuration = new Configuration();
    FileSystem fs = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");
        // boolean delSrc 指是否将原文件删除,默认:false
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验,不开启文件校验,下载时会多一个crc文件,默认:false
        fs.copyToLocalFile(false,new Path(hdfsFile), new Path(dst),true);
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.2.3.HDFS文件夹删除
public static void delete(String filePath){
    Configuration configuration = new Configuration();
    FileSystem fs = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");
        // Path f 删除的路径
        // boolean recursive 路径是目录设置为true,路径为文件,可以设置为true或者false
        fs.delete(new Path(filePath),true);
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.2.4.HDFS文件名更改
public static void rename(String srcPath, String dstPath) {
    Configuration configuration = new Configuration();
    FileSystem fs = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");
        fs.rename(new Path(srcPath), new Path(dstPath));
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.2.5.HDFS文件详情查看

查看文件名称、权限、长度、块信息

public static void listFiles(String path){
    Configuration configuration = new Configuration();
    FileSystem fs = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");
        RemoteIterator files = fs.listFiles(new Path(path),true);
        while(files.hasNext()){
            LocatedFileStatus locatedFileStatus = files.next();
            System.out.println(locatedFileStatus.getPath().getName());
            System.out.println(locatedFileStatus.getLen());
            System.out.println(locatedFileStatus.getPermission());
            System.out.println(locatedFileStatus.getGroup());
            BlockLocation[] blockLocations = locatedFileStatus.getBlockLocations();
            for(BlockLocation blockLocation: blockLocations){
                String[] hosts = blockLocation.getHosts();
                for(String host:hosts){
                    System.out.println(host);
                }
            }
        }
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.2.6.HDFS文件和文件夹判断
public static void fileStatus(String path) {
    Configuration configuration = new Configuration();
    FileSystem fs = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"), configuration, "hdfs");
        FileStatus[] fileStatus = fs.listStatus(new Path(path));
        for (FileStatus fileStatus1 : fileStatus) {
            if (fileStatus1.isFile()) {
                System.out.println(fileStatus1.getPath().getName() + " is file!");
            } else {
                System.out.println(fileStatus1.getPath().getName() + " is directory!");
            }
        }
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if (fs != null) {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
3.3 HDFS的I/O流操作 3.3.1.HDFS文件上传
public static void putFileToHDFS(String localFile, String hdfsFile){
    Configuration configuration = new Configuration();
    FileSystem fs = null;
    FileInputStream fis = null;
    FSDataOutputStream fos = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"),configuration,"hdfs");
        fis = new FileInputStream(new File(localFile));
        fos = fs.create(new Path(hdfsFile));
        IOUtils.copyBytes(fis,fos,configuration);
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null){
            IOUtils.closeStream(fis);
        }
        if(fos != null){
            IOUtils.closeStream(fos);
        }
    }
}
3.3.2.HDFS文件下载
  1. 需求:从HDFS上下载文件到本地。
  2. 编写代码:
public static void getFileFromHDFS(String hdfsFile, String localFile){
    Configuration configuration = new Configuration();
    FileSystem fs = null;
    FSDataInputStream fis = null;
    FileOutputStream fos = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"),configuration, "hdfs");
        fis = fs.open(new Path(hdfsFile));
        fos = new FileOutputStream(localFile);
        IOUtils.copyBytes(fis,fos,configuration);
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null){
            IOUtils.closeStream(fis);
        }
        if(fos != null){
            IOUtils.closeStream(fos);
        }
    }
}
3.3.3.定位文件读取
  1. 下载文件快的第一块
public static void readFileSeek(String hdfsFile, String localFile){
    Configuration configuration = new Configuration();
    FileSystem fs = null;
    FSDataInputStream fis = null;
    FileOutputStream fos = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"),configuration, "hdfs");
        fis = fs.open(new Path(hdfsFile));
        fos = new FileOutputStream(localFile);
        byte[] buf = new byte[1024];

        for(int i =0 ; i < 1024*128 ; i++){
            fis.read(buf);
            fos.write(buf);
        }
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null){
            IOUtils.closeStream(fis);
        }
        if(fos != null){
            IOUtils.closeStream(fos);
        }
    }
}
  1. 下载文件块的第二块
public static void readFileSeek2(String hdfsFile, String localFile){
    Configuration configuration = new Configuration();
    FileSystem fs = null;
    FSDataInputStream fis = null;
    FileOutputStream fos = null;

    try {
        fs = FileSystem.get(new URI("hdfs://10.13.11.22:8020"),configuration, "hdfs");
        fis = fs.open(new Path(hdfsFile));
//从指定文件的pos位置,对文件流向前搜索。
        fis.seek(1024*1024*128);
        fos = new FileOutputStream(localFile);
        IOUtils.copyBytes(fis,fos,configuration);
    } catch (IOException | InterruptedException | URISyntaxException e) {
        e.printStackTrace();
    } finally {
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fis != null){
            IOUtils.closeStream(fis);
        }
        if(fos != null){
            IOUtils.closeStream(fos);
        }
    }
}
  1. 合并文件

在window命令窗口中执行

type hadoop-2.7.2.tar.gz.part2 >> hadoop-2.7.2.tar.gz.part1
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/943377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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