Sqooop1

import输入:

sqoop import --params
sqoop-import --params

  1. 如果分割字段是文本:-Dorg.apache.sqoop.splitter.allow_text_splitter=true
  2. 如果表没有主键或者使用sql取数需要参数:--split-by
  3. 数据量如果较大则使用压缩:-z,--compress  --compression-codec <c>
  4. 设定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:

  1. SQL中必须包含where 必须包含$CONDITIONS
  2. sql是使用单引号来包裹的
  3. select *from student where sname='aaa'
  4. 使用双引号来代替包裹sql的单引号,但是$CONDITIONS必须要写成:\$CONDITIONS 
     例;--query "select *from student where sname='aaa' where \$CONDITIONS"
  5. 因为使用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
  1. hbase-row-key在mysql中字段名称大小写敏感
  2. 对版本有要求,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报错:

  1. 把hive安装目录下的lib目录的所有的jar包拷贝到sqoop安装目录里的lib目录下
        cp $HIVE_HOME/lib/* $SQOOP_HOME/lib/
  2. 删除sqoop/lib目录下的
     rm -f icu4j-4.8.1.jar
  3. 环境变量中要有:
    export HIVE_CONF_DIR=$HIVE_HOME/conf
  4. 要保证hive的hive-site.xml配置文件里的元数据连接url是主机名不是localhost

 增量导入:

 
  • 需要导入的数据
  1. 维度数据
  2. 事实数据
  • 维度数据:
  1. 数据量不算太大
  2. 增量不会太大
  3. 可以跨周期,可以全量
  4. 也可以随着周期增量导入
  • 事实数据
  1. 数据量巨大
  2. 增量巨大
  3. 不能跨周期,不能全量,只能随着周期增量
增量实现:
  •  用sql实现:
  1. select * from order where $CONDITION and op_date=${now_day}
  2. --table和--where结合使用
  3. 选择字段来取不同周期的数据
  4. 每次增量导入给原始数据打上已导入标签
  • sqoop实现:
  1. --incremental
    --incremental append
    --check-column op_date
    --last-value 20180502
    ----------------------------------------
    --incremental lastmodified
    --check-column lastmodified_timestamp
    --last_value 144203040200 
    告诉sqoop如何确定那些数据是增量数据
    append :数据是根据某个字段连续递增产生的
    lastmodified :每当数据增量变化会更新某个操作日
  2. --check-column
    指定字段,用来过滤增量数据
    它的类型可以是时间和数值,不能是字符串
  3. --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)
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值