import输入:
sqoop import --params
sqoop-import --params
- 如果分割字段是文本:-Dorg.apache.sqoop.splitter.allow_text_splitter=true
- 如果表没有主键或者使用sql取数需要参数:--split-by
- 数据量如果较大则使用压缩:-z,--compress --compression-codec <c>
- 设定map的数量,很重要的一个参数:-m,--num-mappers <n>
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.170:3306/card \
--driver com.mysql.jdbc.Driver \
--username root \
--password 1234 \
--verbose \
--table student \
--target-dir /sqoop_student \
--as-textfile \
--split-by s
--一个map方法
sqoop-import \
--connect jdbc:mysql://192.168.5.170:3306/card \
--driver com.mysql.jdbc.Driver \
--username root \
--password 1234 \
--verbose \
--table student \
--target-dir /sqoop_student \
--as-textfile \
--delete-target-dir \
-m 1
使用sql:
- SQL中必须包含where 必须包含$CONDITIONS
- sql是使用单引号来包裹的
- select *from student where sname='aaa'
- 使用双引号来代替包裹sql的单引号,但是$CONDITIONS必须要写成:\$CONDITIONS
例;--query "select *from student where sname='aaa' where \$CONDITIONS"
- 因为使用sql就没有主键,因此使用sql要和split-by结合使用
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.170:3306/card \
--driver com.mysql.jdbc.Driver \
--username root \
--password 1234 \
--verbose \
--query 'SELECT a.*, b.score, c.c, c.cname, c.t FROM student a LEFT JOIN sc b ON a.s=b.s LEFT JOIN course c ON b.c=c.c WHERE $CONDITIONS' \
--split-by a.s \
--target-dir /sqoop_student_score \
--as-textfile \
--delete-target-dir \
导入数据到hbase中
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.170:3306/card \
--driver com.mysql.jdbc.Driver \
--username root \
--password 1234 \
--verbose \
--table student \
--split-by S \
--column-family i \
--hbase-create-table \
--hbase-row-key S \
--hbase-table sqoop_student
- hbase-row-key在mysql中字段名称大小写敏感
- 对版本有要求,hbase1.2.3成功
导入到hive表中
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.170:3306/card \
--driver com.mysql.jdbc.Driver \
--username root \
--password 1234 \
--verbose \
--table student \
--split-by S \
--hive-home /opt/hive/apache-hive-2.3.3-bin \
--hive-import \
--hive-overwrite \
--create-hive-table \
--hive-table sqoop_student
sqoop-1.4.7.bin__hadoop-2.6.0 导入hive报错:
- 把hive安装目录下的lib目录的所有的jar包拷贝到sqoop安装目录里的lib目录下
cp $HIVE_HOME/lib/* $SQOOP_HOME/lib/
- 删除sqoop/lib目录下的
rm -f icu4j-4.8.1.jar
- 环境变量中要有:
export HIVE_CONF_DIR=$HIVE_HOME/conf
- 要保证hive的hive-site.xml配置文件里的元数据连接url是主机名不是localhost
增量导入:
需要导入的数据
- 维度数据
- 事实数据
- 维度数据:
- 数据量不算太大
- 增量不会太大
- 可以跨周期,可以全量
- 也可以随着周期增量导入
- 事实数据
- 数据量巨大
- 增量巨大
- 不能跨周期,不能全量,只能随着周期增量
增量实现:
- 用sql实现:
- select * from order where $CONDITION and op_date=${now_day}
- --table和--where结合使用
- 选择字段来取不同周期的数据
- 每次增量导入给原始数据打上已导入标签
- sqoop实现:
- --incremental
告诉sqoop如何确定那些数据是增量数据--incremental append --check-column op_date --last-value 20180502 ---------------------------------------- --incremental lastmodified --check-column lastmodified_timestamp --last_value 144203040200
append :数据是根据某个字段连续递增产生的
lastmodified :每当数据增量变化会更新某个操作日 - --check-column
指定字段,用来过滤增量数据
它的类型可以是时间和数值,不能是字符串 - --last-value
指定上一次导入的被比较字段的最大值
具体实现:
1.找一个id是递增的数值的表
2.用sqoop把它导入到hdfs上
3.导入语句上加上
--incremental append
--check-column op_date
--last-value 20180502
4.往关系型数据库表中再增加一条记录
再次执行导入语句,更改
--last-value 为上次导入的id的最大值
5.注意入hdfs时用
--append
6.数据量小时可以加
-m 1
当数据量大时通过-m num来配置数据导入的并行度
CREATE TABLE for_sqoop_imp_inc(
inc_id INT NOT NULL AUTO_INCREMENT
,str1 VARCHAR(20)
,str2 VARCHAR(20)
,str4 VARCHAR(20)
,PRIMARY KEY(inc_id)
)
INSERT INTO for_sqoop_imp_inc(str1,str2,str4) VALUES('a','b','c')
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.155:3306/test3 \
--driver com.mysql.jdbc.Driver \
--username root \
--password sa \
--verbose \
--table for_sqoop_imp_inc \
--target-dir /for_sqoop_imp_inc \
--append \
--as-textfile \
--split-by inc_id \
--incremental append \
--check-column inc_id \
-m 1 \
第一次可以不用加--last-value
sqoop-import -Dorg.apache.sqoop.splitter.allow_text_splitter=true \
--connect jdbc:mysql://192.168.5.155:3306/test3 \
--driver com.mysql.jdbc.Driver \
--username root \
--password sa \
--verbose \
--table for_sqoop_imp_inc \
--target-dir /for_sqoop_imp_inc \
--append \
--as-textfile \
--split-by inc_id \
--incremental append \
--check-column inc_id \
-m 1 \
--last-value 13
后面再不加last-value的话数据就会重复导入了
export输出:
sqoop export \
--connect jdbc:mysql://192.168.5.176:3306/test3 \
--driver com.mysql.jdbc.Driver \
--username root \
--password sa \
--verbose \
--table sqoop_department \
--export-dir /external/dep \
--input-fields-terminated-by '\t'
导出数据需要关系型数据库事先要建好表
create table sqoop_department(
dep_id varchar(20),
dep_name varchar(50),
address varchar(100)
)