標籤:open report 安全 分布式檔案 install 詳解 tor success inf
轉自:http://blog.csdn.net/michaelwubo/article/details/50879832
一、使用Hadoop URL讀取資料
package hadoop;import java.io.InputStream;import java.net.URL;import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;import org.apache.hadoop.io.IOUtils;public class URLCat { static { URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); } public static void readHdfs(String url) throws Exception { InputStream in = null; try { in = new URL(url).openStream(); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } public static void main(String[] args) throws Exception { readHdfs("hdfs://192.168.49.131:9000/user/hadoopuser/input20120828/file01"); }}
其中,我使用到的jar包有:
hadoop-core的版本一定要和分布式環境上安裝的hadoop版本保持一致,不然會報錯:
12/09/11 14:18:59 INFO security.UserGroupInformation: JAAS Configuration already set up for Hadoop, not re-installing.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/thirdparty/guava/common/collect/LinkedListMultimap
at org.apache.hadoop.hdfs.SocketCache.<init>(SocketCache.java:48)
at org.apache.hadoop.hdfs.DFSClient.<init>(DFSClient.java:240)
分布式環境上安裝的hadoop版本如下:
運行main方法,輸出:hello world bye world 和hdfs中儲存的檔案資訊是保持一致的:
二、使用FileSystem API 讀取資料
package hadoop;import java.io.IOException;import java.io.InputStream;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;public class FileSystemCat { public static void readHdfs(String url) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url), conf); InputStream in = null; try { in = fs.open(new Path(url)); IOUtils.copyBytes(in, System.out, 4096, false); } finally { IOUtils.closeStream(in); } } public static void main(String[] args) throws IOException { readHdfs("hdfs://192.168.49.131:9000/user/hadoopuser/output20120828/part-00000"); }}
執行輸出:
bye 2
hadoop 2
hello 2
world 2
三、建立目錄
3.1 寫資料 public boolean mkdirs(Path f) throws IOException 會按照用戶端請求建立未存在的父目錄
package hadoop;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URI;import org.apache.hadoop.conf.Configuration;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 FileCopyWithProgress { public static void fileCopy(String localFile, String hdfsFile) throws IOException{ InputStream in = new BufferedInputStream(new FileInputStream(localFile)); Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(hdfsFile),conf); OutputStream out = fs.create(new Path(hdfsFile),new Progressable(){ public void progress(){ System.out.println("*"); } }); IOUtils.copyBytes(in, out, 4096,true); } public static void main(String[] args) throws IOException { fileCopy("D://heat2.txt", "hdfs://192.168.49.131:9000/user/hadoopuser/output20120911/"); }}
執行後會報錯如下:
Exception in thread "main" org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=libininfo, access=WRITE, inode="/user/hadoopuser":hadoopuser:supergroup:drwxr-xr-x
因為往hadoop寫檔案是許可權不容許的,
解決方案:在hdfs-site.xml 中取消許可權校正,即加入以下配置:
到伺服器上修改hadoop的設定檔:conf/hdfs-core.xml, 找到 dfs.permissions 的配置項 , 將value值改為 false
再次運行,如果有以下報錯:
Exception in thread "main" org.apache.hadoop.ipc.RemoteException: org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot create file/user/hadoopuser/output20120911. Name node is in safe mode.
The reported blocks 6 has reached the threshold 0.9990 of total blocks 6. Safe mode will be turned off automatically in 5 seconds.
說明Hadoop的NameNode處在安全模式下,那什麼是Hadoop的安全模式呢?
在Distributed File System啟動的時候,開始的時候會有安全模式,當Distributed File System處於安全模式的情況下,檔案系統中的內容不允許修改也不允許刪除,直到安全模式結束。安全模式主要是為了系統啟動的時候檢查各個DataNode上資料區塊的有效性,同時根據策略必要的複製或者刪除部分資料區塊。運行期通過命令也可以進入安全模式。在實踐過程中,系統啟動的時候去修改和刪除檔案也會有安全模式不允許修改的出錯提示,只需要等待一會兒即可。
現在就清楚了,那現在要解決這個問題,我想讓Hadoop不處在safe mode 模式下,能不能不用等,直接解決呢?
答案是可以的,只要在Hadoop的目錄下輸入:
bin/hadoop dfsadmin -safemode leave
也就是關閉Hadoop的安全模式,這樣問題就解決了。如果不這麼操作,我們可以等待幾秒,然後再次執行程式,可以看到程式正常執行,有以下輸出:
*
*
*
*
*
"*",即上傳進度,沒寫入64KB即輸出一個"*"
然後查看hdfs的目錄發現檔案已經存在。
3.2 檔案系統查詢 列出目錄檔案資訊
package hadoop;import java.io.IOException;import java.net.URI;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.FileUtil;import org.apache.hadoop.fs.Path;public class ListStatus { public static void readStatus(String url) throws IOException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(URI.create(url), conf); Path[] paths = new Path[1]; paths[0] = new Path(url); FileStatus[] status = fs.listStatus(paths); Path[] listedPaths = FileUtil.stat2Paths(status); for (Path p : listedPaths) { System.out.println(p); } } public static void main(String[] args) throws IOException { readStatus("hdfs://192.168.49.131:9000/user/hadoopuser/output20120828/"); }}
輸出:
hdfs://192.168.49.131:9000/user/hadoopuser/output20120828/_SUCCESS
hdfs://192.168.49.131:9000/user/hadoopuser/output20120828/_logs
hdfs://192.168.49.131:9000/user/hadoopuser/output20120828/part-00000
HDFS常用的Java Api詳解