package day01;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;
public class hdfsDemo {
FileSystem fs = null;
Configuration conf = null;
/**
* 初始化一个hdfs FileSystem
*
* @throws Exception
*/
@Before
public void init() throws Exception {
conf = new Configuration();
conf.set("dfs.replication", "5");
fs = FileSystem.get(new URI("hdfs://myspark:9000"), conf, "sparkuser");
}
/**
* 上传文件到hdfs
*
* @throws Exception
*/
@Test
public void testUpload() throws Exception {
fs.copyFromLocalFile(new Path("E:/data/access.log"), new Path("/hdfs_test"));
fs.close();
}
/**
* 从hdfs下载一个文件到本地
*
* @throws Exception
*/
@Test
public void testDownload() throws Exception {
fs.copyToLocalFile(new Path("/hdfs_test/access.log"), new Path("E:/data/"));
fs.close();
}
/**
* 获取配置信息并打印
*/
@Test
public void showConf() {
Iterator<Entry<String, String>> iterator = conf.iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
System.out.println(entry.getKey() + "---" + entry.getValue());
}
}
/**
* 创建hdfs目录
*
* @throws Exception
*/
@Test
public void testMkDir() throws Exception {
fs.mkdirs(new Path("/abc/cba"));
fs.close();
}
/**
* 删除hdfs目录或文件
*
* @throws Exception
*/
@Test
public void testdel() throws Exception {
fs.delete(new Path("/hdfs_test/access.log"));
fs.close();
}
/**
* 列出hdfs的文件路径、文件名
* listStatus和listFiles的使用
* @throws Exception
*/
@Test
public void testList() throws Exception {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
System.err.println(fileStatus.getPath() + "++++" + fileStatus.toString());
}
RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);
while (remoteIterator.hasNext()) {
LocatedFileStatus next = remoteIterator.next();
String name = next.getPath().getName();
Path path = next.getPath();
System.out.println(name + "---" + path.toString());
}
}
}