hdfs深入:10、hdfs的javaAPI操作
/**
* 递归遍历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();
}
hdfs深入:10、hdfs的javaAPI操作的更多相关文章
- 使用javaAPI操作hdfs
欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...
- HDFS文件系统的JAVA-API操作(一)
使用java.net.URL访问HDFS文件系统 HDFS的API使用说明: 1.如果要访问HDFS,HDFS客户端必须有一份HDFS的配置文件 也就是hdfs-site.xml,从而读取Nameno ...
- Linux单机环境下HDFS伪分布式集群安装操作步骤v1.0
公司平台的分布式文件系统基于Hadoop HDFS技术构建,为开发人员学习及后续项目中Hadoop HDFS相关操作提供技术参考特编写此文档.本文档描述了Linux单机环境下Hadoop HDFS伪分 ...
- HDFS命令行及JAVA API操作
查看进程 jps 访问hdfs: hadoop-root:50070 hdfs bash命令: hdfs dfs <1> -help: 显示命令的帮助的信息 <2> - ...
- 初识HDFS(10分钟了解HDFS、NameNode和DataNode)
概览 首先我们来认识一下HDFS, HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.它其实是将一个大文件分成若干块保存在不同服务器的多个节点中.通 ...
- Hadoop HDFS的shell(命令行客户端)操作实例
HDFS的shell(命令行客户端)操作实例 3.2 常用命令参数介绍 -help 功能:输出这个命令参数手册 -ls 功能:显示目录信息 示例: hadoop fs ...
- [bigdata] 使用Flume hdfs sink, hdfs文件未关闭的问题
现象: 执行mapreduce任务时失败 通过hadoop fsck -openforwrite命令查看发现有文件没有关闭. [root@com ~]# hadoop fsck -openforwri ...
- [HDFS Manual] CH3 HDFS Commands Guide
HDFS Commands Guide HDFS Commands Guide 3.1概述 3.2 用户命令 3.2.1 classpath 3.2.2 dfs 3.2.3 envvars 3.2.4 ...
- [HDFS Manual] CH2 HDFS Users Guide
2 HDFS Users Guide 2 HDFS Users Guide 2.1目的 2.2.概述 2.3.先决条件 2.4. Web Interface 2.5. Shell Command 2. ...
- [HDFS Manual] CH1 HDFS体系结构
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
随机推荐
- gevent 协程 使用
Python通过yield提供了对协程的基本支持,但是不完全.而第三方的gevent为Python提供了比较完善的协程支持. gevent是第三方库,通过greenlet实现协程,其基本思想是: 当一 ...
- C++实现斐波那契第N项非递归与递归实现的时间比较
/* * 斐波那契数列.cpp * * Created on: 2018年4月9日 * Author: soyo */ #include<iostream> #include<cti ...
- Linux下 SSH远程管理服务
第1章 SSH基本概述 1.1 SSH服务协议说明 SSH 是 Secure Shell Protocol 的简写,由 IETF 网络工作小组(Network Working Group )制定 在进 ...
- 【BZOJ3625】【CF438E】小朋友和二叉树
题目 传送门 思路&做法 我们可以用\(v_i\)表示\(i\)在\(c\)中出现了几次, 用\(f_i\)表示权值为\(i\)的神犇树的总数, 于是 \[ f_x = \sum_{i = 0 ...
- bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】
算法详见:http://www.cnblogs.com/lokiii/p/8191573.html 求出点两两之间的最小割之后,把他们扔到map/set里跑即可 可怕的是map和set跑的时间竟然完全 ...
- CodeForces E. Goods transportation【最大流+dp最小割】
妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...
- nginx下配置虚拟主机
linux 虚拟机下配置虚拟主机 nginx.conf 文件不动, 在 conf.d 或者 conf 目录下 新建项目.conf server { listen 80; server_name loc ...
- Rabbitmq笔记二
消息何去何从 mandatory 和 immediate 是 channel . basicPublish 方法中的两个参数,它们都有 当消息传递过程中不可达目的地时将消息返回给生产者的功能. 当 m ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- Hdu 5348 MZL's endless loop (dfs)
题目链接: Hdu 5348 MZL's endless loop 题目描述: 给出一个无向图(有环,有重边),包含n个顶点,m条边,问能否给m条边指定方向,使每个顶点都满足abs(出度-入度)< ...