熟悉常用的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
在配置mapper和reducer中,加入了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.csdn.net/deqingguo/article/details/7448427
https://blog.csdn.net/liang0000zai/article/details/50547177
熟悉常用的HBase操作,编写MapReduce作业的更多相关文章
- 熟悉常用的HBase操作
1. 以下关系型数据库中的表和数据,要求将其转换为适合于HBase存储的表并插入数据: 学生表(Student)(不包括最后一列) 学号(S_No) 姓名(S_Name) 性别(S_Sex) 年龄(S ...
- 实验3- 熟悉常用的 HBase 操作
石家庄铁道大学信息科学与技术学院 实验报告 2018年----2019年 第一学期 题目: 熟悉常用的 HBase ...
- Tutorial 01_熟悉常用的Linux操作和Hadoop操作
(一)熟悉常用的Linux 操作cd 命令:切换目录 (1) 切换到目录“/usr/local” (2) 切换到当前目录的上一级目录 (3) 切换到当前登录Linux 系统的用户的自己的主文件夹 ...
- 课程作业——熟悉常用的Linux操作
cd命令:切换目录 (1) 切换到目录 /usr/local cd /usr/local (2) 去到目前的上层目录 cd .. (3) 回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4) ...
- Tutorial 02_熟悉常用的HDFS操作
Shell命令实现: (1)向HDFS 中上传任意文本文件,如果指定的文件在HDFS 中已经存在,则由用户来指定是追加到原有文件末尾还是覆盖原有的文件: (2) 从HDFS 中下载指定文件,如果本地文 ...
- 熟悉常用的HDFS操作
编程实现以下指定功能,并利用Hadoop提供的Shell命令完成相同任务: 在本地Linux文件系统的“/home/hadoop/”目录下创建一个文件txt,里面可以随意输入一些单词. 在本地查看文件 ...
- 第三章 熟悉常用的HDFS操作
一.Hadoop提供的Shell命令完成相同任务: 1.在本地Linux文件系统的“/home/hadoop/”目录下创建一个文件txt,里面可以随意输入一些单词. mkdir hadoop 在本地查 ...
- 熟悉常用的Linux操作
请按要求上机实践如下linux基本命令. cd命令:切换目录 (1)切换到目录 /usr/local cd /usr/local (2)去到目前的上层目录 cd .. (3)回到自己的主文件夹 ...
- MapReduce编程中常用的字符操作
本文主要用于记录自己在编写mapreduce程序时常用的一些方法,后期会不断更新,用于自己复习和给新手一些帮助. 字符串操作 String str = " 12345"; // 字 ...
随机推荐
- Spring源码工程导入Eclsipse缺少两个jar文件
按照<Spring源码深度解析>所述,使用gradle cleanidea eclipse将Spring源码转为eclipse工程后,导入eclipse,最后发现还是缺少spring-cg ...
- [Swift]LeetCode198. 打家劫舍 | House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- [Swift]LeetCode373. 查找和最小的K对数字 | Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [Swift]LeetCode623. 在二叉树中增加一行 | Add One Row to Tree
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- [Swift]LeetCode707. 设计链表 | Design Linked List
Design your implementation of the linked list. You can choose to use the singly linked list or the d ...
- Metrics.NET step by step
安装Nuget包 nuget中搜索metrics,如图: 配置Metrics 在程序入口处插入配置Metrics的代码. class Program { static void Main(string ...
- mybatis 批量更新 Parameter '__frch_item_0' not found. Available parameters are [list]
一次在做批量更新数据的时候报错 Parameter '__frch_item_0' not found. Available parameters are [list] 记过反复查找,最后才发现是一个 ...
- spark System memory must be at least
运行 ScalaSpark 程序的时候出现错误: System memory * must be at least *.Please increase heap size using the --dr ...
- spark使用udf给dataFrame新增列
在 spark 中给 dataframe 增加一列的方法一般使用 withColumn // 新建一个dataFrame val sparkconf = new SparkConf() .setMas ...
- .Net Core中的Api版本控制
原文链接:API Versioning in .Net Core 作者:Neel Bhatt 简介 Api的版本控制是Api开发中经常遇到的问题, 在大部分中大型项目都需要使用到Api的版本控制 在本 ...