HDFS文件存取编程
来源:互联网 发布:人工智能书籍 知乎 编辑:程序博客网 时间:2024/06/02 03:59
1、接口说明
Hadoop文件系统由一个namenode节点和N个datanode节点组成,每个结点均是一台普通的计算机。在使用上同我们熟悉的单机上的文件系统非常类似,一样可以建目录,创建,复制,删除文件,查看文件内容等。
客户端要实现对Hadoop文件系统的读写,要配置目标文件的绝对路径,来连接Hadoop文件系统,如“hdfs://10.191.1.1:54310/user/hdfs/testdir/test.txt”,接着通过FileSystem的get方法获得对目标文件的抽象引用。
对于Hadoop文件系统中的文件的访问是基于InputStream和OutputStream的流式访问,其访问方法如下所示。
String hdfsFileName = _hdfsFileName;
String hdfsFullPathFile = hdfsSrvAddr + hdfsDownloadPath + hdfsFileName;
InputStream hdfsInStream = null;
FileSystem fs = FileSystem.get(URI.create(hdfsFullPathFile), conf);
hdfsInStream = fs.open(new Path(hdfsFullPathFile));
访问文件系统一些常用的类及其功能如下所示:
Hadoop类
功能
org.apache.hadoop.fs.FileSystem
一个通用文件系统的抽象基类,可以被分布式文件系统继承。所有的可能使用Hadoop文件系统的代码都要使用到这个类。。
org.apache.hadoop.fs.FileStatus
客户端可见的文件状态信息。
org.apache.hadoop.fs.FSDataInputStream
文件输入流,用于读取Hadoop文件。
org.apache.hadoop.fs.FSDataOutputStream
文件输出流,用于写Hadoop文件。
org.apache.hadoop.fs.permission.FsPermission
文件或者目录的权限
org.apache.hadoop.conf.Configuration
访问配置项。所有的配置项的值,如果没有专门配置,以core-default.xml为准;否则,以core-site.xml中的配置为准。
2、开发步骤
在Windows客户端编写JAVA程序操作HBase,需要引入的JAR如下:hadoop-0.20.1-core.jar,commons-logging-1.0.4.jar,commons-logging-api-1.0.4.jar。
2.1上传文件
打开本地上传文件的输入流,同时以创建方式打开Hadoop文件的输出流,将输入流按字节读取出来,写入输入流即可。
部分实现代码如下:
InputStream in = new BufferedInputStream(new FileInputStream(localSrcFile));
FileSystem fs = FileSystem.get(URI.create(hdfsDstFile), conf);
OutputStream out = fs.create(new Path(hdfsDstFile));
int readLen = in.read(ioBuffer);
while(-1 != readLen){
out.write(ioBuffer, 0, readLen);
uploadBytes += readLen;
readLen = in.read(ioBuffer);
}
2.2下载文件
打开Hadoop文件的输入流,同时以创建方式打开本地下载文件的输出流,将输入流按字节读取出来,写入输入流即可。
部分代码如下:
FileSystem fs = FileSystem.get(URI.create(hdfsFullPathFile), conf);
hdfsInStream = fs.open(new Path(hdfsFullPathFile));
OutputStream out = new FileOutputStream(localDstFile);
int readLen = hdfsInStream.read(ioBuffer);
while(-1 != readLen){
out.write(ioBuffer, 0, readLen);
downloadBytes += readLen;
readLen = hdfsInStream.read(ioBuffer);
}
2.3更新文件
以附加模式打开Hadoop文件的输出流,将输入流按字节读取出来,写入输入流即可。
文件更新,需要在hdfs-site.xml中添加
</property>
<name>dfs.append.support</name>
<value>true</value>
</property>
暂时只实现了在文件尾部附加文本,随机的增删改还未实现。
部分实现代码如下:
FileSystem fs = FileSystem.get(URI.create(hdfsDstFile), conf);
FSDataOutputStream out = fs.append(new Path(hdfsDstFile));
int readLen = inStream.read(ioBuffer);
while(-1 != readLen){
out.write(ioBuffer, 0, readLen);
appendBytes = (appendBytes < 0) ? readLen:(appendBytes + readLen);
readLen = inStream.read(ioBuffer);
}
2.4删除文件
删除文件先判断文件是否存在,如果存在则删除。
部分实现代码如下:
FileSystem fs = FileSystem.get(URI.create(hdfsFile), conf);
fs.deleteOnExit(new Path(hdfsFile));
2.5查看目录
部分实现代码如下:
FileSystem fs = FileSystem.get(URI.create(hdfsDirPath), conf);
FileStatus fileList[] = fs.listStatus(new Path(hdfsDirPath));
fileNum = fileList.length;
for(int fileCount = 0; fileCount < fileNum; fileCount++){
System.out.println(fileList[fileCount].getPath().getName() + "/t/t"
+ fileList[fileCount].getLen());
}
- HDFS文件存取编程
- HDFS文件存取编程
- HDFS文件存取编程
- Hadoop:HDFS文件存取机制
- 文件存取编程基础
- 在hdfs上存取xml文件的实现代码
- HDFS文件系统基本文件命令、编程读写HDFS
- 文件存取
- HDFS基本原理及数据存取实战
- HDFS基本原理及数据存取实战
- HDFS文件
- Hadoop 核心编程之 HDFS 的文件操作
- mr编程递归显示hdfs目录下所有文件
- Hadoop文件存储系统-HDFS详解以及java编程实现
- Spark核心编程:创建RDD(集合、本地文件、HDFS文件)
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- 总结一年得失 , 想法片面。
- 数据转字符串
- 2010.3.13~2010.3.22——实车训练前
- 寻找一种可以只安装客户端插件就能很好运行的还还能提高开发效率的技术最好java???
- 转载 一个C#线程池的例子
- HDFS文件存取编程
- 火车进站出站
- 配置DNN站点日志
- Ant构建
- powerbuilder百科介绍
- 在 windows 下安装和简单配置 trac 0.12
- vmware 下linux上网设置
- 简单实用的Code Review工具
- 洪强宁谈豆瓣网技术架构