HDFS客户端操作

1. HDFS客户端环境搭建

1.1 安装hadoop

1.安装对应的编译后的hadoop包到非中文路径下
	1.1 这里简单阐述一下在搭建环境时为什么hadoop需要重新编译源码
		答: hadoop是用java写的,但是某些操作不适合用java实现,所以用的是c/c++的动态库(本地库),所以需要根据不同的处理器架构,重新编译,他们以库的形式提供接口供上层调用。
2.配置HADOOP_HOME相关环境变量(如何配置hadoop环境变量笔者就不在赘述,跟Java同理)

1.2 创建Maven工程导入相应依赖

<dependencies>
    
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-client</artifactId>
			<version>2.7.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-hdfs</artifactId>
			<version>2.7.2</version>
		</dependency>
		
</dependencies>
注:
1) 如果idea打印不出日志,需要在项目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

1.3 连接集群测试

package com.hadoop.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


/**
 * @author childwen
 * HDFS客户端测试
 */
public class HdfsClient {
    static FileSystem fs;

    /**
     * 连接集群
     */
    @Before
    public void before() throws URISyntaxException, IOException, InterruptedException {
        // 1 生成配置文件
        Configuration configuration = new Configuration();
        // 2 获取集群对象操作集群
        // 注: 这里的hadoop103是core-site.xml配置文件中指定的内部通讯集群节点,在连接时不指定内部通讯节点则会出现connectionexception异常
        fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "childWen");
    }
    
    /**
     * 关闭资源
    */
    @After
    public void after() throws IOException {
        fs.close();
    }
    
    /**
     * 创建文件目录
     * @throws IOException
     */
    @Test
    public void testMkdir() throws IOException {
        fs.mkdirs(new Path("/hello"));
    }
    
    /**
     * 查看目录下文件信息
     * @throws IOException
     */
    @Test
    public void testCat() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println("===========分割线===============");
            System.out.println(fileStatus.getPath());
            System.out.println(fileStatus.getPermission());
            if(fileStatus.isDirectory()){
                System.out.println("这是文件夹");
            }
        }
    }
    
     /**
     * 测试参数优先级
       测试结果:参数优先级排序:(1)客户端代码中设置的值 >(2)ClassPath下的用户自定义配置文件 >(3)然后是服务器的site配置 > (4) 然后是框架的default配置
     */
    @Test
    public void testCopyLocalFile() throws IOException, URISyntaxException, InterruptedException {
        Configuration entries = new Configuration();
        //设置副本数量
        entries.set("dfs.replication","1");
        FileSystem Fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), entries, "atguigu");
        Fs.copyFromLocalFile(new Path("E:\\helloworld"),new Path("/"));
    }
    
     /**
     * 测试删除
     */
    @Test
    public void testDelete() throws IOException {
        fs.delete(new Path("/helloworld"),true);
    }
    
      /**
     * HDFS文件下载
     * @parm boolean delSrc 指是否将原文件删除
     * @parm Path src 指要下载的文件路径
     * @parm Path dst 指将文件下载到的路径
     * @parm boolean useRawLocalFileSystem 是否开启文件校验
     */
    @Test
    public void testCopyToLocalFile() throws IOException {
        fs.copyToLocalFile(false,new Path("/hello"),new Path("E:\\helloworld"),true);
    }
    
    /**
     * HDFS文件改名
     */
    @Test
    public void testRename() throws IOException {
        fs.rename(new Path("/hello"),new Path("/helloworld"));
    }
    
     /**
     * HDFS文件下载
     * @parm boolean delSrc 指是否将原文件删除
     * @parm Path src 指要下载的文件路径
     * @parm Path dst 指将文件下载到的路径
     * @parm boolean useRawLocalFileSystem 是否开启文件校验
     */
    @Test
    public void testcopyFromLocalFile() throws IOException {
        fs.copyFromLocalFile(false,false,new Path("E:\\2020Target"),new Path("/"));
    }
    
    /**
     * 文件详情查看
     * 深度遍历HDFS目录文件
     * 遍历方式: 递归
     * 打印方式: 顺序打印
     */
    @Test
    public void testListFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> listFile = fs.listFiles(new Path("/"), true);
        while(listFile.hasNext()){
            LocatedFileStatus status = listFile.next();
            //文件路径
            System.out.println(status.getPath());
            //文件名称
            System.out.println(status.getPath().getName());
            //文件权限
            System.out.println(status.getPermission());
            //文件长度
            System.out.println(status.getLen());
            //文件分组
            System.out.println(status.getGroup());

            //获取存储的块信息
            BlockLocation[] blockLocations = status.getBlockLocations();
            for (BlockLocation blockLocation : blockLocations) {
            	//获取块对应的服务器
                String[] hosts = blockLocation.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("**************文件分割线***************");
        }
    }
    
    /**
     * 遍历删除HDFS目录下所有文件
     * @throws IOException
     */
    @Test
    public void delete() throws IOException {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            if(fileStatus.isDirectory()){
                Path path = fileStatus.getPath();
                fs.delete(path,true);
            }
        }
    }
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值