java操作hdfs

来源:互联网 发布:织梦cookies 编辑:程序博客网 时间:2024/05/22 01:18
Hadoop文件系统
基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件。

Java抽象类org.apache.hadoop.fs.FileSystem定义了hadoop的一个文件系统接口。该类是一个抽象类,通过以下两种静态工厂方法可以过去FileSystem实例:
public static FileSystem.get(Configuration conf) throws IOException
public static FileSystem.get(URI uri, Configuration conf) throws IOException


具体方法实现:
1、public boolean mkdirs(Path f) throws IOException
一次性新建所有目录(包括父目录), f是完整的目录路径。

2、public FSOutputStream create(Path f) throws IOException
创建指定path对象的一个文件,返回一个用于写入数据的输出流
create()有多个重载版本,允许我们指定是否强制覆盖已有的文件、文件备份数量、写入文件缓冲区大小、文件块大小以及文件权限。

3、public boolean copyFromLocal(Path src, Path dst) throws IOException
将本地文件拷贝到文件系统

4、public boolean exists(Path f) throws IOException
检查文件或目录是否存在

5、public boolean delete(Path f, Boolean recursive)
永久性删除指定的文件或目录,如果f是一个空目录或者文件,那么recursive的值就会被忽略。只有recursive=true时,一个非空目录及其内容才会被删除。

6、FileStatus类封装了文件系统中文件和目录的元数据,包括文件长度、块大小、备份、修改时间、所有者以及权限信息。
通过"FileStatus.getPath()"可查看指定HDFS中某个目录下所有文件。



Java代码  收藏代码
  1. package hdfsTest;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.fs.FSDataOutputStream;  
  7. import org.apache.hadoop.fs.FileStatus;  
  8. import org.apache.hadoop.fs.FileSystem;  
  9. import org.apache.hadoop.fs.Path;  
  10.   
  11. public class OperatingFiles {  
  12.     //initialization  
  13.     static Configuration conf = new Configuration();  
  14.     static FileSystem hdfs;  
  15.     static {  
  16.         String path = "/usr/java/hadoop-1.0.3/conf/";  
  17.         conf.addResource(new Path(path + "core-site.xml"));  
  18.         conf.addResource(new Path(path + "hdfs-site.xml"));  
  19.         conf.addResource(new Path(path + "mapred-site.xml"));  
  20.         path = "/usr/java/hbase-0.90.3/conf/";  
  21.         conf.addResource(new Path(path + "hbase-site.xml"));  
  22.         try {  
  23.             hdfs = FileSystem.get(conf);  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.       
  29.     //create a direction  
  30.     public void createDir(String dir) throws IOException {  
  31.         Path path = new Path(dir);  
  32.         hdfs.mkdirs(path);  
  33.         System.out.println("new dir \t" + conf.get("fs.default.name") + dir);  
  34.     }     
  35.       
  36.     //copy from local file to HDFS file  
  37.     public void copyFile(String localSrc, String hdfsDst) throws IOException{  
  38.         Path src = new Path(localSrc);        
  39.         Path dst = new Path(hdfsDst);  
  40.         hdfs.copyFromLocalFile(src, dst);  
  41.           
  42.         //list all the files in the current direction  
  43.         FileStatus files[] = hdfs.listStatus(dst);  
  44.         System.out.println("Upload to \t" + conf.get("fs.default.name") + hdfsDst);  
  45.         for (FileStatus file : files) {  
  46.             System.out.println(file.getPath());  
  47.         }  
  48.     }  
  49.       
  50.     //create a new file  
  51.     public void createFile(String fileName, String fileContent) throws IOException {  
  52.         Path dst = new Path(fileName);  
  53.         byte[] bytes = fileContent.getBytes();  
  54.         FSDataOutputStream output = hdfs.create(dst);  
  55.         output.write(bytes);  
  56.         System.out.println("new file \t" + conf.get("fs.default.name") + fileName);  
  57.     }  
  58.       
  59.     //list all files  
  60.     public void listFiles(String dirName) throws IOException {  
  61.         Path f = new Path(dirName);  
  62.         FileStatus[] status = hdfs.listStatus(f);  
  63.         System.out.println(dirName + " has all files:");  
  64.         for (int i = 0; i< status.length; i++) {  
  65.             System.out.println(status[i].getPath().toString());  
  66.         }  
  67.     }  
  68.   
  69.     //judge a file existed? and delete it!  
  70.     public void deleteFile(String fileName) throws IOException {  
  71.         Path f = new Path(fileName);  
  72.         boolean isExists = hdfs.exists(f);  
  73.         if (isExists) { //if exists, delete  
  74.             boolean isDel = hdfs.delete(f,true);  
  75.             System.out.println(fileName + "  delete? \t" + isDel);  
  76.         } else {  
  77.             System.out.println(fileName + "  exist? \t" + isExists);  
  78.         }  
  79.     }  
  80.   
  81.     public static void main(String[] args) throws IOException {  
  82.         OperatingFiles ofs = new OperatingFiles();  
  83.         System.out.println("\n=======create dir=======");  
  84.         String dir = "/test";  
  85.         ofs.createDir(dir);  
  86.         System.out.println("\n=======copy file=======");  
  87.         String src = "/home/ictclas/Configure.xml";  
  88.         ofs.copyFile(src, dir);  
  89.         System.out.println("\n=======create a file=======");  
  90.         String fileContent = "Hello, world! Just a test.";  
  91.         ofs.createFile(dir+"/word.txt", fileContent);  
  92.     }  
  93. }  




//output~
=======create dir=======
new dir hdfs://sfserver20.localdomain:9000/test

=======copy file=======
Upload to hdfs://sfserver20.localdomain:9000/test
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/word.txt

=======create file=======
new file hdfs://sfserver20.localdomain:9000/test/word.txt

=======before delete file=======
new dir hdfs://sfserver20.localdomain:9000/test/del
new file hdfs://sfserver20.localdomain:9000/test/del.txt
/test has all files:
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/del
hdfs://sfserver20.localdomain:9000/test/del.txt
hdfs://sfserver20.localdomain:9000/test/word.txt

=======after delete file=======
/test/del  delete? true
/test/del.txt  delete? true
/test/no  exist? false
/test has all files:
hdfs://sfserver20.localdomain:9000/test/Configure.xml
hdfs://sfserver20.localdomain:9000/test/word.txt
0 0
原创粉丝点击