1、创建一个maven工程



创建好之后,第一个先添加依赖:
<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>


新建一个.xml,是log4j2.xml
在项目的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>



准备类做操作:

开发客户端代码的套路:
1、获取客户端对象
2、调用API进行具体的操作
3、关闭操作!
测试一、正常连接hdfs

1、获取客户端对象:文件系统对象
import org.apache.hadoop.fs.FileSystem;

第一个参数:
uri是当时配置core-site.xml里面namenode的值

uri=hdfs://hadoop102:9820
因为整个hadoop都是围绕namenode而工作的。
这个URI导入的包:
import java.net.URI;
有两种方式:
1、直接new
URI uri=new URI(“hdfs://hadoop102:9820”);
2、使用create()
URI uri = URI.create(“hdfs://hadoop102:9820”);
第二个参数:configuration
import org.apache.hadoop.conf.Configuration;
Configuration conf = new Configuration();
第三个:登录用户
String user=“atguigu”;
FileSystem fileSystem = FileSystem.get(uri, conf, user);
这个fileSystem就是客户端对象

第二步、使用这个客户端对象去调用API方法做测试
创建一个目录



代码优化:
不要出现每操作一次都需要定义测试方法获取文件系统对象
定义init()使用注解 @Before
package com.atguigu.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.net.URI;
public class TestHdfs {
private URI uri=URI.create("hdfs://hadoop102:9820");
private String user="atguigu";
private Configuration conf=new Configuration();
private FileSystem fs;
//希望这个方法在以后测试之前都先调用
@Before
public void init() throws Exception {
fs=FileSystem.get(uri,conf,user);
}
@After
public void close() throws Exception {
fs.close();
}
@Test
public void testHDFS() throws Exception{
boolean b = fs.mkdirs(new Path("/java"));
}
}

测试文件的上传:
- @param delSrc whether to delete the src
- @param overwrite whether to overwrite an existing file
- @param src path
- @param dst path


写路径的灵活使用:
new Path(“E:”+ File.separator+“io”+File.separator+“hello.txt”)
不管是linux还是windows系统



参数的优先级:
-site.xml > -default.xml
上个案例讲的上传文件hello.txt 默认副本数是3

现在在上传一个文件fangfang.txt
并在resources里新建一个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>1</value>
</property>
</configuration>



@Test
public void testUpload() throws Exception{
fs.copyFromLocalFile(false,false,
// new Path("E:\\io\\input\\hello.txt"),
new Path("E:"+ File.separator+"io"+File.separator+"input"+File.separator+"fangfang.txt"),
new Path("/java"));
}


在resources下面写的XXX-site.xml的配置>-site.xml > -default.xml
在代码中去修改副本数:




总结:

下载/bigdata下的yaner.txt文件到本地


实现文件的删除:
- @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.
文件的更改和移动:
使用-raname 实现移动文件
将/bigdata/songsong1.txt文件改名为songsong2.txt并移动到根目录