熟悉常用的HBase操作,编写MapReduce作业

时间:2023-03-09 08:27:21
熟悉常用的HBase操作,编写MapReduce作业

1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据:

学生表(Student)

学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S_Age)
2015001 Zhangsan male 23
2015002 Marry female 22
2015003 Lisi male 24

命令如下

ssh localhost
start-dfs.sh
start-hbase.sh
hbase shell
create 'Student', 'S_No', 'S_Name', 'S_Sex', 'S_Age' put 'Student', '001', 'S_No', '2015001'
put 'Student', '001', 'S_Name', 'Zhangsan'
put 'Student', '001', 'S_Sex', 'male'
put 'Student', '001', 'S_Age', '23' put 'Student', '002', 'S_No', '2015002'
put 'Student', '002', 'S_Name', 'Marry'
put 'Student', '002', 'S_Sex', 'female'
put 'Student', '002', 'S_Age', '22' put 'Student', '003', 'S_No', '2015003'
put 'Student', '003', 'S_Name', 'Lisi'
put 'Student', '003', 'S_Sex', 'male'
put 'Student', '003', 'S_Age', '24'

2. 用Hadoop提供的HBase Shell命令完成相同任务:

  • 列出HBase所有的表的相关信息;list
  • 在终端打印出学生表的所有记录数据;
  • 向学生表添加课程列族;
  • 向课程列族添加数学列并登记成绩为85;
  • 删除课程列;
  • 统计表的行数;count 's1'
  • 清空指定的表的所有记录数据;truncate 's1'
list
scan 'Student'
alter 'Student', NAME=>'S_Course'
put 'Student', '001', 'S_Course:math', '85'
alter 'Student', {NAME=>'S_Course', METHOD=>'delete'}
count 'Student'
truncate 'Student'

3. 用Python编写WordCount程序任务

程序 WordCount
输入 一个包含大量单词的文本文件
输出 文件中每个单词及其出现次数(频数),并按照单词字母顺序排序,每个单词和其频数占一行,单词和频数之间有间隔
  • 编写map函数,reduce函数
  • 将其权限作出相应修改
  • 本机上测试运行代码
  • 放到HDFS上运行
  • 下载并上传文件到hdfs上
  • 用Hadoop Streaming命令提交任务

这里所有的操作均在用户目录下 ~

首先把hdfs中input里面的txt文件清除,然后放入需要分析的文本文件,命令如下

hdfs dfs -rm input/*.txt
hdfs dfs -put ~/lyric.txt input/

在目录下创建mapper.py

内容如下:

import sys

for line in sys.stdin:
line = line.strip()
words = line.split()
for word in words:
print('%s\t%s' % (word, 1))

在目录下创建reducer.py

内容如下:


from operator import itemgetter
import sys current_word = None
current_count = 0
word = None for line in sys.stdin:
line = line.strip()
word, count = line.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)

接下来配置.bashrc文件,将streaming的路径配置到环境变量中


export HADOOP_HOME=/usr/local/hadoop
export STREAM=$HADOOP_HOME/share/hadoop/tools/lib/hadoop-streaming-*.jar

配置好后在目录下创建run.sh

内容如下:


hadoop jar $STREAM \
-D stream.non.zero.exit.is.failure=false \
-file /home/hadoop/mapper.py \
-mapper 'python /home/hadoop/mapper.py' \
-file /home/hadoop/reducer.py \
-reducer 'python /home/hadoop/reducer.py' \
-input /user/hadoop/input/*.txt \
-output /user/hadoop/wcoutput

在配置mapperreducer中,加入了python,不然运行出错。

还有上面的命令中加入-D stream.non.zero.exit.is.failure=false 是因为运行时抛出异常

java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): subprocess failed wi

这个异常是streaming默认的情况下,mapper和reducer的返回值不是0,被认为异常任务,将被再次执行,默认尝试4次都不是0,整个job都将失败。

现在在本目录下写入命令source run.sh即可运行,之后在运行命令

hdfs dfs -cat wcoutput/*就可看见执行后代码后的结果

参考链接

https://blog.****.net/deqingguo/article/details/7448427

https://blog.****.net/liang0000zai/article/details/50547177