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">
<parent>
<artifactId>beijing45-parent</artifactId>
<groupId>cn.itcast</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>day08_hdfs</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
</dependencies>
</project>
java API操作
package cn.itcast.hdfs;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
public class HdfsTest01 {
static FileSystem fileSystem;
static {
Configuration configuration = new Configuration();
//指定我们使用的文件系统类型:
configuration.set("fs.defaultFS", "hdfs://node1:8020/");
//获取指定的文件系统
try {
fileSystem = FileSystem.get(configuration);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取文件系统
*/
@Test
public void getFileSystem1() {
System.out.println(fileSystem.toString());
}
/**
* 获取 HDFS 上某个路径下所有的文件
*/
@Test
public void test01() throws IOException {
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
while (locatedFileStatusRemoteIterator.hasNext()) {
LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
Path path = next.getPath();
System.out.println(path);
}
//关闭释放资源
fileSystem.close();
}
/**
* 在HDFS上创建一个文件夹
*
* @throws IOException
*/
@Test
public void test02() throws IOException {
boolean mkdirs = fileSystem.mkdirs(new Path("/app/config"));
System.out.println(mkdirs);
//关闭释放资源
fileSystem.close();
}
/**
* 在HDFS上创建一个文件
*
* @throws IOException
*/
@Test
public void test03() throws IOException {
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/app/config/aaa.xml"));
fsDataOutputStream.write("username=zhangsan:password=123456".getBytes());
fsDataOutputStream.flush();
//关闭释放资源
fileSystem.close();
}
/**
* HDFS文件下载到本地
*
* @throws IOException
*/
@Test
public void test04() throws IOException {
fileSystem.copyToLocalFile(new Path("/app/config/aaa.xml"), new Path("D:\\"));
//关闭释放资源
fileSystem.close();
}
//6.完成文件上传的操作
@Test
public void upload() throws IOException {
fileSystem.copyFromLocalFile(new Path("F:\\_workspace_java\\hadoop_hdfs\\c3p0.xml"), new Path("/"));
//释放资源
fileSystem.close();
}
//7本地有多个小文件,上传到hdfs中,因为hdfs中不推荐使用小文件,将这些小文件进行合并操作
//合并成一个大文件统一上传。
@Test
public void mergeUpload() throws Exception {
//2.执行创建一个文件,生成输出流
FSDataOutputStream outputStream = fileSystem.create(new Path("/merge.txt"));
//获取本地文件系统
LocalFileSystem localFileSystem = FileSystem.getLocal(new Configuration());
//4.获取本地小文件
RemoteIterator<LocatedFileStatus> localListFiles = localFileSystem.listFiles(new Path("D:\\hadoop_hdfs\\"), false);
while (localListFiles.hasNext()) {
LocatedFileStatus fileStatus = localListFiles.next();
//获取本地文件的路径
Path path = fileStatus.getPath();
FSDataInputStream inputStream = localFileSystem.open(path);
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
}
outputStream.close();
}
//hdfs 访问权限设置
@Test
public void acl() throws Exception {
//1.创建hdfs的客户端
URI uri = new URI("hdfs://node1:8020");
Configuration conf = new Configuration();
//以root用户访问
FileSystem fileSystem = FileSystem.newInstance(uri, conf, "root");
//2.将core-site.xml 保存到本地系统
fileSystem.copyToLocalFile(new Path("/config2/core-site.xml"), new Path("F:\\_workspace_java\\hadoop_hdfs\\core-site.xml"));
//释放资源
fileSystem.close();
}
}