在hive中表的类型:管理表和托管表(外部表)。

内部表也称之为MANAGER_TABLE,默认存储在/user/hive/warehouse下,也可以通过location指定;删除表时,会删除表的数据以及元数据;

外部表称之为EXTERNAL_TABLE。在创建表时可以自己指定目录位置(LOCATION),数据存储所在的目录;删除表时,只会删除元数据不会删除表数据;

创建外部表实例

create external table if not exists default.emp_ext(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
row format delimited fields terminated by '\t'
location '/opt/input/emp';

分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所以的数据文件。hive中的分区就是分目录,把一个大的数据集根据业务需要分割成更小的数据集。

在查询时通过WHERE子句中的表达来选择所需要的指定的分区,这样的查询效率会提高很多。

create external table if not exists default.emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by(month string)
row format delimited fields terminated by '\t';

分区表注意事项:

修复表:msck repair table table_name;

可以写shell脚本

dfs -mkdir -p /user/hive/warehouse/dept_part/day=;
dfs -put /opt/weblog/log.log /user/hive/warehouse/dept_part/day=; alter table dept_part and partition('day=20171025');

查看表的分区数:show partitions dept_part;

导入数据进入hive表

load data [local] inpath 'filepath' [overwrite] into table tablename into tablename [partition (partcol1=val,...)];

参数带local意思是本地文件,不带就是HDFS文件

参数带overwrite意思是覆盖原本文件的内容,不带就追加内容

分区表加载,特殊性partition (partcol1=val,...)

1.加载本地文件到hive表

load  data  local  inpath '/root/emp.txt' into  table  default.emp

2.加载hdfs文件到hive表中

load  data  inpath '/root/emp.txt' into  table  default.emp

3.加载数据覆盖表中已有的数据

load  data  inpath '/root/emp.txt' overwrite into  table  default.emp

4.创建表是通过insert加载

create table    default.emp_ci like emp;
insert into table default.emp_ci select * from default.emp;

5.创建表的时候通过指定location指定加载

导出hive表数据

insert overwrite local directory '/opt/datas/hive/hive_exp_emp'  select * from default.emp 

row format delimited fields terminated by '\t';

#bin/hive -e "select * from default.emp;" > /opt/datas/hive/exp_res.txt

hive表多重插入
假如有一个需求:
从t_4中筛选出不同的数据,插入另外两张表中;

insert overwrite table t_4_st_lt_200 partition(day='')
select ip,url,staylong from t_4 where staylong<; insert overwrite table t_4_st_gt_200 partition(day='')
select ip,url,staylong from t_4 where staylong>;

但是以上实现方式有一个弊端,两次筛选job,要分别启动两次mr过程,要对同一份源表数据进行两次读取
如果使用多重插入语法,则可以避免上述弊端,提高效率:源表只要读取一次即可

from t_4
insert overwrite table t_4_st_lt_200 partition(day='')
select ip,url,staylong where staylong<
insert overwrite table t_4_st_gt_200 partition(day='')
select ip,url,staylong where staylong>;

Hive- 表的更多相关文章

  1. hive 表分区操作

    hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念    hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...

  2. 如何快速把hdfs数据动态导入到hive表

    1. hdfs 文件   {"retCode":1,"retMsg":"Success","data":[{" ...

  3. HDFS文件和HIVE表的一些操作

    1. hadoop fs -ls  可以查看HDFS文件 后面不加目录参数的话,默认当前用户的目录./user/当前用户 $ hadoop fs -ls 16/05/19 10:40:10 WARN ...

  4. 用puthivestreaming把hdfs里的数据流到hive表

    全景图:   1. 创建hive表 CREATE TABLE IF NOT EXISTS newsinfo.test( name STRING ) CLUSTERED BY (name)INTO 3 ...

  5. spark使用Hive表操作

    spark Hive表操作 之前很长一段时间是通过hiveServer操作Hive表的,一旦hiveServer宕掉就无法进行操作. 比如说一个修改表分区的操作 一.使用HiveServer的方式 v ...

  6. spark+hcatalog操作hive表及其数据

    package iie.hadoop.hcatalog.spark; import iie.udps.common.hcatalog.SerHCatInputFormat; import iie.ud ...

  7. 【原】创建Hive表,分号分隔符“;”引起的异常

    [障碍再现] 在创建支持Map数据结构的Hive表时,抛出如下异常 hive> create table tab_map(name string,info map<string,strin ...

  8. Hive表分区

    必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...

  9. 导hive表项目总结(未完待续)

    shell里面对日期的操作 #!/bin/bash THIS_FROM=$(date +%Y%m%d -d "-7 day") THIS_TO=$(date +%Y-%m-%d - ...

  10. 使用spark对hive表中的多列数据判重

    本文处理的场景如下,hive表中的数据,对其中的多列进行判重deduplicate. 1.先解决依赖,spark相关的所有包,pom.xml spark-hive是我们进行hive表spark处理的关 ...

随机推荐

  1. 使用spring-boot-admin对spring-boot服务进行监控(转自牛逼的人物)

    尊重原创:http://www.cnblogs.com/ityouknow/p/8440455.html 上一篇文章<springboot(十九):使用Spring Boot Actuator监 ...

  2. YUV格式学习汇总

    本文为个人学习使用,部分内容摘自他人. 参考: https://www.cnblogs.com/ALittleDust/p/5935983.html http://www.cnblogs.com/az ...

  3. lua table库

      整理自:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html 1.table.concat(table, sep,  st ...

  4. JFinal中json的使用

    之前Java开发一直使用的是经典的ssh,去年接触了jfinal,觉得jfinal的魅力非常之大,让我无法自拔,现在还深深地陷在其中. 简单的介绍一下jfinal,jfinal短小精悍,让java有了 ...

  5. Agri-Net - poj 1258 (Prim 算法)

      Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44373   Accepted: 18127 Description F ...

  6. 【SSH进阶之路】Hibernate基本映射(三)

    [SSH进阶之路]Hibernate基本原理(一) ,小编介绍了Hibernate的基本原理以及它的核心.採用对象化的思维操作关系型数据库. [SSH进阶之路]Hibernate搭建开发环境+简单实例 ...

  7. mysql ODBC连接配置

    1.软件包安装 [root@server1 mysql]# rpm -ivh MySQL-server-5.1.72-1.glibc23.i386.rpm [root@server1 mysql]# ...

  8. 如何正确对tomcat host进行配置

    今天在对tomcat的host容器(即虚拟主机的配置)进行配置时,发现即使修改了host name的值(默认为localhost),但是仍无法访问web项目的问题(提示域名解析出错).只能使用默认的值 ...

  9. Struts中类型转换踩的坑

    出现的异常: 当我输入的数据很大时候,转换后如上,这并不是我想要的, 出现问题的原因: Struts2对常用的数据类型如String.Integer.Double等都添加了转换器进行对应的转换操作. ...

  10. What happens when we continue stacking deeper layers on a “plain” convolutional neural network?

    http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture9.pdf The deeper model performs worse, but ...