- 安装
1.安装过程就是解压
2.修改conf/目录下的sqoop-env-template.sh为sqoop-env.sh(应该改不改都行)文件里面不需要配置 因为环境变量里都有
3.然后就是添加环境变量
export SQOOP_HOME=/opt/sqoop
export PATH=$PATH:$SQOOP_HOME/bin
记得source /etc/profile
4.还要添加数据库驱动包(放在lib/目录下)
5.修改配置文件bin/configure-sqoop
请参考https://blog.youkuaiyun.com/wx1528159409/article/details/87948866
6.测试sqoopsqoop version
测试连接数据库
sqoop list-databases -connect jdbc:mysql://master.oppo.com:3306/ -username root -password 123456
- 使用sqoop从mysql将数据导入hdfs
sqoop import --connect jdbc:mysql://master.oppo.com/test --username root --password 123456 --table psn --columns id,name -m 1 --target-dir /sqoop
对上面命令解释如下:
--connect 连接数据库
--username
--password
--table 需要用的表(必填)
--columns 需要用到的列
-m mapper的个数
--target-dir 目标路径(hdfs上的)
要是需要覆盖原有的文件,只需要添加--delete-target-dir
你也可以用执行文件的方式执行sqoop
官网提供的例子:
$ sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST
$ sqoop --options-file /users/homer/work/import.txt --table TEST
第二种即为执行文件方式
文件的内容:
import
--connect
jdbc:mysql://master.decloud.com/t_user
--delete-target-dir
--username
root
--password
123456
--table
net
--columns
html
-m
1
--target-dir
/sqoop
需要注意的是用文件的方式执行需要将参数和值都放到单独的一行
在文件里可以加入空行和注释方便阅读
下面是官网的例子:
#
# Options file for Sqoop import
#
# Specifies the tool being invoked
import
# Connect parameter and value
--connect
jdbc:mysql://localhost/db
# Username parameter and value
--username
foo
#
# Remaining options should be specified in the command line.
#
当然也可以用sql语句执行
import
--connect
jdbc:mysql://master.decloud.com/t_user
--delete-target-dir
--username
root
--password
123456
-e
select * from t_test where id = 1 and $CONDITIONS
-m
1
--target-dir
/sqoop
你可能会疑惑sql语句为啥还有个$CONDITIONS
,上面是从官方文档截的图,红色括起来的地方明确要求添加,但是它本身没啥意义,加上就完事了。
- 导入hive
import
--connect
jdbc:mysql://master.decloud.com/t_user
--username
root
--password
123456
--table
net
--columns
html
-m
1
--target-dir
/sqoop2
--hive-home
/usr/local/hive
--hive-import
--hive-table
abc
--target-dir
在此处表示临时文件存储位置
- 从hive数据导出到mysql
export
--connect
jdbc:mysql://master.oppo.com/test
--username
root
--password
123456
-m
1
--columns
id,name
--export-dir
/sqoop/
--table
h_test
--export-dir
HDFS source path for the export
- hive和hbase整合
https://cwiki.apache.org/confluence/display/Hive/HBaseIntegration
1、把hive/lib目录下的hive-hbase-handler-1.2.1.jar cp到hbase/lib 下
同时把hbase中的所有的jar,cp到hive/lib
2、在hive的配置文件增加属性:
<property>
<name>hbase.zookeeper.quorum</name>
<value>node1,node2,node3</value>
</property>
3、在hive中创建临时表
(1) If you want to give Hive access to an existing HBase table,use CREATE EXTERNAL TABLE:
CREATE EXTERNAL TABLE tmp_order
(key string, id string, user_id string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,order:order_id,order:user_id")
TBLPROPERTIES ("hbase.table.name" = "t_order");
创建外部表的方式需要先在hbase中创建表t_order
key为行键,后面为Hbase的列,hbase.columns.mapping的结构是:{key,列族:列名,列族:列名)
hbase.table.name填入hbase中的表名
(2) In order to create a new HBase table which is to be managed by Hive,use the STORED BY clause on CREATE TABLE:
CREATE TABLE hbasetbl(key int, value string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,cf1:val")
TBLPROPERTIES ("hbase.table.name" = "xyz", "hbase.mapred.output.outputtable" = "xyz");
创建内部表后,其内容存储在hbase中,而不是由hive来维护