HDFS的IDEA连接使用

HDFS的IDEA连接使用

上一章我们已经学会了shell操作,这一章其实没有很重要,但是有刀不用和无刀可用是两个概念。本来打算昨天晚上写的,但是昨晚太困了,结果。。。。一上床就打王者去了。

开始准备文件:

  • 下载Hadoop依赖:Apache Hadoop3.1.0
  • 下面这个就自己找链接去了,百度太多了
    • IDEA的安装
    • jdk安装

准备Windows关于Hadoop的开发环境

3.1 准备Windows关于Hadoop的开发环境

1、找到资料目录下的Windows依赖目录,打开:

image-20220119094310526

2、配置HADOOP_HOME环境变量。

3、配置Path环境变量。然后重启电脑%HADOOP_HOME%\bin这里截图截错了,自己注意哟

image-20220119094632825

4、创建一个Maven工程hadoop_operate,并导入相应的依赖坐标+日志添加

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.12.0</version>
  </dependency>
  <dependency>
  <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>3.1.3</version>
  </dependency>
</dependencies>

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

项目格式

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" strict="true" name="XMLConfig">
    <Appenders>
        <!-- 类型名为Console,名称为必须属性 -->
        <Appender type="Console" name="STDOUT">
            <!-- 布局为PatternLayout的方式,
            输出样式为[INFO] [2018-01-22 17:34:01][org.test.Console]I'm here -->
            <Layout type="PatternLayout"
                    pattern="[%p] [%d{yyyy-MM-dd HH:mm:ss}][%c{10}]%m%n" />
        </Appender>
    </Appenders>
    <Loggers>
        <!-- 可加性为false -->
        <Logger name="test" level="info" additivity="false">
            <AppenderRef ref="STDOUT" />
        </Logger>
        <!-- root loggerConfig设置 -->
        <Root level="info">
            <AppenderRef ref="STDOUT" />
        </Root>
    </Loggers>
</Configuration>

将hdfs-site.xml拷贝到项目的根目录下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>2</value>
    </property>
</configuration>

5、创建包名:com.pier.hdfs

6 、创建HdfsClient类

package com.pier.hdfs;

/**
 * @author:Pier
 * @DATE:2022/1/9
 */

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.FilterFactory;

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

/**
 * 1、和hdfs建立连接
 * 2、调用API完成具体功能
 * 3、关闭连接
 */
public class hdfsClientTest {
    private FileSystem fs;
    /**Before和After时在每一次执行test前搜狐不i去执行*/
    @Before
    public void init() throws IOException, InterruptedException {
        URI uri = URI.create("hdfs://hadoop105:9820");
        //conf配置对象
        Configuration conf = new Configuration();
//        conf.set("dfs.replication","6");
        //操作的用户(用那个用户操作HDFS)
        String user = "pier";
        //获取hdfs的客服端连接对象(文件系统对象)
        fs = FileSystem.get(uri ,conf ,user);
        /*System.out.println(fileSystem.getClass().getName());*/
    }
    /**
     * 关闭资源
     */
    @After
    public void close() throws IOException {
        fs.close();
    }

    /**上传文件
     * 配置优先级Configuration > hdfs-site.xml > hdfs-default.xml
     * */
    @Test
    public void testCopyFromLocal() throws IOException {
        /**
         * 上传文件,如果是true上传后本地文件删除
         * @param delSrc whether to delete the src
         * 如果上传时之前有这个文件是都需要覆盖
         * @param overwrite whether to overwrite an existing file
         * 本地路径
         * @param src path
         * hdfs文件
         * @param dst path
         * @throws IOException IO failure
         * */
        //  public void copyFromLocalFile(boolean delSrc, boolean overwrite,
        //                                Path src, Path dst)
        fs.copyFromLocalFile(false ,true ,
                             new Path("D:\\StudyFile\\BigDate\\02.大数据技术之Hadoop\\01.笔记\\HDFS坏块处理.txt") ,
                             new Path("/sanguo/client_test"));
    }

    /**
     * 下载文件
     * */
    @Test
    public void testCopyToLocal() throws IOException {
        /*** @param delSrc whether to delete the src
         * @param src path
         * @param dst path
         * @param useRawLocalFileSystem whether to use RawLocalFileSystem as local file system or not.
         **/
        fs.copyToLocalFile(false,
                           new Path("/sanguo/shuguo.txt"),
                           new Path("D:\\Study\\Test\\"),
                           true);
    }
    /**
     * 删除文件和目录
     * */
    @Test
    public void testDelete() throws IOException{
        /**
         *  * @param f the path to delete.
         *    * @param recursive(递归) if path is a directory and set to
         *    * true, the directory is deleted else throws an exception. In
         *    * case of a file the recursive can be set to either true or false.
         *    * @return  true if delete is successful else false.*/
        fs.delete(new Path("/sanguo/client_test"),true);
    }
    /**
     * 移动文件
     * */
    @Test
    public void testRemove() throws IOException{
        /**
         * 移动文件
         * 提示:
         * 这里可能出现问题,原因是文windows依赖的问题
         * */
        fs.rename(new Path("/xiyou/liubei.txt"),new Path("/xiyou/xiaoqiao.txt"));
    }
    /**
     * 查看文件详情
     * */
    @Test
    public void testListFiles() throws IOException{
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/") ,true);
        while ( listFiles.hasNext() ){
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println("文件名\t"+fileStatus.getPath().getName());
            System.out.println("文件副本数\t"+fileStatus.getReplication());
            System.out.println("文件块大小\t"+fileStatus.getBlockSize());
            System.out.println("文件权限\t"+fileStatus.getPermission()+"\n");
        }
    }
    /**
     * 判断是文件还是目录
     * */
    @Test
    public void testListStatus() throws IOException{
        FileStatus[] listStatus = fs.listStatus(new Path("/"));
        for (FileStatus status: listStatus) {
            if(status.isDirectory()){
                System.out.println("DIR:\t"+status.getPath().getName());
            }
            else {
                System.out.println("FILE:\t"+status.getPath().getName());
            }
        }
    }

}

执行程序即可

结果就不展示了,自己去尝试吧!!嘿嘿!!!

可能这一章你们会觉得我敷衍,因为这个实在太简单了,就调用几个方法,我把源码也给把注释弄出来了,参数详情,自己去看看就好啦。如果格式复制过去过后有些不好看,可以使用ctrl+alt+L调整一下哟!!再见,虽然简单,但是pier还是建议大家自己去多尝试一下哟。毕竟刀不开封,终究不过是锈铁。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值