/**
* 递归遍历hdfs中所有的文件路径
*/
@Test
public void getAllHdfsFilePath() throws URISyntaxException, IOException {
//获取fs的客户端
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration()); Path path = new Path("/");
FileStatus[] fileStatuses = fileSystem.listStatus(path); //循环遍历fileStatuses,如果是文件,打印文件的路径,如果是文件夹,继续递归进去
for (FileStatus fileStatus : fileStatuses){
if (fileStatus.isDirectory()){//文件夹
getDirectoryFiles(fileSystem,fileStatus);
}else{ //文件
System.out.println(fileStatus.getPath().toString());
}
} //方法二:
System.out.println("方法二:利用官方提供API");
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true); while (locatedFileStatusRemoteIterator.hasNext()){
LocatedFileStatus next = locatedFileStatusRemoteIterator.next();
System.out.println(next.getPath());
} //关闭fs的客户端
fileSystem.close();
} /**
* 递归获取文件路径
*/
public void getDirectoryFiles(FileSystem fileSystem,FileStatus fileStatus) throws IOException {
//通过fileStatus获取文件夹路径
Path path = fileStatus.getPath(); //该fileStatus必定为一个文件夹
FileStatus[] fileStatuses = fileSystem.listStatus(path);
for (FileStatus status:fileStatuses){
if (fileStatus.isDirectory()){
getDirectoryFiles(fileSystem,status);
}else{
System.out.println(fileStatus.getPath().toString());
}
}
} /**
* 下载hdfs文件到本地
*/
@Test
public void copyHdfsToLocal() throws Exception { FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration()); FSDataInputStream inputStream = fileSystem.open(new Path("hdfs://node01:8020/aa/haha2.txt")); FileOutputStream outputStream = new FileOutputStream(new File("d:\\install-log.txt")); IOUtils.copy(inputStream,outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream); //方法二:利用官方API
//有报错:java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.createFileWithMode0(Ljava/lang/String;JJJI)Ljava/io/FileDescriptor;
fileSystem.copyToLocalFile(new Path("hdfs://node01:8020/aa/haha2.txt"),new Path("file:///d:\\install-log2.txt")); fileSystem.close();
} /**
* hdfs上面创建文件夹
*/
@Test
public void createHdfsDir() throws Exception{
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
fileSystem.mkdirs(new Path("/aa/bb/cc/"));
fileSystem.close();
} /**
* hdfs的文件上传
*/
@Test
public void uploadFileToHdfs() throws Exception{
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
//注:new Path()中的字符串参数如果省略file:///或hdfs://的话,默认会在参数前添加hdfs://node01:8020,即,默认是hdfs路径
fileSystem.copyFromLocalFile(false,new Path("file:///d:\\output.txt"),new Path("/aa/bb/cc")); //第二种方法:通过流的方式
//输出流,负责将数据输出到hdfs的路径上面去
FSDataOutputStream outputStream = fileSystem.create(new Path("/aa/bb/cc/empSel.hdfs"));
//通过输入流读取本地文件系统的文件
InputStream inputStream = new FileInputStream(new File("d:\\empSel.txt"));
IOUtils.copy(inputStream,outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
fileSystem.close();
} /**
* hdfs的权限校验机制
*/
@Test
public void hdfsPermission() throws Exception{
/*
在所有节点的hdfs-site.xml中设置开启权限验证:
<property>
<name>dfs.permissions</name>
<value>true</value>
</property>
普通的filesystem,执行时会报错:org.apache.hadoop.security.AccessControlException:
Permission denied: user=Administrator, access=READ, inode="/config/core-site.xml":root:supergroup:-rw-------
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
*/
//通过伪造用户来获取分布式文件系统的客户端
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration(), "root");
//从hdfs上下载文件到本地
FSDataInputStream inputStream = fileSystem.open(new Path("/config/core-site.xml"));
FileOutputStream outputStream = new FileOutputStream(new File("d:\\core-site.txt"));
IOUtils.copy(inputStream,outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
// fileSystem.copyFromLocalFile(new Path("file:///d:\\transferIndex.txt"),new Path("/aa/bb/cc/"));
// fileSystem.delete(new Path("/aa/bb/cc/"),false);
fileSystem.close();
} /**
* hdfs在上传小文件的时候进行合并
* 在我们的hdfs 的shell命令模式下,可以通过命令行将很多的hdfs文件合并成一个大文件下载到本地:
* hdfs dfs -getmerge /config/*.xml ./hello.xml
* 上传时也能将小文件合并到一个大文件里面去。
*/
@Test
public void mergeFile()throws Exception{
//获取分布式文件系统
FileSystem fileSystem = FileSystem.get(new URI("hdfs://192.168.8.100:8020"), new Configuration(),"root");
FSDataOutputStream outputStream = fileSystem.create(new Path("/bigFile.xml")); //获取本地所有小文件的输入流
//首先获取本地文件系统
LocalFileSystem localFileSystem = FileSystem.getLocal(new Configuration());
FileStatus[] fileStatuses = localFileSystem.listStatus(new Path("file:///D:\\上传小文件合并"));
for (FileStatus fileStatus:fileStatuses){
Path path = fileStatus.getPath();
FSDataInputStream fsDataInputStream = localFileSystem.open(path);
IOUtils.copy(fsDataInputStream,outputStream);
IOUtils.closeQuietly(fsDataInputStream);
}
IOUtils.closeQuietly(outputStream);
fileSystem.close();
localFileSystem.close();
}

最新文章

  1. git pull push 不用输入用户名和密码的方法
  2. JavaScript-Function基础知识
  3. Java部署_IntelliJ创建一个可运行的jar包(实践)
  4. 免费而优秀的图表JS插件
  5. .Net魔法堂:开启IIS的WebGarden、WebFarm和StateServer之旅
  6. HTTP Status Code [RFC]
  7. C语言 百炼成钢12
  8. MFC之目录结构及消息流转(一)
  9. 灵活控制 Hibernate 的日志或 SQL 输出(包含参数),以便于诊断
  10. AIX系统上压缩与解压文件
  11. 解决MacOS Terminal打开慢的问题
  12. Python自动化环境搭建
  13. LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
  14. javascript中对字符串的操作总结
  15. Mysql获取去重后的总数
  16. 数组a和&amp;a区别
  17. Android POJO 转换器 —&gt; RapidOOO
  18. Kafka安装配置
  19. 【第一次作业】&amp;&amp;软件工程大一班---甘昀
  20. 【译】第15节---数据注解-StringLength

热门文章

  1. BZOJ_2081_[Poi2010]Beads_哈希
  2. hdu4507(数位DP)
  3. IDC 内网机器 通 过 iptables SNAT上网的配置方法
  4. java中static,super,final关键字辨析
  5. bzoj 3028: 食物【生成函数】
  6. bzoj 2618【半平面交模板】
  7. thinkphp 中 dump 函数调试数组时显示不全解决方法
  8. 【SpringCloud构建微服务系列】学习断路器Hystrix
  9. 拓扑排序/DFS HDOJ 4324 Triangle LOVE
  10. DFS Codeforces Round #299 (Div. 2) B. Tavas and SaDDas