• hive和hbase的一些数据导入导出操作


    一、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
    
    • 1

    2、export/import 拷贝

    简单导入和导出

    export table department to 'hdfs_exports_location/department';
    import from 'hdfs_exports_location/department';
    
    • 1
    • 2

    导出分区并且导入

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

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

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

    在工作中,由于数据表数据量超级大,无法全量导出,可以通过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;
    
    • 1
    • 2
    • 3

    另外,在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 ',';  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    把数据导入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;
    
    • 1
    • 2
    • 3

    二、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
    
    • 1

    2、import

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

    3、数据查询

    按时间段查询hbase

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

    模糊查询,类似like

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

    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

  • 相关阅读:
    JVM面试题
    openpyxl关于区域的操作
    数据库系统的重做日志大体上长得什么样?(数据库理论)
    如何配置Header Editor
    Redis架构之哨兵机制与集群
    常见的LED显示屏拼接优缺点解析
    2019年互联网高频Java面试题指南!互联网升职加薪方案!
    Kotlin Sequence 是时候派上用场了
    Vue3类与样式绑定
    Python获取Window系统注册表安装的软件,再卸载软件
  • 原文地址:https://blog.csdn.net/quantam/article/details/127966649