Hive 批量数据迁移

测试环境
HDP 2.6.2 到 HDP 2.5.0
hdfs 2.7.3 到 hdfs 2.7.1
两个集群都没有启用kerberos以及ranger权限
1. 通过hive export/import 迁移数据
1.1 导出hive表数据
beeline -u "jdbc:hive2://dc2.xx.com:2181,dc3.xx.com:2181,dc4.xx.com:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n "hive"
export table customer to '/tmp/export/customer' ;
1.2 采用distcp把文件夹导出到目标系统
hadoop distcp hdfs://dc1.xx.com:8020/tmp/export/ hdfs://dc2.xx.com:8020/tmp/export
1.3 在目标系统中把表导入
beeline -u "jdbc:hive2://dc2.xx.com:2181,amb03.v120.ubuntu:2181,amb02.v120.ubuntu:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2" -n "hive"
import from '/tmp/export/customer';
import table ds_customer from '/tmp/export/customer';
1.4 特殊表导入导出
1.4.1 导出整个分区表
源端 hive beeline 
export table saleslineitem_orc to '/tmp/export/saleslineitem_orc' ;
export table saleslineitem_parquet to '/tmp/export/saleslineitem_parquet' ;
同步数据
hadoop distcp hdfs://dc1.xx.com:8020/tmp/export/ hdfs://dc2.xx.com:8020/tmp/export
目标端
import from '/tmp/export/saleslineitem_orc';
import from '/tmp/export/saleslineitem_parquet';
检查
show tables;
show partitions saleslineitem_orc ;
1.4.2 导出一个分区并导入到指定分区
目标端执行
export table saleslineitem_orc partition(dt='20171216') to '/tmp/export/saleslineitem_orc_dt_20171216' ;
同步数据
hadoop distcp hdfs://dc1.xx.com:8020/tmp/export/ hdfs://dc2.xx.com:8020/tmp/export
目标端执行
alter table saleslineitem_orc drop partition(dt='20171216');
import from '/tmp/export/saleslineitem_orc_dt_20171216'


注意事项: 
1. 目标分区必须不存在
2. 不能把导出得分区导入到其他分区,例如导出得是 dt='20171216' 分区数据,不能使用 import table saleslineitem_orc partition(dt='20171215') from '/tmp/export/saleslineitem_orc_dt_20171216' 导入到201715分区中


2. 通过distcp 命令导出到目标集群,然后建立表
此出省略
3. 总结
可以使用hive export/import 进行hive数据的批量迁移,本实验测试了text,orc,parquet,分区表,并测试了不同版本的导入导出。理论上hive导入导出的数据迁移不受版本,数据格式以及表的限制,可以得出结论可以适应hive export/import进行任何hive数据的迁移


4. 使用脚本
create table if not exists proc.saleslineitem (
saleslineitemid int,
productid int,
customerid int,
quantity int,
extendedamount float ,
transactiondate timestamp
)
comment 'saleslineitem information'
row format delimited
fields terminated by '|'
lines terminated by '\n'
stored as textfile;


load data inpath '/tmp/saleslineitem.txt' overwrite into table proc.saleslineitem;


create table if not exists proc.saleslineitem_orc (
saleslineitemid int,
productid int,
customerid int,
quantity int,
extendedamount float ,
transactiondate timestamp
) partitioned by (dt string)
stored as orc;


create table if not exists proc.saleslineitem_parquet (
saleslineitemid int,
productid int,
customerid int,
quantity int,
extendedamount float ,
transactiondate timestamp
) partitioned by (dt string)
stored as parquet;


insert overwrite table proc.saleslineitem_orc partition(dt='20171216') select * from saleslineitem;
insert overwrite table proc.saleslineitem_orc partition(dt='20171217') select * from saleslineitem;
insert overwrite table proc.saleslineitem_orc partition(dt='20171218') select * from saleslineitem;


show partitions saleslineitem_orc
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nostrick;


insert overwrite table proc.saleslineitem_parquet partition(dt) select * from proc.saleslineitem_orc;

### 如何批量插入数据到Hive表 在大数据处理场景中,批量插入数据到Hive表是一种常见的需求。以下是几种常用方法及其具体实现: #### 方法一:使用 `LOAD DATA` 命令 当数据已经存储在 HDFS 上时,可以通过 `LOAD DATA` 命令将文件中的数据加载到 Hive 表中。这种方法适用于大规模静态数据的导入。 ```sql LOAD DATA INPATH '/path/to/data.csv' INTO TABLE my_hive_table; ``` 此命令会将指定路径下的文件移动到 Hive 表对应的 HDFS 存储位置[^2]。需要注意的是,该操作不会解析文件内容,而是简单地将其物理地址关联至目标表。 --- #### 方法二:创建外部表并加载数据 对于需要频繁更新的大规模数据集,可以先创建一个外部表来指向原始数据文件的位置,然后再通过 SQL 查询的方式读取这些数据。 1. 创建外部表: ```sql CREATE EXTERNAL TABLE IF NOT EXISTS external_my_table ( id STRING, name STRING, age INT ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LOCATION '/path/to/external/table'; ``` 2. 将外部表的数据插入内部表: ```sql INSERT INTO internal_my_table SELECT * FROM external_my_table; ``` 这种方式适合于从多个不同的源文件中抽取数据,并最终统一存放到单个 Hive 表中[^1]。 --- #### 方法三:利用 Hive 的 Bulk Load 功能 如果目标是高效地将大量数据写入 Hive 和 HBase 联合使用的环境,则推荐采用 **Bulk Load** 技术。它能够显著提升性能,尤其是在涉及大容量数据迁移的情况下。 ##### 步骤概述: 1. 编写 MapReduce 或 Spark 程序生成 HFile 文件; 2. 使用 HBase 提供的工具完成实际数据上传过程; 3. 更新元数据信息使得 Hive 可见新加的内容。 虽然配置较为复杂,但对于特定应用场景来说是非常值得投入时间去学习掌握的技术之一[^4]。 --- #### 方法四:借助 Sqoop 工具导出关系型数据库中的数据 当面对传统的关系型数据库(如 MySQL、PostgreSQL 或者本案例提到的 SQL Server),则可通过 Apache Sqoop 实现自动化桥接工作流程。 例如要将来自 SQL Server 数据库的一个视图结果迁移到 Hive 中作为永久记录保存下来,按照如下步骤执行即可: 1. 准备必要的 JDBC 驱动程序包放置到合适目录下; 2. 测试网络连通性和认证机制有效性; 3. 启动正式任务提交脚本运行。 下面给出一段简单的 sqoop import 示例代码片段用于演示目的: ```bash sqoop import \ --connect jdbc:sqlserver://<host>:<port>;database=<dbname> \ --username <user> --password <pass> \ --query 'SELECT col1,col2,... FROM view_name WHERE $CONDITIONS' \ --split-by col1 \ --target-dir /tmp/sqoop_output \ --fields-terminated-by ',' \ --hive-import --create-hive-table \ --hive-table hive_db.hive_table_name ``` 这里特别强调一点,在某些情况下可能还需要额外调整参数设置以满足特殊业务逻辑要求[^5]。 --- ### 总结 以上介绍了四种主流技术手段帮助开发者顺利完成大批量数据注入 Hive 平台的任务。每种方案都有各自适用范围及优缺点,请根据实际情况灵活选用最合适的那一种。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值