需求:统计各省业务量,使用hive语句分析业务,用sqoop工具进行导入导出
步骤:
- 利用sqoop将mysql中业务表导入hive库(两种方式)
- 可以在hive中执行create建表语句先创建一个与mysql表结构相同的表,再执行数据导入操作
- 直接进行数据导入,hive自动创建表。
- 我采用第二种方式,导入语句为sqoop --connect jdbc:mysql://ip:3306/databaseName --username *** --password *** --table mysqlTabelName --hive-import --hive-overwrite --hive-database hiveDatabase --fields-terminated-by "\t" --create-hive-table --lines-terminated-by "\n" --hive-drop-import-delims --delete-target-dir(其余参数根据自身需要添加)
- 在hive中执行hql语句进行统计(两种方式)
- 可以在hive中执行create创建结果表结构,例如create table temp_result( total int,province string)
row format delimited fields terminated by '\t',再执行insert into table 结果表 as 统计语句,例如insert into temp_result as select count(*) as total,province from bussiness group by province.
-
直接创建统计表并统计,create table temp_result as select count(*) as total,province from bussiness group by province.。。。。。这里是有个疑问的,不知道这个直接进行保存的统计表有没有表头????
- 可以在hive中执行create创建结果表结构,例如create table temp_result( total int,province string)
-
将结果表通过sqoop导出到mysql(两步)
-
首先在mysql中创建表temp_result,结构和hive中的表结构相同,表名字可以不同
-
其次使用sqoop语句导出
sqoop export --connect jdbc:mysql://ip:3306/databaseName --username *** --password *** -m 1 --table temp_result --fields-terminated-by '\t' --export-dir '/user/hive/warehouse/hiveDatabaseName/temp_result';
-
导出时有一个问题:关于mysql主键的问题,为了保持mysql的表和hive的表字段一致,就没创建主键,因为如果在mysql中创建自增id的时候导出就会报错,因为hive中没有id这一列,当然如果能有id最好了,需要再学习。。。