1. 概述
2. 文件操作
2.1上传本地文件到hadoop fs
2.2 在hadoop fs中新建文件,并写入
2.3 删除hadoop fs上的文件
2.4读取文件
3. 目录操作
3.1 在hadoop fs上创建目录
3.2 删除目录
3.3 读取某个目录下的所有文件
4. 参考资料接代码下载
<1>. 概述
hadoop中关于文件操作类基本上全部是在org.apache.hadoop.fs包中,这些api能够支持的操作包含:打开文件,读写文件,删除文件等。
hadoop类库中最终面向用户提供的接口类是FileSystem,该类是个抽象类,只能通过来类的get方法得到具体类。get方法存在几个重载版本,常用的是这个:
static FileSystem get(Configuration conf);
该类封装了几乎所有的文件操作,例如mkdir,delete等。综上基本上可以得出操作文件的程序库框架:
operator()
{
得到Configuration对象
得到FileSystem对象
进行文件操作
}
另外需要注意的是,如果想要运行下面的程序的话,需要将程序达成jar包,然后通过hadoop jar的形式运行,这种方法比较麻烦,另外一种方法就是安装eclipse的hadoop插件,这样能够很多打包的时间。
<1>. 文件操作
1.1 上传本地文件到文件系统
/*
*uploadthelocalfiletothehds
*noticethatthepathisfulllike/tmp/test.c
*/
publicstaticvoiduploadLocalFile2HDFS(Strings,Stringd)
throwsIOException
{
Configurationconfig=newConfiguration();
FileSystemhdfs=FileSystem.get(config);
Pathsrc=newPath(s);
Pathdst=newPath(d);
hdfs.copyFromLocalFile(src,dst);
hdfs.close();
}
1.2 创建新文件,并写入
/*
*createanewfileinthehdfs.
*noticethatthetoCreateFilePathisthefullpath
*andwritethecontenttothehdfsfile.
*/
publicstaticvoidcreateNewHDFSFile(StringtoCreateFilePath,Stringcontent)throwsIOException
{
Configurationconfig=newConfiguration();
FileSystemhdfs=FileSystem.get(config);
FSDataOutputStreamos=hdfs.create(newPath(toCreateFilePath));
os.write(content.getBytes("UTF-8"));
os.close();
hdfs.close();
}
1.3 删除文件
/*
*deletethehdfsfile
*noticethatthedstisthefullpathname
*/
publicstaticbooleandeleteHDFSFile(Stringdst)throwsIOException
{
Configurationconfig=newConfiguration();
FileSystemhdfs=FileSystem.get(config);
Pathpath=newPath(dst);
booleanisDeleted=hdfs.delete(path);
hdfs.close();
returnisDeleted;
}
1.4 读取文件
/**readthehdfsfilecontent
*noticethatthedstisthefullpathname
*/
publicstaticbyte[]readHDFSFile(Stringdst)throwsException
{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
//checkifthefileexists
Pathpath=newPath(dst);
if(fs.exists(path))
{
FSDataInputStreamis=fs.open(path);
//getthefileinfotocreatethebuffer
FileStatusstat=fs.getFileStatus(path);
//createthebuffer
byte[]buffer=newbyte[Integer.parseInt(String.valueOf(stat.getLen()))];
is.readFully(0,buffer);
is.close();
fs.close();
returnbuffer;
}
else
{
thrownewException("thefileisnotfound.");
}
}
<2>. 目录操作
2.1 创建目录
/**makeanewdirinthehdfs
*
*thedirmaylike'/tmp/testdir'
*/
publicstaticvoidmkdir(Stringdir)throwsIOException
{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
fs.mkdirs(newPath(dir));
fs.close();
}
2.2 删除目录
/**deleteadirinthehdfs
*
*dirmaylike'/tmp/testdir'
*/
publicstaticvoiddeleteDir(Stringdir)throwsIOException
{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
fs.delete(newPath(dir));
fs.close();
}
2.3 读取某个目录下的所有文件
publicstaticvoidlistAll(Stringdir)throwsIOException
{
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
FileStatus[]stats=fs.listStatus(newPath(dir));
for(inti=0;i<stats.length;++i)
{
if(stats[i].isFile())
{
//regularfile
System.out.println(stats[i].getPath().toString());
}
elseif(stats[i].isDirectory())
{
//dir
System.out.println(stats[i].getPath().toString());
}
elseif(stats[i].isSymlink())
{
//isssymlinkinlinux
System.out.println(stats[i].getPath().toString());
}
}
fs.close();
}
<4>. 参考资料及代码下载
/Files/xuqiang/HadoopFSOperations.rar
1. 本博客中的文章均是个人在学习和项目开发中总结。其中难免存在不足之处 ,欢迎留言指正。
2. 本文版权归作者和博客园共有,转载时,请保留本文链接。
分享到:
相关推荐
NULL 博文链接:https://anna66.iteye.com/blog/1605502
java操作Hadoop源码之HDFS Java API操作-上传文件,附带所需全部jar包,欢迎下载学习。
该资源是java通过api的方式来操作hadoop,主要有以下操作: 一.文件操作 1.上传本地文件到hadood 2.在hadoop中新建文件,并写入 3.删除hadoop上的文件 4.读取文件 5.文件修改时间 二.目录操作 1.在hadoop上创建目录 ...
java操作hadoop之mapreduce计算整数的最大值和最小值实战源码,附带全部所需jar包,欢迎下载一起学习。
ecplise远程连接hadoop--hdfs java api操作文件.pdf
在搭建完hadoop大数据系统(CDH5.16.1)后,如何访问hdfs文件系统上的数据呢?那当然是通过构建maven项目 使用java api接口进行文件了。为此,特别进行了hdfs文件系统java api访问的整理。
标签:apache、hadoop、api、yarn、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
标签:apache、client、hadoop、yarn、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译...
1.java接口操作Hadoop文件系统(文件上传下载删除创建...可举一反三) 2.RPC远程过程调用的java代码实现,便于理解Hadoop的RPC协议,具体使用方法可参考我的博客...
标签:apache、client、hadoop、yarn、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和...
凡是hadoop2.x以上版本都可以使用这个导入包,下载后直接将里面的所有文件直接导入到需要使用hadoop接口的项目里就好
标签:core、apache、mapreduce、client、hadoop、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...
标签:apache、hadoop、api、yarn、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,...
标签:apache、common、hadoop、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...
赠送jar包:hadoop-common-2.9.1.jar ...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
Windows下配置Hadoop的Java开发环境以及用Java API操作HDFS: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119379055
在开发工具中(例如Eclipse)使用Java语言开发Hadoop程序,调用API,可以在Windows环境运行。配置步骤大致是1 解压缩,2 配置Windows的...简单说就是在windows系统里面编写能够使用HADOOP API的Java代码所需要的文件。
标签:apache、common、hadoop、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...
标签:apache、hadoop、yarn、api、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
标签:apache、hdfs、hadoop、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...