「 Hadoop」使用FileSystem API操作HDFS文件
HDFS的操作可以使用shell,也可以使用FileSystem API,上篇文章记录了如何在windows下使用idea编写和调试hadoop的代码,原文链接:http://www.ptbird.cn/intellij-idea-for-hadoop-programming.html
这篇文章详细记录使用Hadoop的FileSystem API操作HDFS文件系统的代码。
Hadoop-2.7.1 API 文档地址:http://hadoop.apache.org/docs/r2.7.1/api/index.html
代码及注释如下:
为了图方便,写了两个静态的成员,一个是当前active的namenode的hdfs地址,另外一个是方法,获得hdfs文件系统(从过Configuration这个类)
//hdfs当前活跃的namenode url
//public static String hdfsUrl="hdfs://node1.hadoop.ptbird.cn:8020";
public static String hdfsUrl="hdfs://node2.hadoop.ptbird.cn:8020";
//获取hdfs的句柄
public static FileSystem getHdfs()throws Exception{
//获取配置文件信息
Configuration conf=new Configuration();
//获取文件系统
FileSystem hdfs=FileSystem.get(URI.create(hdfsUrl),conf);
return hdfs;
}
1、读取文件
====
参数是文件的地址,如:/test/input/data/log1.log。只需要根目录开始的地址就可以了。
//读取文件信息
public static void testRead(Path p) throws Exception{
FileSystem hdfs=getHdfs();
//打开文件流
FSDataInputStream inStream=hdfs.open(p);
//读取文件内容到控制台显示
IOUtils.copyBytes(inStream, System.out,4096,false);
//关闭文件流
IOUtils.closeStream(inStream);
}
2、写入新的文件和内容
====
- 直接在文件中写了,没有设置参数
- 如果文件已经存在,则跑出异常
//写入文件
public static void testWrite(Path p)throws Exception{
FileSystem hdfs = getHdfs();
//创建文件输出流
FSDataOutputStream fsOutStream=hdfs.create(p);
//写入数据通过byte
fsOutStream.write("Hello HDFS Writen".getBytes());
//通过UTF写入
fsOutStream.writeUTF("Hello HDFS Write by UTFn");
//关闭
IOUtils.closeStream(fsOutStream);
}
3、删除文件
====
//删除文件
public static Boolean testDel(Path p)throws Exception{
FileSystem hdfs = getHdfs();
//删除文件
return hdfs.delete(p,true);
}
4、将本地文件上传到hadoop中
====
//上传文件
public static void testUpload(Path localPath,Path hdfsPath)throws Exception{
FileSystem hdfs = getHdfs();
hdfs.copyFromLocalFile(false,true,localPath,hdfsPath);
}
5、重命名文件
=====
//重命名文件
public static Boolean testRename(Path p1, Path p2)throws Exception{
FileSystem hdfs = getHdfs();
return hdfs.rename(p1,p2);
}
6、获取文件目录
====
包括文件夹和文件
//获取文件目录
public static void testList(Path path)throws Exception{
FileSystem hdfs=getHdfs();
FileStatus[] fileStatuses=hdfs.listStatus(path);
for(FileStatus fs:fileStatuses){
Path p=fs.getPath();
String info = fs.isDirectory()?"Dir: ":"File: ";
System.out.println(info+" : "+p);
}
}
7、一个main方法对各个方法的测试
====
public static void main(String args[])throws Exception{
Path path=new Path("/test/output/temp/");
// Path path=new Path("/test/output/wc/");
testWrite(path);
testRead(path);
if(testDel(path)){
System.out.println("delete success!");
}else{
System.out.println("delete failed!");
}
Path localPath=new Path("E:/resFile.txt");
Path hdfsPath=new Path("/test/input/");
testUpload(localPath,hdfsPath);
testRead(new Path("/test/input/wc/test1.log"));
Path prePath=new Path("/test/input/resFile.txt");
Path resPath=new Path("/test/input/filelist.txt");
if(testRename(prePath,resPath)){
System.out.println("rename "+prePath+" to "+resPath+" success!");
}else{
System.out.println("rename "+prePath+" to "+resPath+" failed!");
}
Path listPath=new Path("/test/input");
testList(listPath);
}
8、完整代码示例
git@OSC地址:https://git.oschina.net/postbird/codes/5n2ar709voj8ehcfzgyxt56
文章版权:Postbird-There I am , in the world more exciting!
本文链接:http://www.ptbird.cn/hdfs-filesystem-api.html
转载请注明文章原始出处 !
扫描二维码,在手机阅读!