windows下,用eclipse連線虛擬機器中的hadoop
1. 前言
1.1 目標
具體目標是:
在windows 上能夠使用eclipse連線Centos系統上部署的hadoop進行開發與測試
1.2 軟硬體要求
硬體要求: |
三臺普通PC。 |
當然使用虛擬機器也可以,本環境搭建也是VMWare10虛擬機器上進行的。 |
軟體要求: |
作業系統:Centos7.1。 Hadoop版本: hadoop-1.2.1 Eclipse版本:2.0.2 Java版本:1.6.0_06(必須1.6以上) |
注意:
Hadoop版本和Eclipse版本請嚴格按照要求。
在使用的時候很多需要獲取許可權的,但是開發測試程式是沒有問題的。 如果要減少許可權問題的發生,可以這樣做:Centos上執行
1.3 環境拓撲圖
主機名 |
Hadoop角色 |
Hadoop jps命令結果 |
Hadoop使用者 |
Hadoop安裝目錄 |
CentosMaster |
root |
NameNode JobTracker SecondaryNameNode |
建立相同的使用者的組名:root。 安裝hadoop-1.2.1時使用root使用者,並且hadoop的資料夾歸屬也是root:root |
/opt/modules/hadoop-1.2.1 |
CentosSlave1 |
root |
DataNode TaskTracker |
||
CentosSlave2 |
root |
DataNode TaskTracker |
||
Windows |
開發測試環境 |
安裝了jdk與eclipse,本身不需要安裝hadoop,但是需要hadoop安裝包下面的jar包。 |
注意:Centos在偽分佈和單機模式下master既是NameNode又是DataNode,同時也是JobTracker,taskTracker。
2、安裝hadoop
詳情見這裡
在windows上通過WEB檢視hadoop相關資訊。
修改C:\WINDOWS\system32\drivers\etc\hosts檔案,加入主機名與IP對應關係:
192.168.128.130 master.dragon.org 192.168.128.130 slave1.dragon.org 192.168.128.131 slave2.dragon.org |
3.8 執行WordCount例項
WordCount是hadoop自帶的例項,統計一批文字檔案中各單詞出現的資料,輸出到指定的output目錄中,輸出目錄如果已經存在會報錯。
$ cd master.dragon.org $ hadoop fs -mkdir input $ hadoop fs -cp master.dragon.org*.txt input/ $ hadoop jar hadoop-1.2.1-examples.jar wordcount input output $ hadoop fs -cat output/* #最後檢視結果 |
4. Windows下eclipse開發環境配置
4.1 系統環境配置
在windows上通過WEB檢視hadoop相關資訊。
修改C:\WINDOWS\system32\drivers\etc\hosts檔案,加入主機名與IP對應關係:
192.168.128.130 master.dragon.org 192.168.128.130 slave1.dragon.org 192.168.128.131 slave2.dragon.org |
4.2 安裝開發hadoop外掛
將hadoop安裝包hadoop\contrib\eclipse-plugin\包經過編譯,拷貝到eclipse的外掛目錄plugins下。
需要注意的是外掛版本(及後面開發匯入的所有jar包)與執行的hadoop一致,否則可能會出現EOFException異常。
重啟eclipse,開啟windows->open perspective->other->map/reduce 可以看到map/reduce開發檢視。
4.3 設定連線引數
開啟windows->show view->other-> map/reduce Locations檢視,在點選大象後彈出的對話方塊(General tab)進行引數的新增:
引數說明如下:
Location name:任意
map/reduce master:與mapred-site.xml裡面mapred.job.tracker設定一致。
DFS master:與core-site.xml裡fs.default.name設定一致。
User name: 伺服器上執行hadoop服務的使用者名稱。
然後是開啟“Advanced parameters”設定面板,修改相應引數。上面的引數填寫以後,也會反映到這裡相應的引數:
主要關注下面幾個引數:
fs.defualt.name:與core-site.xml裡fs.default.name設定一致。
mapred.job.tracker:與mapred-site.xml裡面mapred.job.tracker設定一致。
dfs.replication:與hdfs-site.xml裡面的dfs.replication一致。
hadoop.tmp.dir:與core-site.xml裡hadoop.tmp.dir設定一致。
hadoop.job.ugi:並不是設定使用者名稱與密碼。是使用者與組名,所以這裡填寫root,root。
4.4 執行hadoop程式
首先將hadoop安裝包下面的所有jar包都導到eclipse工程裡。
然後建立一個類:DFSOperator.java,該類寫了四個基本方法:建立檔案,刪除檔案,把檔案內容讀為字串,將字串寫入檔案。同時有個main函式,可以修改測試:
package com.kingdee.hadoop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; /** * * The utilities to operate file on hadoop hdfs. * * @author luolihui 2011-07-18 * */ public class DFSOperator { private static final String ROOT_PATH = "hdfs:///"; private static final int BUFFER_SIZE = 4096; /** * construct. */ public DFSOperator(){} /** * Create a file on hdfs.The root path is /.<br> * for example: DFSOperator.createFile("/lory/test1.txt", true); * @param path the file name to open * @param overwrite if a file with this name already exists, then if true, the file will be * @return true if delete is successful else IOException. * @throws IOException */ public static boolean createFile(String path, boolean overwrite) throws IOException { //String uri = "hdfs://192.168.1.100:9000"; //FileSystem fs1 = FileSystem.get(URI.create(uri), conf); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path f = new Path(ROOT_PATH + path); fs.create(f, overwrite); fs.close(); return true; } /** * Delete a file on hdfs.The root path is /. <br> * for example: DFSOperator.deleteFile("/user/hadoop/output", true); * @param path the path to delete * @param recursive if path is a directory and set to true, the directory is deleted else throws an exception. In case of a file the recursive can be set to either true or false. * @return true if delete is successful else IOException. * @throws IOException */ public static boolean deleteFile(String path, boolean recursive) throws IOException { //String uri = "hdfs://192.168.1.100:9000"; //FileSystem fs1 = FileSystem.get(URI.create(uri), conf); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path f = new Path(ROOT_PATH + path); fs.delete(f, recursive); fs.close(); return true; } /** * Read a file to string on hadoop hdfs. From stream to string. <br> * for example: System.out.println(DFSOperator.readDFSFileToString("/user/hadoop/input/test3.txt")); * @param path the path to read * @return true if read is successful else IOException. * @throws IOException */ public static String readDFSFileToString(String path) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path f = new Path(ROOT_PATH + path); InputStream in = null; String str = null; StringBuilder sb = new StringBuilder(BUFFER_SIZE); if (fs.exists(f)) { in = fs.open(f); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); while ((str = bf.readLine()) != null) { sb.append(str); sb.append("\n"); } in.close(); bf.close(); fs.close(); return sb.toString(); } else { return null; } } /** * Write string to a hadoop hdfs file. <br> * for example: DFSOperator.writeStringToDFSFile("/lory/test1.txt", "You are a bad man.\nReally!\n"); * @param path the file where the string to write in. * @param string the context to write in a file. * @return true if write is successful else IOException. * @throws IOException */ public static boolean writeStringToDFSFile(String path, String string) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); FSDataOutputStream os = null; Path f = new Path(ROOT_PATH + path); os = fs.create(f,true); os.writeBytes(string); os.close(); fs.close(); return true; } public static void main(String[] args) { try { DFSOperator.createFile("/lory/test1.txt", true); DFSOperator.deleteFile("/dfs_operator.txt", true); DFSOperator.writeStringToDFSFile("/lory/test1.txt", "You are a bad man.\nReally?\n"); System.out.println(DFSOperator.readDFSFileToString("/lory/test1.txt")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("===end==="); } } |
然後Run AsàRun on HadoopàChoose an exitsing server from the list belowàfinish.
結果很簡單(那個警告不管):
11/07/16 18:44:32 WARN conf.Configuration: DEPRECATED: hadoop-site.xml found in the classpath. Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, mapred-site.xml and hdfs-site.xml to override properties of core-default.xml, mapred-default.xml and hdfs-default.xml respectively You are a bad man. Really? ===end=== |
也可以執行hadoop自帶的WorkCount程式,找到其原始碼導進來,然後設定輸入輸出引數,然後同樣“Run on hadoop”。具體步驟不再示範。
每“Run on hadoop”都會在workspace\.metadata\.plugins\org.apache.hadoop.eclipse下生成臨時jar包。不過第一次需要Run on hadoop,以後只需要點選那執行的綠色按鈕了。
5. 錯誤及處理
5.1 安全模式問題
我在eclipse上刪除DFS上的資料夾時,出現下面錯誤:
錯誤提示說得也比較明示,是NameNode在安全模式中,其解決方案也一併給出。
類似的執行hadoop程式時,有時候會報以下錯誤:
org.apache.hadoop.dfs.SafeModeException: Cannot delete /user/hadoop/input. Name node is in safe mode
解除安全模式:
bin/hadoop dfsadmin -safemode leave
使用者可以通過dfsadmin -safemode value 來操作安全模式,引數value的說明如下:
enter - 進入安全模式
leave - 強制NameNode離開安全模式
get - 返回安全模式是否開啟的資訊
wait - 等待,一直到安全模式結束。
5.2 開發時報錯Permission denied
org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=Administrator, access=WRITE, inode="test1.txt":hadoop:supergroup:rw-r--r-- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.hadoop.ipc.RemoteException.instantiateException(RemoteException.java:96) at org.apache.hadoop.ipc.RemoteException.unwrapRemoteException(RemoteException.java:58) at org.apache.hadoop.hdfs.DFSClient$DFSOutputStream.<init>(DFSClient.java:2710) at org.apache.hadoop.hdfs.DFSClient.create(DFSClient.java:492) at org.apache.hadoop.hdfs.DistributedFileSystem.create(DistributedFileSystem.java:195) at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:484) at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:465) at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:372) at com.kingdee.hadoop.DFSOperator.createFile(DFSOperator.java:46) at com.kingdee.hadoop.DFSOperator.main(DFSOperator.java:134) |
解決方法是,在“Advanced parameters”設定面板,設定hadoop.job.ugi引數,將hadoop使用者加上去。
變為:
然後重新在執行中”Run on hadoop”。
另一方法是改變要操作的檔案的許可權。
Permission denied: user=Administrator, access=WRITE, inode="test1.txt":hadoop:supergroup:rw-r--r-- |
上面的意思是:test1.txt檔案的訪問許可權是rw-r--r--,歸屬組是supergroup,歸屬使用者是hadoop,現在使用Administrator使用者對test1.txt檔案進行WRITE方式訪問,被拒絕了。
所以可以改變下test1.txt檔案的訪問許可權:
$ hadoop fs –chmod 777 /lory/test1.txt $ hadoop fs –chmod 777 /lory #或者上一級資料夾 |
當然使用-chown命令也可以。