sqoop操作
点这里sqoop安装配置教程
使用需要hadoop伪分布式、集群或者高可用,以及hive
我测试的数据库为bigdata 表为goods
1.连接数据库
命令:
sqoop-list-tables --connect jdbc:mysql://localhost:3306/bigdata --username root --password 123456
--connect jdbc:mysql://localhost:3306/bigdata :数据库位置,localhost也可以是虚拟机ip地址
-
--username root:账号
-
--password 12345:密码
2.将数据库表存入hadoop
命令:
sqoop import --connect jdbc:mysql://localhost:3306/bigdata --username root --password 123456 --delete-target-dir --target-dir daoua --table goods --where "id > 3" --m 1 --fields-terminated-by '/' --driver com.mysql.jdbc.Driver
--connect jdbc:mysql://localhost:3306/bigdata :数据库位置,localhost也可以是虚拟机ip地址
--username root:账号
--password 12345:密码
--delete-target-dir:如果目录存在删除
--target-dir daout:数据库表要存的hadoop位置
--table goods:表名
--where "id > 3":筛选id>3的数据存入
--fields-terminated-by '/':指定数据分割符
--m 1:设置文件数量为1,否则一条记录会形成一个文件
--driver com.mysql.jdbc.Driver:mysql驱动包过低不加此会报错
3.将mysql数据库表增量追加存入hadoop
sqoop import --connect jdbc:mysql://192.168.88.100:3306/bigdata_db --username root --password 123456 --m 1 --table tb_goods --target-dir /user/increase --incremental append --check-column id --last-value 2 --driver com.mysql.jdbc.Driver
--incremental append:增量追加
--check-column id:以表中id增量追加
--last-value 2:从第二个后开始追加
4.hadoop存入mysql数据库表
sqoop export --connect jdbc:mysql://192.168.88.100:3306/bigdata?useUnicode=true\&characterEncoding=utf-8 --username root --password 123456 --table goods --export-dir /user/increase --fields-terminated-by ',' --m 2 --driver com.mysql.jdbc.Driver
useUnicode=true\&characterEncoding=utf-8:指定编码格式
--export-dir /user/increase:数据存在的hdfs位置
--fields-terminated-by ',':数据的分隔符,查看用 hdfs dfs -cat 文件路径
注意:mysql的数据库清空。
5.mysql数据库表存入hive表
sqoop import --m 1 --hive-import --connect jdbc:mysql://localhost:3306/bigdata?useUnicode=true\&characterEncoding=utf-8 --username root --password 123456 --table goods --fields-terminated-by '\t' --driver com.mysql.jdbc.Driver --hive-database test6 --hive-table goods;
--hive-database adds:存入的数据库
--hive-table goods: 存入的表,此表自动创建
6.hive表存入mysql数据库表
sqoop export --connect jdbc:mysql://localhost:3306/bigdata?useUnicode=true\&characterEncoding=utf-8 --username root --password 123456 --table goods --num-mappers 1 --export-dir /user/hive/warehouse/test6.db/goods/part-m-00000 --input-fields-terminated-by '\t';
--export-dir /user/hive/warehouse/test6.db/goods/part-m-00000:hadoop中表的路径