1. 如下關係型數據庫中的表和數據,要求將其轉換爲適合於HBase存儲的表並插入數據:python
學生表(Student)(不包括最後一列)shell
學號(S_No)數據庫 |
姓名(S_Name)app |
性別(S_Sex)函數 |
年齡(S_Age)oop |
課程(course)測試 |
2015001spa |
Zhangsancode |
maleblog |
23 |
|
2015003 |
Mary |
female |
22 |
|
2015003 |
Lisi |
male |
24 |
數學(Math)85 |
首先啓動hadoop,其次啓動hbase,最後打開hbase數據庫
cd /usr/local/hadoop ./sbin/start-dfs.sh cd /usr/local/hbase ./bin/start-hbase.sh hbase shell
create 'Student',{NAME=>'S_No',VERSIONS=>5},{NAME=>'S_Name',VERSIONS=>5},{NAME=>'S_Sex',VERSIONS=>5},{NAME=>'S_Age',VERSIONS=>5} put 'Student','2015001','sname','Zhangsan' put 'Student','2015001','ssex','male' put 'Student','2015001','sage','23' put 'Student','2015002','sname','Mary' put 'Student','2015002','ssex','female' put 'Student','2015002','sage','22' put 'Student','2015003','sname','Lisi' put 'Student','2015003','ssex','male' put 'Student','2015003','sage','24
2. 用Hadoop提供的HBase Shell命令完成相同任務:
scan 'Student'
alter 'Student','NAME'=>'course'
put 'Student','3','course:Math','85'
truncate 'Student'
3. 用Python編寫WordCount程序任務
程序 |
WordCount |
輸入 |
一個包含大量單詞的文本文件 |
輸出 |
文件中每一個單詞及其出現次數(頻數),並按照單詞字母順序排序,每一個單詞和其頻數佔一行,單詞和頻數之間有間隔 |
建立mapper.py文件
cd /home/hadoop/wc
sudo gedit mapper.py
map函數
#!/usr/bin/env python import sys for i in stdin: i = i.strip() words = i.split() for word in words: print '%s\t%s' % (word,1)
賦予權限
chmod a+x /home/hadoop/mapper.py
建立reducer.py文件
cd /home/hadoop/wc
sudo gedit reducer.py
reduce函數
#!/usr/bin/env python from operator import itemgetter import sys current_word = None current_count = 0 word = None for i in stdin: i = i.strip() word, count = i.split('\t',1) try: count = int(count) except ValueError: continue if current_word == word: current_count += count else: if current_word: print '%s\t%s' % (current_word, current_count) current_count = count current_word = word if current_word == word: print '%s\t%s' % (current_word, current_count)
賦予權限
chmod a+x /home/hadoop/reduce.py
測試代碼
echo "foo foo quux labs foo bar quux" | /home/hadoop/wc/mapper.py echo "foo foo quux labs foo bar quux" | /home/hadoop/wc/mapper.py | sort -k1,1 | /home/hadoop/wc/reducer.p
下載文件上傳
cd /home/hadoop/wc wget http://www.gutenberg.org/files/5000/5000-8.txt wget http://www.gutenberg.org/cache/epub/20417/pg20417.txt
cd /usr/hadoop/wc
hdfs dfs -put /home/hadoop/hadoop/gutenberg/*.txt /user/hadoop/input