windows下eclipse+hadoop2.4開發手冊java
1.解壓下載的hadoop2.4,到任意盤符,例如D:\hadoop-2.4.0。
2.設置環境變量
①新建系統變量,以下所示。服務器
②將新建的HADOOP_HOME變量「%HADOOP_HOME%\bin;」加入到PATH變量裏,以下圖。eclipse
3.將hadoop服務器下的hadoop目錄下etc/hadoop目錄下的如下四個文件拷貝到本身開發的電腦相應目錄下,以下圖所示。
4.若是hadoop服務器中上述四個文件配置的是機器名,請在開發的電腦中改成ip地址,例以下圖。
5.將hadoop目錄下的全部jar包拷貝到本身的項目中,例以下圖所示。
①將「D:\hadoop-2.4.0\share\hadoop」目錄下及其子目錄中全部jar以及子目錄下的lib目錄下的jar拷貝到本身的項目中。orm
②我一共拷貝了117個jar,以下圖所示。blog
6.將hadoop服務器上的hadoop目錄下的etc/hadoop目錄下的如下兩個文件拷貝到項目中的src目錄下,一樣將文件內容中的機器名改成ip。
7.HDFS操做類
其中hdfspath例如:"hdfs://192.168.1.103:9000/input/";//要保證你的hdfs空間中有此路徑
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
public class HDFSOperation {
private Configuration conf;
private FileSystem fs;
/**
* @Title: HDFSOperation
* @Description 初始化配置
* @author cpthack
* @see 初始化配置
* @return 對參數的說明
* @param 對方法中某參數的說明
* @example 方法使用例子
* */
public HDFSOperation() throws IOException{
conf = new Configuration();
fs = FileSystem.get(conf);
}
/**
* @Title: upLoad
* @Description 上傳文件
* @author cpthack
* @see 上傳文件
* @return 對參數的說明
* @param in:文件輸入流;hdfsPath:保存在雲端的文件路徑
* @example 方法使用例子
* */
public boolean upLoad(InputStream in, String hdfsPath){
Path p = new Path(hdfsPath);
try{
if(fs.exists(p)){
System.out.println("文件已經存在");
return false;
}
//得到hadoop系統的鏈接
FileSystem fs = FileSystem.get(URI.create(hdfsPath),conf);
//out對應的是Hadoop文件系統中的目錄
OutputStream out = fs.create(new Path(hdfsPath));
IOUtils.copyBytes(in, out, 4096,true);//4096是4k字節
in.close();
}catch(Exception e){
e.printStackTrace();
}
return true;
}
/**
* @Title: upLoad
* @Description 下載文件
* @author cpthack
* @see 下載文件
* @return 對參數的說明
* @param localPath:文件保存在本地的路徑;hdfsPath:文件存在雲端的路徑
* @example 方法使用例子
* */
@SuppressWarnings("resource")
public boolean downLoad(String hdfsPath,String localPath ){
Path path = new Path(hdfsPath);
try {
if(!fs.exists(path)){
System.out.println("雲端文件不存在");
return false;
}
FileSystem hdfs = FileSystem.get(conf);
Path dstPath = new Path(localPath);
hdfs.copyToLocalFile(true,path, dstPath);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public boolean downFromCloud(String hdfsPath,String srcFileName){
// 實例化一個文件系統
FileSystem fs;
try {
fs = FileSystem.get(URI.create(hdfsPath), conf);
// 讀出流
FSDataInputStream HDFS_IN = fs.open(new Path(hdfsPath));
// 寫入流
OutputStream OutToLOCAL = new FileOutputStream(srcFileName);
// 將InputStrteam 中的內容經過IOUtils的copyBytes方法複製到OutToLOCAL中
IOUtils.copyBytes(HDFS_IN, OutToLOCAL, 1024, true);
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
/**
* @Title: deletePath
* @Description 刪除文件
* @author cpthack
* @see 刪除文件
* @return 對參數的說明
* @param hdfsPath:文件存在雲端的路徑
* @example 方法使用例子
* */
public boolean deletePath(String hdfsPath){
try {
fs.delete(new Path(hdfsPath), true);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @Title: getFileList
* @Description 獲取某個目錄下全部文件
* @author cpthack
* @see 獲取某個目錄下全部文件
* @return 對參數的說明
* @param hdfsPath:存在雲端的文件夾
* @example 方法使用例子
* */
public ArrayList<FileBean> getFileList(String hdfsPath){
Path path = new Path(hdfsPath);
ArrayList<FileBean> fileList = new ArrayList<FileBean>();
FileStatus[] status;
try {
status = fs.listStatus(path);
for(FileStatus fs : status){
fileList.add(new FileBean(fs));
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}
//建立文件夾
public boolean mkdir(String dir){
FileSystem fs;
try {
fs = FileSystem.get(conf);
fs.mkdirs(new Path(dir));
fs.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
/*刪除文件夾*/
@SuppressWarnings("deprecation")
public boolean deleteDir(String dir){
FileSystem fs;
try {
fs = FileSystem.get(conf);
fs.delete(new Path(dir));
fs.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}