sqoop学习笔记&hive和hbase整合

本文档详细介绍了如何安装和配置Sqoop,包括安装步骤、添加环境变量、放置数据库驱动以及测试数据库连接。此外,还阐述了使用Sqoop从MySQL导入HDFS数据,以及如何将数据导入和导出到Hive与HBase之间的整合操作,如在Hive中创建HBase外部表和内部表的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 安装
    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来维护

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值