hadoop之旅(二)

前面我們已經搭建好hadoop,hdfs,一臺master兩臺slaves。
1。hadoop-daemon.sh start/stop namenode/datanode啓動/關閉hdfs
2。hdfs dfsadmin -report|more 查看集羣情況
3.start-dfs 直接在master啓動集羣
4.ssh’免密碼登錄ssh_keygen -t rsa ,生成一個私鑰一個公鑰文件,把公鑰拷貝到slaves。ssh-copy-id master /slave1/slave2
5.start-dfs 直接在master啓動集羣
6.修改hdfs-site.xml, 增加property dfs.replication 修改默認備份數
7.core-site.xml

fs.defaultFS
hdfs://master:9000

配置master
hadoop.tmp.dir
/var/hadoop
默認存在tmp下容易出問題就修改
8.java操作HDFS
URL url=new URL(“http://www“);//遠程操作http協議
inputstream in =url.openStream();
IOUtils.copyBytes(in,System.out,4096,true)
1.方式
URL.setURLStreamHandlerfactory(new FsUrlStreamHandlerFactory());
URL url=new URL(“hdfs:192.168.233.131:9000/hello.txt”);//遠程操作hdfs
inputstream in =url.openStream();
IOUtils.copyBytes(in,System.out,4096,true)
2.方式filesystem
Configuration conf=new Configuration();
conf.set(“fs.defaultFS”,”hdfs://192.168.233.131:9000”);
conf.set(“dfs.replication”,2)//默認備份數
FileSystem fileSystem=FileSystem.get(conf); //文件系統抽象類
boolean success=fieSystem.mkdir(new Path(“/msb”));//創建目錄(會覆蓋,用之前要判斷是否存在)

success=fileSystem.exists(new Path(“/hello.txt”))
success=fileSystem.delete(new Path(“/hello.txt”),true) //刪除
FsDataOutputStream out=create(new Path(“/test.data”),true)
FileInputStream fis=new FileInputStream(“f:/hello.txt”)
IOUtils.copyBytes(fis,out,4096,true)//從windows拷貝文件放在集羣上(傳數據)
/第二種傳數據,用java的io/
FsDataOutputStream out=create(new Path(“/test.data”),true)
FileInputStream in=new FileInputStream(“f:/hello.txt”)
byte []buf=new byte[4096]; //buffer
int len = in.read(buf);
while(len !=-1){
out.write(buf,0,len)
len=in.read(buf)
}
in.close();
out.close();

/列舉目錄下所有文件或者子目錄的信息/
FileStatus[] statuses=fileSystem.listStatus(new Path(“/”)); // 子目錄下所有信息

for(FileStatus status:statuses){
status.getPath()//目錄下所有文件路徑
status.getPermission()//權限
status.getReplication//分了多少份
}
9.
windows與linux權限系統不一樣,測試期間可以關閉權限檢查,在namenode-site。xml添加配置
dfs.permissions.enabled
false

hadoop dfsadmin -safemode leave #解除hadoop的安全模式

hadoop fs -copyFromLocal URI#拷貝本地文件到hdfs
hadoop fs -cat file:///file3 /user/hadoop/file4#將路徑指定文件的內容輸出到stdout
hadoop fs -chgrp [-R] GROUP URI#改變文件的所屬組
hadoop fs -chmod [-R] 755 URI#改變用戶訪問權限
hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]#修改文件的所有者
hadoop fs -copyToLocal URI localdst#拷貝hdfs文件到本地
hadoop fs -cp URI [URI …] #拷貝hdfs文件到其它目錄
hadoop fs -du URI [URI …]#顯示目錄中所有文件的大小
hadoop fs -getmerge [addnl]#合併文件到本地目錄

發佈了19 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章