1.创建MAVEN工程
名字写好一直next就行
2.在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>BigData</groupId>
<artifactId>hadoop-train-v2</artifactId>
<version>1.0</version>
<properties>
<!--定义Hadoop版本-->
<hadoop.version>2.6.0-cdh5.15.1</hadoop.version>
</properties>
<!--引入CDH仓库-->
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
</repository>
</repositories>
<dependencies>
<!--添加Hadoop的依赖包-->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!--添加junit依赖包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3.在测试文件中新建一个HDFSApp的类,用作测试连通性
4.在HDFSApp中编写如下代码
package hadoop.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
/**
* 使用Java API操作HDFS文件系统
*/
public class HDFSApp {
public static void main(String[] args) throws Exception{
//hadoop000:8020服务器端口号
Configuration configuration=new Configuration();
FileSystem fileSystem= FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration, "jerry");
//注意服务器名为hadoop000、用户名为jerry
Path path=new Path("/hdfsapi/test");
boolean result= fileSystem.mkdirs(path);
}
}
执行
5.到hdfs中去查看文件
到本地主机的浏览器中查看文件
创建成功!
6.做单元测试,查看文本系数,对文件进行增删改查等操作,就不一一举例了,公布源码
在hadoop中新建一个hdfs的包,其次新建一个名为HDFSApp的java文件
API操作的其他部分源码,进行单元测试时引入@Test接口 右键方法名执行对该方法的单元测试。
(!!!!注意导入包的时候不要导入错了,例如Configuration在java包中也有,但是你要导入的是apache.hadoop中的)
package hadoop.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.nio.ch.IOUtil;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
/**
* 使用Java API操作HDFS文件系统
* 关键点
* 1.创建Configuration
* 2.获取FileSystem
* 3.HDFS中API的操作
*
*/
public class HDFSApp {
public static final String HDFS_PATH= "hdfs://hadoop000:8020";
FileSystem fileSystem=null;
Configuration configuration=null;//定义一个配置信息
@Before
public void setUp() throws Exception{
System.out.println("-------setUp-------");
configuration=new Configuration();
configuration.set("dfs.replication","1");//设置副本的节点个数为1,默认为三个
/*
* 构造一个访问指定HDFS系统的客户端对象
*第一个参数:HDFS的URI
* 第二个参数:客户端指定的配置参数
*第三个参数:客户端的身份,即用户名
* */
fileSystem=FileSystem.get(new URI(HDFS_PATH), configuration,"jerry");
}
/*
* 创建HDFS文件夹
* */
@Test
public void mkdir() throws Exception{
fileSystem.mkdirs(new Path("/hadoop") );
}
/*
* 查看HDFS内容
* */
/* public void text() throws Exception{
FSDataInputStream in=fileSystem.open(new Path("/hdfsapi/test.text"));//文件的路径以及文件的类型HDFS中
IOUtils.copyBytes(in,System.out,1024);
}*/
/*
* 创建文件
* */
@Test
public void create() throws Exception{
FSDataOutputStream out=fileSystem.create(new Path("/hdfsapi/test/a.txt"));
out.writeUTF("hellow pk");
out.flush();//走缓冲区,先缓冲一下
out.close();
}
/*
* 测试文件名更改
* */
@Test
public void rename() throws Exception{
Path oldPath=new Path("/hdfsapi/test/a.txt");
Path newPath=new Path("/hdfsapi/test/c.txt");
boolean result=fileSystem.rename(oldPath,newPath);
System.out.println(result);
}
/*
* 拷贝本地文件到HDFS
* */
@Test
public void copyFromlocalFile() throws Exception{
Path src=new Path("D:\\hello.txt");
Path dst=new Path("/hdfsapi/test/");
fileSystem.copyFromLocalFile(src,dst);
}
/*
* 拷贝大文件到HDFS带进度显示
* */
@Test
public void copyFromlocalBigFile() throws Exception{
InputStream in=new BufferedInputStream(new FileInputStream(new File("D:\\迅雷\\dg.zip")));
FSDataOutputStream out=fileSystem.create(new Path("/hdfsapi/test1/do.zip"),
new Progressable(){
public void progress(){
System.out.print(".");
}
});
IOUtils.copyBytes(in, out ,4096);
}
/*
* 从HDFS上下载文件到本地 下载
* */
@Test
public void copyToLocalFile() throws Exception{
Path src=new Path("/hdfsapi/test/hello.txt");
Path dst=new Path("D:\\test");
fileSystem.copyToLocalFile(src,dst);
}
/*
* 查看文件夹下的所有文件
* */
@Test
public void listFile() throws Exception{
FileStatus[] statuses=fileSystem.listStatus(new Path("/hdfsapi"));
for(FileStatus file : statuses){
String isDir=file.isDirectory() ? "文件夹" : "文件";
String permission=file.getPermission().toString();
short replication=file.getReplication();
long length=file.getLen();
String path=file.getPath().toString();
System.out.println(isDir + "\t" + permission
+ "\t" + replication + "\t" + length
+ "\t" + path
);
}
}
/*
* 递归查看目标文件夹下的所有文件
* 递归查看文件夹中所有文件和直接查看文件夹中所有文件有什么区别
* */
@Test
public void listFileRecursive() throws Exception{
RemoteIterator<LocatedFileStatus> files=fileSystem.listFiles(new
Path("/hdfsapi"), true);
while (files.hasNext()){
LocatedFileStatus file=files.next();
String isDir=file.isDirectory() ? "文件夹" : "文件";
String permission=file.getPermission().toString();
short replication=file.getReplication();
long length=file.getLen();
String path=file.getPath().toString();
System.out.println(isDir + "\t" + permission
+ "\t" + replication + "\t" + length
+ "\t" + path
);
}
}
/*
* 查看文件块信息(在大型分布式环境中,文件的分布以及节点数)
* */
@Test
public void getFileBlockLocation() throws Exception{
FileStatus fileStatus=fileSystem.getFileStatus(new Path("/hdfsapi/test/dg.zip"));
BlockLocation[] blocks=fileSystem.getFileBlockLocations(fileStatus,0,fileStatus.getLen());
for(BlockLocation block:blocks){
for (String name : block.getNames()){
System.out.println(name + " : " + block.getOffset() + " : " + block.getLength() + block.getHosts());
}
}
}
/*
* 删除文件
* */
@Test
public void delete() throws Exception{
boolean result=fileSystem.delete(new Path("/hdfsapi/output/wc.out"),true);//第二个参数表示递归调用开关
System.out.println(result);
}
/*
* 查看副本系数
* */
@Test
public void testReplication(){
System.out.println(configuration.get("dfs.replication"));//new出来一个空的configuration执行默认的dfs.replication三个节点
}
@After
public void tearDown(){
configuration=null;
fileSystem=null;
System.out.print("--------tearDown--------");
}
/* public static void main(String[] args) throws Exception{
//hadoop000:8020服务器端口号
Configuration configuration=new Configuration();
FileSystem fileSystem= FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration, "jerry");
//注意服务器名为hadoop000名和用户名为jerry
Path path=new Path("/hdfsapi/test");
boolean result= fileSystem.mkdirs(path);
}*/
}