大数据(四):通过API和IO流控制HDFS

一、获取api和配置开发环境

  1. 准备jar包

    1. 解压hadoop-2.7.2.tar.gz到非中文目录

    2. 进入share文件夹,查找所有jar包,并把jar包拷贝到_lib文件夹下

    3. 在全部jar包中查找sources.jar,并剪切到_source文件夹

    4. 在全部jar包中查找tests.jar,并剪切到_test文件夹

  2. 准备环境变量

    1. 在系统环境变量中添加HADOOP_HOME指向刚刚解压的hadoop-2.7.2文件夹

    2. 在系统环境变量中修改path变量,添加%HADOOP_HOME%/bin

  3. 配置ide环境

    1. 将_lib文件下的jar包引入ide即可,_source和_test中的不需要

    2. 查看服务器hadoop目录下etc/hadoop/core-site.xml中配置的路径(不清楚的看之前的博客)

    3. 编写上传文件的代码测试

//连接hdfs
Configuration entries = new Configuration();
//url:core-site.xml中填写的参数  gyx:linux的用户
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//上传文件
fileSystem.copyFromLocalFile(new Path("D://hello.txt"),new Path("/hello.txt"));
fileSystem.close();

二、使用API实现HDFS基本操作

1.修改文件副本数,当前连接的时候传送的文件的副本数

Configuration entries = new Configuration();
entries.set("dfs.replication","1");

2.文件下载

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//下载文件 4个参数 从左向右分别是:复制完后是否删除原文件 源文件地址 目标地址 是否启动文件校验
fileSystem.copyToLocalFile(false,new Path("/hello.txt"),new Path("D://hello.txt"),false);
fileSystem.close();

3.创建文件目录

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSytsem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//创建目录 支持递归创建
fileSystem.mkdirs(new Path("/home/gyx"));
fileSystem.close();

4.删除文件

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//删除文件,参数是文件路径和是否递归
fileSystem.delete(new Path("/home"),true);
fileSystem.close(); 

5.修改文件名称

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//修改文件名称,修改前和求改后名称
fileSystem.rename(new Path("/hello.txt"),new Path("/hello1.txt"));
fileSystem.close();

6.查看文件详情(文件名、权限、长度、块信息)

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//查询文件目录列表,文件路径和是否递归查询
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
while (locatedFileStatusRemoteIterator.hasNext()){
LocatedFileStatus locatedFileStatus = locatedFileStatusRemoteIterator.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);
            }
        }
    }
fileSystem.close();

7.判断是文件还是文件夹

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//查询文件目录列表,文件路径和是否递归查询
FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/"));
    for (FileStatus fileStatus : fileStatuses) {
        if (fileStatus.isFile()){
            System.out.println("文件:"+fileStatus.getPath().getName());
        }else {
            System.out.println("目录:"+fileStatus.getPath().getName());
        }
    }
fileSystem.close();

三、通过IO流控制HDFS

1.上传文件

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//输入流获取文件
FileInputStream fis = new FileInputStream(new File("D://hello.txt"));
//输出流获取上传路径
FSDataOutputStream fos = fileSystem.create(new Path("/hello2.txt"));
//执行上传
IOUtils.copyBytes(fis,fos,entries);
//关闭流
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fileSystem.close();

2.文件下载

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//输入流获取文件
FSDataInputStream fis = fileSystem.open(new Path("/hello2.txt"));
//输出流获取下载位置
FileOutputStream fos = new FileOutputStream(new File("D://hello2.txt"));
//执行下载
IOUtils.copyBytes(fis,fos,entries);
//关闭流
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fileSystem.close();

3.文件的定位下载(定位块读取)

    下载第一部分

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//输入流获取文件
FSDataInputStream fis = fileSystem.open(new Path("/hello2.txt"));
//输出流获取下载位置
FileOutputStream fos = new FileOutputStream(new File("D://hello2.txt.part1"));
//执行下载
byte[] buf= new byte[1024];
for (int i = 0; i < 1024 * 128; i++) {
    fis.read(buf);
    fos.write(buf);
}
//关闭流
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fileSystem.close();

    下载第二部分

//连接hdfs
Configuration entries = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.44.128:9000"),entries,"gyx");
//输入流获取文件
FSDataInputStream fis = fileSystem.open(new Path("/hello2.txt"));
//定位读取位置
fis.seek(1024*1024*128);
//输出流获取下载位置
FileOutputStream fos = new FileOutputStream(new File("D://hello2.txt.part2"));
//执行下载
IOUtils.copyBytes(fis,fos,entries);
//关闭流
IOUtils.closeStream(fis);
IOUtils.closeStream(fos);
fileSystem.close();

猜你喜欢

转载自blog.csdn.net/qq_34886352/article/details/82220230