hdfs的javaAPI使用
参考博客:https://www.cnblogs.com/Eddyer/p/6641778.html
1.需求
使用hdfs的javaAPI访问hdfs系统。
2.环境配置
(1)hadoop为本地模式
(2)pom文件代码如下
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
3.使用hdfs的javaAPI操作hdfs的代码
(1)初始化环境
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class hadoopJavaApiDemo {
FileSystem fs = null;
@Before
public void init() throws IOException, URISyntaxException, InterruptedException {
Configuration configuration = new Configuration();
//设置文件系统为hdfs
//获取文件系统的客户端实例对象
//注意:运行程序是要执行用户名为hadoop,否则会出现没写权限的情况
fs = FileSystem.get(new URI("hdfs://127.0.0.1:9900"),configuration,"hadoop");
}
//...
}
(2)上传文件
@Test
public void testUpload() throws IOException {
fs.copyFromLocalFile(new Path("file://[本地文件的路径,例如/a/b.txt]"),new Path("[hdfs文件系统的路径,例如/]"));
fs.close();
}
(3)下载文件
@Test
public void testDownLoad() throws IOException {
fs.copyToLocalFile(new Path("[hdfs上的文件路径,例如/testData]"),new Path("[本地文件路径,例如/home/a.txt]"));
fs.close();
}
(4)创建文件夹
@Test //创建新的文件夹 public void testMakeDir() throws Exception { boolean mkdirs = fs.mkdirs(new Path("/x/y/z")); System.out.println(mkdirs); }
(5)删除文件夹
@Test
public void testDelete() throws Exception{
//第二个参数为true是递归删除
boolean delete = fs.delete(new Path("/x"), true);
System.out.println(delete);
}
(6)修改文件夹名称
@Test
//测试修改文件夹的名称
public void testRenameDir() throws IOException {
boolean change = fs.rename(new Path("/x"),new Path("/xx"));
System.out.println(change);
}
(7)查看目录信息
@Test
//测试查看目录信息功能
public void testListFiles() throws IOException {
//第二个参数为true表示递归调用
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"),true);
while (listFiles.hasNext()){
//打印各种文件信息,和命令行中执行 hdfs dfs -lsr / 效果一样
LocatedFileStatus status = listFiles.next();
System.out.println(status.getPath().getName());
System.out.println(status.getBlockSize());
System.out.println(status.getPermission());
System.out.println(status.getLen());
//打印文件的每个分组位置,每128M一个分组
BlockLocation[] blockLocations = status.getBlockLocations();
for(BlockLocation bl:blockLocations){
System.out.println("block-length:"+bl.getLength()+"--"+
"block-offset:"+bl.getOffset());
String[] hosts = bl.getHosts();
for(String host:hosts){
System.out.println(host);
}
}
System.out.println();
}
}
(8)查看文件以及文件夹信息
@Test
//查看文件以及文件夹信息
public void testListAll() throws IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
String flag = "d-----------------";
for(FileStatus fileStatus:listStatus){
if(fileStatus.isFile())
flag = "f----------------";
else
flag = "d----------------";
System.out.println(flag+fileStatus.getPath().getName());
}
}
4.注意事项
(1)hdfs各种端口的含义(就因为将127.0.0.1:9900写为127.0.0.1:50070,debug了好久。。)
9900是fileSystem的端口号(默认是9000,这里我自定义为9900)
50070是namenode主节点的端口号
50090是namenode的secondarynamenode的端口号
(2)file://是一个传输协议
比如可以通过在浏览器中输入路径file:///a/b/c.txt访问本地文件
5.github链接
https://github.com/gulu2016/STBigData/hadoopJavaApiDemo.java