1.復習Linux hadoop hdfs MapReduce基礎知識
?
1,列舉linux常用命令 shutdown now reboot mkdir mkdir -p touch filename rm -r filename rm -rf filename vi filename i--->可編輯狀態 esc --> : --->wq 保存退出 q! wq! cat grep find ifconfig ping useradd chmod 777 filename more cp srcfile destfile mv tar -zxvf filename -C destpath ls ls -a pwd 查看當前目錄的絕對路徑 filewall: service iptables start/stop/status/restart centos7 : systemctl stop frewalld.service systemctl -cmd --state 修改網絡配置文件: /etc/sysconfig/network-scripts/ifcfg-eth1 配置環境變量 /etc/profile ~/.bash_profile /etc/profild.d/x.sh JAVA export JAVA_HOME=/root/app/jdk1.8.0_161 export PATH=$JAVA_HOME/bin:$PATH 配置文件生效: source ~/.bash_priofile 查看java安裝目錄 echo $JAVA_HOME 2,hash函數特點是什么 代碼論道 字符串----->固定位數的Hash值。 1,確定性 str1--->hash1值 str2---->hash1值 2,單向性 str----hash()----->hash值 3,防篡改 str1--->bit--->hash()--->hash值 4,防碰撞 str1 ---> hash1 str2---->hash1 mapreduce分區默認實現: hash(key)%reduce task Hash實現: SHA MD5 區塊鏈:不可篡改 鏈表+hash(key) 3,hdfs如何實現文件的存儲 NameNode: 處理客戶端的請求 操作元數據(修改文件名\打開文件、關閉文件、DataNode info) DataNode 存儲文件數據block,blocksize=128MB 發送心跳信息,將自身所有存儲的block信息,健康狀況發 送給 NameNode。 SecondNameNode:hadoop2.x可有可無 130MB--> 128MB block + 2MB block 4,hdfs副本存放機制? block 默認3份。 5,hdfs主要進程、yarn的主要進程分別是? NameNode DataNode SecondNameNode yarn:資源調度框架 ResourceManager NodeManager 6,簡述mapreduce計算流程 7,搭建偽分布式步驟 cents6.5 1,前置準備 java tar -zxvf 配置環境變量 ssh 1,安裝ssh服務器 openserver-ssh 2, ssh-keygen -t rsa cd ~/.ssh id_rsa id_rsa.pub cp id_rsa.pub authorized_keys 3,ssh-copy-id 2,安裝hadoop 2.1解壓hadoop tar -zxvf hadoop-xx.tar.gz 2.2 配置文件 $HADOOP_HOME/etc/hadoop hadoop-env.sh JAVA_HOME core-site.xml <property> <name>fs.defaultFS</name> <value>hdfs://Master:9000</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/root/hadoop/app/tmp</value> </property> hdfs-site.xml <property> <name>dfs.replication</name> <value>1</value> </property> HDFS Shell操作: 1,查看Hdfs上的文件 hdfs dfs -ls / hadoop fs -ls / 2,將本地文件上傳到hdfs hdfs dfs -put hello.txt / 3,查看hdfs文件內容 hdfs dfs -cat /hello.txt hdfs dfs -text /hello.txt 4,在hdfs創建目錄 hdfs dfs -mkdir /hello 5,hdfs遞歸創建目錄 hdfs dfs -mkdir -p /test/a/b 6,遞歸查詢目錄 hdfs dfs -ls -p /test 7,將hdfs上的文件下載到本地 hdfs dfs -get /hello.txt 8,將本地文件拷貝到hdfs上 /test/a/b hdfs dfs -copyFromLocal hello.txt /test/a/b/h.txt 9,刪除hdfs上目錄 hdfs dfs -rm -R /hello 10,刪除hdfs上的文件 hdfs dfs -rm /hello.txt
?
2.java操作HDFS文件
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 5 import org.apache.hadoop.conf.Configuration; 6 import org.apache.hadoop.fs.FSDataInputStream; 7 import org.apache.hadoop.fs.FSDataOutputStream; 8 import org.apache.hadoop.fs.FileSystem; 9 import org.apache.hadoop.fs.Path; 10 11 public class HDFSTest { 12 13 public static void main(String[] args) { 14 // TODO 自動生成的方法存根 15 System.setProperty("hadoop.home.dir", "E:\\hadoop"); 16 17 String str=""; 18 //createDir("/new/abc.txt"); 19 //delete("/new"); 20 //createFile("/new/abc.txt"); 21 //write(str); 22 append(str); 23 read(); 24 25 } 26 27 //向文件中寫入 28 public static void write(String str){ 29 if(str==null||str.equals("")){ 30 str="Hello World!\n"; 31 } 32 Configuration conf=new Configuration(); 33 conf.set("fs.default.name", "hdfs://192.168.137.11:9000"); 34 try { 35 FileSystem fs=FileSystem.get(conf); 36 FSDataOutputStream outpustream = fs.create(new Path("/hadoop/a.txt")); 37 outpustream.writeBytes(str); 38 outpustream.close(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 System.out.println(e.getMessage()); 42 } 43 } 44 //向文件中添加 45 public static void append(String str){ 46 if(str==null||str.equals("")){ 47 str="Hello World!\n"; 48 } 49 Configuration conf=new Configuration(); 50 conf.set("fs.default.name", "hdfs://192.168.137.11:9000"); 51 conf.set("dfs.support.append", "true"); 52 conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER"); 53 conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true"); 54 try { 55 FileSystem fs=FileSystem.get(conf); 56 FSDataOutputStream outpustream = fs.append(new Path("/hadoop/a.txt")); 57 outpustream.writeBytes(str); 58 outpustream.close(); 59 } catch (IOException e) { 60 // TODO Auto-generated catch block 61 System.out.println(e.getMessage()); 62 } 63 } 64 //讀取文件,并打印在控制臺 65 public static void read(){ 66 Configuration conf=new Configuration(); 67 conf.set("fs.default.name", "hdfs://192.168.137.11:9000"); 68 try { 69 FileSystem fs=FileSystem.get(conf); 70 //創建輸入流 71 FSDataInputStream inputstream = fs.open(new Path("/hadoop/a.txt")); 72 InputStreamReader isr=new InputStreamReader(inputstream); 73 BufferedReader br=new BufferedReader(isr); 74 String str=br.readLine(); 75 76 while(str!=null){ 77 System.out.println(str); 78 str=br.readLine(); 79 } 80 br.close(); 81 isr.close(); 82 inputstream.close(); 83 84 } catch (IOException e) { 85 // TODO 自動生成的 catch 塊 86 e.printStackTrace(); 87 } 88 89 } 90 //創建目錄 91 public static void createDir(String path) { 92 Configuration configuration=new Configuration(); 93 configuration.set("fs.default.name", "hdfs://192.168.137.11:9000"); 94 try { 95 FileSystem fs=FileSystem.newInstance(configuration); 96 fs.mkdirs(new Path(path)); 97 } catch (IOException e) { 98 // TODO 自動生成的 catch 塊 99 e.printStackTrace(); 100 } 101 102 } 103 //刪除文件 104 public static void delete(String path) { 105 Configuration configuration=new Configuration(); 106 configuration.set("fs.default.name", "hdfs://192.168.137.11:9000"); 107 try { 108 FileSystem fs=FileSystem.newInstance(configuration); 109 fs.delete(new Path(path),true); 110 } catch (IOException e) { 111 // TODO 自動生成的 catch 塊 112 e.printStackTrace(); 113 } 114 } 115 116 //創建文件 117 public static void createFile(String path) { 118 Configuration configuration=new Configuration(); 119 configuration.set("fs.default.name", "hdfs://192.168.137.11:9000"); 120 try { 121 FileSystem fileSystem=FileSystem.newInstance(configuration); 122 fileSystem.createNewFile(new Path(path)); 123 } catch (IOException e) { 124 // TODO 自動生成的 catch 塊 125 e.printStackTrace(); 126 } 127 128 } 129 }
?