java操作hdfs

1.添加maven依赖,即在pom.xml文件李添加依赖

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>HdfsOperate</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>2.0.5</version>
        </dependency>
    </dependencies>

</project>
复制代码

2.添加log4j配置

1
2
3
4
5
6
7
8
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/log.log
Log4j.appender.Logfile.Layout=org.apache.log4j.PatternLayout
Log4j.appender.Logfile.Layout.ConversionPattern=%d %p [%c] -%m%n

3.编写测试案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.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;
 
public class HdfsOperateTest {
    private FileSystem fs;
 
    @Before
    public void init() throws URISyntaxException, IOException, InterruptedException {
        URI uri = new URI("hdfs://hadoop01:8020");
        Configuration entries = new Configuration();
        String user = "root";
        fs = FileSystem.get(uri, entries, user);
    }
 
    @Test
    public void testMkdir() throws IOException {
        fs.mkdirs(new Path("/test"));
    }
 
    @After
    public void clean() throws IOException {
        fs.close();
    }
}

4.运行代码,检查结果

 

 

 

5.项目结构

 

posted @   NAVYSUMMER  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2019-11-27 python totp代码
2019-11-27 动态令牌-(OTP,HOTP,TOTP)-基本原理
2019-11-27 Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)
2018-11-27 在Ubuntu上快速搭建基于Beego的RESTful API
交流群 编程书籍
点击右上角即可分享
微信分享提示