package com.bpf.hdfs;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsClient {
FileSystem fs = null;
Configuration conf = null;
//初始化,建立与HDFS系统的连接
@Before
public void init() throws Exception {
conf = new Configuration();
fs = FileSystem.get(new URI("hdfs://Master:9000"), conf);
}
//上传文件
@Test
public void testUpload() throws IllegalArgumentException, IOException {
fs.copyFromLocalFile(new Path("E:/QQPCmgr/Desktop/c.docx"), new Path("/c_copy.dox"));
}
//下载文件
@Test
public void testDownload() throws IllegalArgumentException, IOException {
fs.copyToLocalFile(new Path("/c_copy.dox"), new Path("E:/QQPCmgr/Desktop/b.docx"));
}
//获取并显示相关配置信息
@Test
public void testConf() {
Iterator<Entry<String, String>> iterator = conf.iterator();
while(iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey()+" : "+entry.getValue());
}
}
//创建文件夹
@Test
public void testMkdir() throws IllegalArgumentException, IOException {
fs.mkdirs(new Path("/666/222"));
}
//删除文件夹
@Test
public void testDelete() throws IllegalArgumentException, IOException {
fs.delete(new Path("/666/222"),true);
}
//显示HDFS内的相关文件存储信息
@Test
public void testLS1() throws FileNotFoundException, IllegalArgumentException, IOException {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println(fileStatus.getPath().getName());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
System.out.println("块起始偏移量:" + blockLocation.getOffset());
System.out.println("块长度:" + blockLocation.getLength());
String[] hosts = blockLocation.getHosts();
for (String string : hosts) {
System.out.println("datanode:" + string);
}
}
}
}
//同上
@Test
public void testLS2() throws FileNotFoundException, IllegalArgumentException, IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
System.out.println(fileStatus.toString());
}
}
}
HDFS相关JAVA API
最新推荐文章于 2024-03-30 08:25:40 发布