一、hive 数据导入导出

1、distcp 分布式拷贝

新旧集群之间如果能直接通讯,在不考虑影响业务的情况下,最便捷的方式是使用分布式拷贝,但是又分为相同版本和不同版本直接拷贝,以下为相同版本之间拷贝的方式。

hadoop distcp -D ipc.client.fallback-to-simple-auth-allowed=true  hdfs://10.1.42.51:8020/user/hive/warehouse/default/guangxi_live.db hdfs:// 10.1.42.60:8020/user/hive/warehouse/default

2、export/import 拷贝

简单导入和导出

export table department to 'hdfs_exports_location/department';
import from 'hdfs_exports_location/department';

导出分区并且导入

export table employee partition (emp_country="in", emp_state="ka") to 'hdfs_exports_location/employee';
import from 'hdfs_exports_location/employee';

导出表并且导入到分区表分区

export table employee to 'hdfs_exports_location/employee';
import table employee partition (emp_country="us", emp_state="tn") from 'hdfs_exports_location/employee';

在工作中,由于数据表数据量超级大,无法全量导出,可以通过kettle去连接hive或者impala,用sql语句查出目标数据,然后导出到文本文件(CSV表)

为了让数据导入分区表中,我们需要设定一些参数,让数据库支持动态分区,在hive终端输入以下命令:

set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions.pernode =1000000;

另外,在hive中创建表的时候,需要创建一张已,默认格式TEXTFILE的中间表,这样才能把csv格式文件导入表中,其他格式的都不支持。

其中TEXTFILE为默认格式,建表时不指定默认为这个格式,导入数据时会直接把数据文件拷贝到hdfs上不进行处理; SEQUENCEFILE,RCFILE,ORCFILE,PARQUET格式的表不能直接从本地文件导入数据,数据要先导入到textfile格式的表中, 然后再从表中用insert导入SequenceFile,RCFile,ORCFile,PARQUET各自表中;或者用复制表结构及数据的方式(create table as select * from table )。
一言以蔽之:如果为textfile的文件格式,直接load就OK,不需要走MapReduce;如果是其他的类型就需要走MapReduce了,因为其他的类型都涉及到了文件的压缩,这需要借助MapReduce的压缩方式来实现。

 CREATE TABLE `guangxi_test2`(
  `stbid` string,
  `areacode` string,
  `starttime` string,
  `endtime` string,
 `day` string,
  `content` string)
ROW FORMAT DELIMITED
  FIELDS TERMINATED BY ',';  

把数据导入hdfs—insert进临时表—查询结果:

load data local inpath  '/opt/query.csv' into table test ;
insert into table formmart partition (day,content) select * from test;
select count(*) from guangxi_test;

二、hbase

1、export

hbase org.apache.hadoop.hbase.mapreduce.Export tsdb-uid /copy/tsdb-uid
例如:从dt_gpsdata表中导出2018-01-01至2018-02-01的数据(中间记得加上version)

hbase org.apache.hadoop.hbase.mapreduce.Export dt_gpsdata /copy/dt_gpsdata 1 1514736000000 1517414400000

2、import

hbase org.apache.hadoop.hbase.mapreduce.Import tsdb-uid /copy/tsdb-uid

3、数据查询

按时间段查询hbase

scan 'test',{COLUMNS=>'info',TIMERANGE=>[1667463331656,1667463389253]}

模糊查询,类似like

scan 'guangxi_live_test',{FILTER=>"RowFilter(=,'substring:02')"}

substring筛选时使用=来筛选,否则会出错;binary筛选时可以用=、>=、<=等操作

根据主键进行过滤
RowFilter(=,‘substring:111’) 主键中包含111
RowFilter(=,‘binary:111’) 主键等于111
PrefixFilter(‘user1’) 主键的前缀是user1

根据列进行过滤
列名过滤
QualifierFilter (=, ‘substring:p’) 列名中包含p
QualifierFilter (=, ‘binary:p’) 列名等于p
MultipleColumnPrefixFilter(‘a’,‘b’,‘e’) 列名的前缀是a或者b或者e
ColumnPrefixFilter(‘c2’) 列名的前缀是c2

列值过滤
SingleColumnValueFilter(‘i’, ‘path’, =, ‘substring:student’) 列族为i,列名为path,列值包含student
SingleColumnValueFilter(‘i’, ‘path’, =, ‘binary:student’) 列族为i,列名为path,列值等于student
ValueFilter(=,‘substring:111’) 列值中包含111
ValueFilter(=,‘binary:111’) 列值等于111

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐