5. mysql && hbase && hive && hdfs(部分) 数据互导
5.1 mysql -> hive (包含增量导入)
import命令导入到hdfs中默认采用','进行分割字段值,导入到hive中默认采用'\u0001'来进行分割字段值,如果有特殊的分割方式,我们可以通过参数指定。
import命令导入到hive的时候,会先在/user/${user.name}/文件夹下创建一个同关系型数据库表名的一个文件夹作为中转文件夹,如果该文件夹存在,则报错。可以通过命令sqoop help import查看import命令的帮助信息。
将test表中的数据导出到使用','分割字段的hive表(hivetest2)中。
创建表: create table city_info(city_id int, city_name string, area string) row format delimited fields terminated by ',';
> describe formatted hivetest2;
//sqoop:(从mysql导入hive, 可以不用事先在hive的db中建立相关表)
sqoop import --connect jdbc:mysql://master:3306/sogou --username hive --password 123 --table city_info --hive-database sogou --hive-table city_info --hive-import -m 1 --fields-terminated-by ","
在上述的基础上,分别进行overwrite(覆盖)导入和into(直接加入)导入。
into: 命令同上
overwrite:
sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --hive-table hivetest --hive-import -m 1 --hive-overwrite
通过增加mysql的test表数据,增量导入到hive表中。
sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --where "id>9" --hive-table hivetest --hive-import -m 1
或者
sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --query "select id,name from test where id>9" --hive-table hivetest --hive-import -m 1
5.2 mysql -> hbase
sqoop import --connect jdbc:mysql://master:3306/spark_project --username hive --password 123 --table city_info --hbase-table city_info --column-family info --hbase-row-key city_id --hbase-create-table -m 1
--hbase-table user_action #HBase中表名称
--column-family f1 #列簇名称 //也有用info或者自定义,hbase导出需要注意
--hbase-row-key id #HBase 行键
--hbase-create-table #是否在不存在情况下创建表
-m 1 #启动 Map 数量
5.3 hbase -> hive
create external table city_info(city_id int, city_name string, area string) row format serde 'org.apache.hadoop.hive.hbase.HBaseSerDe' stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties('hbase.columns.mapping'=':key, info:city_name, info:area')tblproperties('hbase.table.name'='city_info');
//create external table hbase_hive_1(key String, value String) 指定hive表和列 stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties 指定hive到hbase应用的类 ("hbase.columns.mapping"="info:des,info:area") 指定hbase的列, 其中hbase的key即使不写也会被hive识别到,如果写写法为 ("hbase.columns.mapping"=":key,info:des,info:area") tblproperties("hbase.table.name"="f_stu") 指定hbase的表
5.4 hbase -> mysql
hbase中的数据不能够直接导出到mysql数据库中,需要借助一个中介实现,两种方法:
(1)将hbase中的数据导出到HDFS平台上,然后导入到mysql;
(2)将hbase中的数据导出到hive(中介),然后通过sqoop将hive中的数据导入到mysql数据库中。(主要是第二种)
先是如5.3 hbase -> hive 操作,将city_info名称改为city_info_external,以作区分
//在hive中新建一个内部表,将hive的external外部表数据导入到内部表。
create table city_info(city_id int, city_name string, area string);
//建好之后,通过以下命令,执行将hbase中的数据导出到hive中:
hive> insert overwrite table city_info_inner select * from city_info_external;
//接下来,就是利用sqoop将hive中的数据导入到mysql中了,转到sqoop目录下,执行以下命令:
sqoop export --connect jdbc:mysql://master:3306/sogou --username hive --password 123 -m 1 --table city_info --export-dir /root/hive/warehouse/sogou.db/city_info_inner --input-null-string "\\\\N" --input-null-non-string "\\\\N" --input-fields-terminated-by "\\01" --input-lines-terminated-by "\\n"
//报错: Input file不存在,在浏览器中找hive配置在什么位置,/root/hive/warehouse
//另外,还要确保mysql中的表已经创建好,一定要保证hive表是内部表,外部表无法导出
后边的--export-dir是本机的hive之中表的HDFS地址。至于这地址之后的,是处理防止导入到mysql之中时,出现NULL的现象,导致原因是因为hive里存储的数据和数据之间的间隔与mysql中存储的数据和数据间隔不一致。所以需要处理一下。使mysql可以识别。
参考:
https://my.oschina.net/dataRunner/blog/304274
5.5 hive -> mysql
见5.4 hbase -> mysql中hive作为中转的例子;
5.6 hive -> hbase
1. hive表
hive (sogou)>
>
> select * from city_info;
OK
0 北京 华北
1 上海 华东
2 南京 华东
2. hbase 创建空表,用于装载hive原表数据
create 'city_info', 'info'
3. hive创建hbase目标表的映射表:
hive (sogou)> create external table city_info_hbase(city_id int, city_name string, area string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties ("hbase.columns.mapping" = ":key,info:city_name, info:area") tblproperties ("hbase.table.name" = "city_info");
4. 将hive原表数据拷贝一份到 hive-hbase映射表
insert overwrite table city_info_hbase select * from city_info;
5. scan一下检测hbase 表数据scan 'city_info', {LIMIT=>10} //大写
hive到hbase,还是在hive中创建了一个hbase的外表,只不过此时hbase表没有数据,然后将hive中创建的这个habse外表数据装载hive原来表的数据而已。一般场景是以hbase表为基础,创建hive外表关联的场景,目的是给数据库的人员查询使用。
参考:
http://chengjianxiaoxue.iteye.com/blog/2279407
5.7 mysql -> hdfs
sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --target-dir /test -m 1
在上述的基础上,增量导入数据到hdfs中。
sqoop import --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test --target-dir /test -m 1 --check-column id --incremental append --last-value 11
5.8 hdfs -> mysql
由于hive的底层就是hdfs的一个基本文件,所以可以将hive导出数据转换为从hdfs导出数据。导出数据的时候,默认字段分割方式是',',所以如果hive的字段分割不是',',那么就需要设计成对应格式的分割符号。可以通过命令:sqoop help export查看export命令的详细参数使用方式&各个参数的含义。
注意:前提条件,关系型数据库中目的表已经存在。
将hdfs上的文件导出到关系型数据库test2表中。
sqoop export --connect jdbc:mysql://hh:3306/test --username hive --password hive --table test2 --export-dir /test