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;
public class HdfsClient {
static FileSystem fs;
@Before
public void before() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
fs = FileSystem.get(new URI("hdfs://hadoop103:9000"), configuration, "childWen");
}
@After
public void after() throws IOException {
fs.close();
}
@Test
public void testMkdir() throws IOException {
fs.mkdirs(new Path("/hello"));
}
@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("这是文件夹");
}
}
}
@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);
}
@Test
public void testCopyToLocalFile() throws IOException {
fs.copyToLocalFile(false,new Path("/hello"),new Path("E:\\helloworld"),true);
}
@Test
public void testRename() throws IOException {
fs.rename(new Path("/hello"),new Path("/helloworld"));
}
@Test
public void testcopyFromLocalFile() throws IOException {
fs.copyFromLocalFile(false,false,new Path("E:\\2020Target"),new Path("/"));
}
@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("**************文件分割线***************");
}
}
@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);
}
}
}
}