HDFS 使用java api实现上传/下载/删除文件

来源:互联网 发布:zip解压软件下载 mac 编辑:程序博客网 时间:2024/05/01 06:24
package hadoop;import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HDFSTest01 {/** * 文件上传 * @param src  * @param dst * @param conf * @return */public static boolean put2HSFS(String src , String dst , Configuration conf){Path dstPath = new Path(dst) ;try{FileSystem hdfs = dstPath.getFileSystem(conf) ;//FileSystem hdfs = FileSystem.get( URI.create(dst), conf) ;hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;}catch(IOException ie){ie.printStackTrace() ;return false ;}return true ;}/** * 文件下载 * @param src * @param dst * @param conf * @return */public static boolean getFromHDFS(String src , String dst , Configuration conf){Path dstPath = new Path(dst) ;try{FileSystem dhfs = dstPath.getFileSystem(conf) ;dhfs.copyToLocalFile(false, new Path(src), dstPath) ;}catch(IOException ie){ie.printStackTrace() ;return false ;}return true ;}/** * 文件检测并删除 * @param path * @param conf * @return */public static boolean checkAndDel(final String path , Configuration conf){Path dstPath = new Path(path) ;try{FileSystem dhfs = dstPath.getFileSystem(conf) ;if(dhfs.exists(dstPath)){dhfs.delete(dstPath, true) ;}else{return false ;}}catch(IOException ie ){ie.printStackTrace() ;return false ;}return true ;}/** * @param args */public static void main(String[] args) {//String src = "hdfs://xcloud:9000/user/xcloud/input/core-site.xml" ;String dst = "hdfs://xcloud:9000/user/xcloud/out" ;String src = "/home/xcloud/cdh3/hbase-0.90.4-cdh3u2/bin/loadtable.rb" ;boolean status = false ;Configuration conf = new Configuration() ;status = put2HSFS( src ,  dst ,  conf) ;System.out.println("status="+status) ;src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;dst = "/tmp/output" ;status = getFromHDFS( src ,  dst ,  conf) ;System.out.println("status="+status) ;src = "hdfs://xcloud:9000/user/xcloud/out/loadtable.rb" ;dst = "/tmp/output/loadtable.rb" ;status = checkAndDel( dst ,  conf) ;System.out.println("status="+status) ;}}


参考:

http://www.360doc.com/content/11/0406/18/11586_107636584.shtml

hadoop-0.20_程式设计.pdf


原创粉丝点击