• Hadoop生态体系组件


    目录:
    一、本地数据集上传到数据仓库Hive
    二、Hive的基本操作
    三、Hive、Mysql、HBase数据互导

    正文:
    一、本地数据集上传到数据仓库Hive
    1.实验数据集的下载
    2.数据集的预处理
    ⁃ 1)删除文件第一行记录
    ⁃ sed -i ‘1d’ filename #1d表示删除第一行,同理,nd表示删除第n行
    ⁃ 2)对字段进行预处理
    3.把得到的.txt文件导入Hive
    基本思路:先将.txt文件上传到分布式文件系统HDFS,然后在Hive中创建一个外部表,完成导入
    • 1)启动HDFS
    • jps #该命令用于查看当前运行的进程
    • 2)启动Hadoop
    ⁃ cd /usr/local/hadoop/sbin #进入/hadoop/sbin文件夹下运行
    ⁃ ./start-all.sh
    • 3)启动mysql(这一步不需要,只是备注)
    • sudo service mysql start
    • 4)把.txt本地文件上传到HDFS中
    • cd /usr/local/hadoop
    • ./bin/hdfs dfs -mkdir -p /bigdatacase/dataset #在HDFS的根目录下面创建一个新的目录bigdatacase,并在这个目录下创建一个子目录dataset
    • ./bin/hdfs dfs -put /本地文件存放的目录/filename /bigdatacase/dataset
    • ./bin/hdfs dfs -cat /bigdatacase/dataset/filename | head -5 #查看hdfs上文件的前五行

    (./bin/hdfs dfs 可以直接换成 hadoop fs)

    二、Hive的基本操作
    1.在Hive上创建数据库
    • 1)启动Hive(前提需要首先启动Hadoop、Mysql)
    • cd /usr/local/hive
    • ./bin/hive
    • (可以直接在终端窗口输入hive,进入hive命令行)
    • 2)创建数据库
    • create databases name;
    • 3)创建外部表
    • create external table db.data(id int, uid string, data date) comment ‘Welcome’ row format delimited fields terminated by ‘ ’ stored as textfile location ‘/bigdatacase/dataset';
    • show create table data; #查看表的各种属性
    • desc data; #查看表的简单结构
    2.Hive的查询语句
    • 1)嵌套查询+取别名
    • select e.bh, e.it from (select behavior_type as bh, item_category as it from bigdata_user) as e limit 20;
    • 2)统计分析
    • >聚合函数count()
    • select count() from data;
    • >去重distinct
    • select count(distinct id) from data;
    • >例:查询不重复的数据有多少条
    • select count(
    ) from (select id,status,loc from data group by id,status,loc having count(*)=1) a

    注意:嵌套语句最好取别名,就是上面的a,否则很容易出现如下错误.

    • 3)关键字条件查询
    • select count() from bigdata_user where behavior_type='1' and visit_date<'2014-12-13' and visit_date>'2014-12-10';
    • select count(distinct uid), day(visit_date) from bigdata_user where behavior_type='4' group by day(visit_date);
    • select count(
    ) from bigdata_user where province='江西' and visit_date='2014-12-12' and behavior_type='4';
    • 4)根据用户行为分析
    • select count() from bigdata_user where visit_date='2014-12-11'and behavior_type='4'; #查询有多少用户在2014-12-11购买了商品
    • select count(
    ) from bigdata_user where visit_date ='2014-12-11'; #查询有多少用户在2014-12-11点击了该店
    • select count() from bigdata_user where uid=10001082 and visit_date='2014-12-12'; #查询用户10001082在2014-12-12点击网站的次数
    • select count(
    ) from bigdata_user where visit_date='2014-12-12'; #查询所有用户在这一天点击该网站的次数
    • select uid from bigdata_user where behavior_type='4' and visit_date='2014-12-12' group by uid having count(behavior_type='4')>5; #查询某一天在该网站购买商品超过5次的用户id
    • 5)用户实时查询分析
    • create table scan(province STRING,scan INT) COMMENT 'This is the search of bigdataday' ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' STORED AS TEXTFILE; #创建新的数据表进行存储
    • insert overwrite table scan select province,count(behavior_type) from bigdata_user where behavior_type='1' group by province; #导入数据
    • select * from scan; #显示结果

    三、Hive、Mysql、HBase数据互导
    1.准备工作
    • 1)启动Hadoop、Mysql、Hive
    • 2)新建一个临时表,把另一个表的数据插入进去
    • create table data2 like data;
    • insert overwrite table db.data2 select * from data;
    2.使用Sqoop将数据从Hive导入到Mysql
    • 1)登陆Mysql,并创建数据库、表
    • mysql -u root -p
    • create database db;
    • use db;
    • show variables like "char%"; #查看数据库的编码
    • set character_set_server=utf8 #设置当前编码为utf8,保证中文的正常导入
    • create table data(id int,status int,uid` varchar(50)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    • exit #退出
    • 2)导入数据(进入sqoop的目录运行)
    • ./bin/sqoop export ##表示数据从 hive 复制到 mysql 中
    • --connect jdbc:mysql://localhost:3306/数据库名
    • --username root #mysql登陆用户名
    • --password strongs #登录密码
    • --table data #mysql 中的表,即将被导入的表名称
    • --export-dir '/user/hive/warehouse/dblab.db/user_action ' #hive 中被导出的文件
    • --input-fields-terminated-by ‘.’
    • --fields-terminated-by ' ' #Hive 中被导出的文件字段的分隔符
    3.使用Sqoop将数据从Mysql导入HBase
    • 1)启动Hadoop、MySQL
    • 2)启动HBase和HBase shell(进入/hbase目录)
    • ./bin/start-hbase.sh
    • ./bin/hbase shell
    • 3)创建表
    • create 'user_action', { NAME => 'f1', VERSIONS => 5} #创建了一个user_action表,这个表中有一个列族f1(你愿意把列族名称取为其他名称也可以,比如列族名称为userinfo),历史版本保留数量为5。
    • 4)导入数据(新开一个页面,进入sqoop的目录运行)
    • ./bin/sqoop import
    • --connect jdbc:mysql://localhost:3306/dblab
    • --username root
    • --password hadoop
    • --table user_action
    • --hbase-table user_action #HBase中表名称
    • --column-family f1 #列簇名称
    • --hbase-row-key id #HBase 行键
    • --hbase-create-table #是否在不存在情况下创建表
    • -m 1 #启动 Map 数量
    • 5)查看导入的数据(切换到HBase Shell运行到窗口)
    • scan 'user_action',{LIMIT=>10}

  • 相关阅读:
    angular 个人零点学习
    angularjs 五大关键点
    OA项目学习总结
    oa
    时间插件
    angular js模态框
    搜索
    xianduanshu
    o-o
    paibingbuzhen
  • 原文地址:https://www.cnblogs.com/hannahzhao/p/11960080.html
Copyright © 2020-2023  润新知