hadoop系列: 数据转换工具sqoop

要点如下:

  1. 安装配置
  2. 关系型数据库<-->hadoop类型数据:互相转换
  3. 使用 sqoop job

出现的异常: ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf

( ---->解决办法: cp /soft/hive/lib/hive-common-**.jar    /soft/sqoop/lib/)


 mysql 数据--> hadoop( hdfs, hive, hbase)

#mysql: 表结构
CREATE TABLE `mysql_music` (
	  `id` int(11) NOT NULL AUTO_INCREMENT,
	  `mname` varchar(100) DEFAULT NULL,
	  `mtime` varchar(20) DEFAULT NULL,
	  `malbum` varchar(100) DEFAULT NULL,
	  `msinger` varchar(100) DEFAULT NULL,
	  `mstyle` varchar(20) DEFAULT NULL,
	  `mlanguage` varchar(255) DEFAULT NULL,
	  `misfree` int(11) DEFAULT NULL,
	  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

mysql-->hdfs

--数据导入到hdfs文件系统===========================
---delete模式
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue  \
--target-dir /user/centos/music \
--delete-target-dir -m 2 --table music --fields-terminated-by '\t'


---append模式
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--target-dir /user/centos/music \
--append -m 2 --table music --fields-terminated-by '\t'

---使用查询: 转储
sqoop import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--target-dir /user/centos/music222 \
--append -m 2 --fields-terminated-by '\t' \
-e 'select mname,malbum,msinger from music where id <10 and $CONDITIONS' \
--split-by 'mname'


--指定文件存储格式
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue  \
--target-dir /user/centos/music \
--append -m 2 --table music --fields-terminated-by '\t' \
--as-sequencefile


--指定压缩格式
sqoop import --connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--target-dir /user/centos/musicCompressed \
--append -m 1 --fields-terminated-by '\t' \
--table music --fields-terminated-by '\t' \
--compression-codec 'org.apache.hadoop.io.compress.GzipCodec'

mysql-->hive, hbase

--数据导入到hive表===========================
hdfs dfs -rmr music
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--table music \
--create-hive-table --fields-terminated-by '\t' \
--hive-database db1 \
--hive-table music3 \
--hive-import

字段映射
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--table music \
--fields-terminated-by '\t' \
--hive-database db1 \
--hive-table music9  \
--hive-import  \
--map-column-hive id=string

--2、增量导入:
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue --table music \
--fields-terminated-by '\t' \
--hive-database db1 --hive-table music3 \
--hive-import \
--incremental append --check-column id --last-value 624

--3,分区导入
sqoop import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue --table music \

--hive-overwrite --hive-partition-key day \
--fields-terminated-by '\t' \
--hive-database db1 --hive-table music7 \
--hive-import \
--hive-partition-value 2018-08-20 

===导入数据到hbase===============================
sqoop import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue --table music \
--hbase-create-table  \
--hbase-table  db1:music \
--hbase-row-key id \
--column-family f1 

sqoop 定义job

===job
sqoop job --create myjob -- import \
--connect jdbc:mysql://localhost:3306/db --username root --password daitoue \
--table music \
--create-hive-table --fields-terminated-by '\t' \
--hive-database db1 \
--hive-table jobmusic \
--hive-import

--执行job
sqoop job --exec myjob

创建mysql表: mysql_music2, 从hdfs中插入数据

====hdfs数据导入mysql: (--update-mode allowinsert 当key不存在则插入)
sqoop export --export-dir /user/hive/warehouse/db1/music/day=2018-08-20/  \
--fields-terminated-by '\t' \
--columns id,mname,mtime,malbum,msinger,mstyle,mlanguage,misfree \
--connect jdbc:mysql://localhost:3306/db  \
--username root --password root \
--table mysql_music2 \
--update-key id \
--update-mode allowinsert

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/81877139