Java實現遠端HDFS的檔案操作(新建、上傳、下載、刪除)
阿新 • • 發佈:2019-02-08
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 {
/**
* @author dcx by 2015.11.19
* 新建檔案
* @param dsta
* @param conf
* @return
*/
public static boolean CreatDir(String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = FileSystem.get(conf);
dhfs.mkdirs(dstPath);
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
/**
* @author dcx by 2015.11.19
* 檔案上傳
* @param src
* @param dst
* @param conf
* @return
*/
public static boolean putToHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem hdfs = dstPath.getFileSystem(conf) ;
hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
/**
* @author dcx by 2015.11.19
* 檔案下載
* @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 ;
}
/**
* @author dcx by 2015.11.19
* 檔案刪除
* @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 主函式測試
*/
public static void main(String[] args) {
boolean status = false ;
String dst1 = "hdfs://192.168.1.225:9000/EBLearn_data/new" ;
Configuration conf = new Configuration() ;
//java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.1.225:9000/EBLearn_data/hello.txt, expected: file:///
//解決這個錯誤的兩個方案:
//方案1:下面這條命令必須加上,否則出現上面這個錯誤
conf.set("fs.default.name", "hdfs://192.168.1.225:9000"); // "hdfs://master:9000"
//方案2: 將core-site.xml 和hdfs-site.xml放入當前工程中
status = CreatDir( dst1 , conf) ;
System.out.println("status="+status) ;
String dst = "hdfs://192.168.1.225:9000/EBLearn_data" ;
String src = "I:/hello.txt" ;
status = putToHDFS( src , dst , conf) ;
System.out.println("status="+status) ;
src = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
dst = "I:/hadoop_need/" ;
status = getFromHDFS( src , dst , conf) ;
System.out.println("status="+status) ;
dst = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
status = checkAndDel( dst , conf) ;
System.out.println("status="+status) ;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFSTest01 {
/**
* @author dcx by 2015.11.19
* 新建檔案
* @param dsta
* @param conf
* @return
*/
public static boolean CreatDir(String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem dhfs = FileSystem.get(conf);
dhfs.mkdirs(dstPath);
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
/**
* @author dcx by 2015.11.19
* 檔案上傳
* @param src
* @param dst
* @param conf
* @return
*/
public static boolean putToHDFS(String src , String dst , Configuration conf){
Path dstPath = new Path(dst) ;
try{
FileSystem hdfs = dstPath.getFileSystem(conf) ;
hdfs.copyFromLocalFile(false, new Path(src), dstPath) ;
}
catch(IOException ie){
ie.printStackTrace() ;
return false ;
}
return true ;
}
/**
* @author dcx by 2015.11.19
* 檔案下載
* @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 ;
}
/**
* @author dcx by 2015.11.19
* 檔案刪除
* @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 主函式測試
*/
public static void main(String[] args) {
boolean status = false ;
String dst1 = "hdfs://192.168.1.225:9000/EBLearn_data/new" ;
Configuration conf = new Configuration() ;
//java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.1.225:9000/EBLearn_data/hello.txt, expected: file:///
//解決這個錯誤的兩個方案:
//方案1:下面這條命令必須加上,否則出現上面這個錯誤
conf.set("fs.default.name", "hdfs://192.168.1.225:9000"); // "hdfs://master:9000"
//方案2: 將core-site.xml 和hdfs-site.xml放入當前工程中
status = CreatDir( dst1 , conf) ;
System.out.println("status="+status) ;
String dst = "hdfs://192.168.1.225:9000/EBLearn_data" ;
String src = "I:/hello.txt" ;
status = putToHDFS( src , dst , conf) ;
System.out.println("status="+status) ;
src = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
dst = "I:/hadoop_need/" ;
status = getFromHDFS( src , dst , conf) ;
System.out.println("status="+status) ;
dst = "hdfs://192.168.1.225:9000/EBLearn_data/hello.txt" ;
status = checkAndDel( dst , conf) ;
System.out.println("status="+status) ;