在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. Android新浪微博client(一)——主框架搭建

    原文出自:方杰| http://fangjie.sinaapp.com/?p=62 转载请注明出处 该项目代码已经放到github:https://github.com/JayFang1993/Sin ...

  2. mapreduce学习资料

    http://blog.csdn.net/tianjun2012/article/category/6794531 http://blog.csdn.net/tianjun2012/article/d ...

  3. jsonp 小结

    JSONP是JSON with Padding的略称. 它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域访问(这仅仅是JSONP简单的实 ...

  4. No breeds found in the signature, a signature update is recommended

    cobbler 2.6.11 遇到这个问题,需要 >> cobbler signature update >> and cobblerd restart 转自: https:/ ...

  5. shiro 实现自己定义权限规则校验

    <span style="font-family: Arial, Helvetica, sans-serif;">在系统中使用shiro进行权限管理,当用户訪问没有权限 ...

  6. Intellij idea 切换SVN路径

    一直不懂如何切换路径,每次都是删除---->检出:本地源码都不能保存下来,非常麻烦 //在idea中svn切换到新分支:[vcs] -> [subversion] -> [updat ...

  7. js中 opener和parent的差别

    opener即谁打开我的,比方A页面利用window.open弹出了B页面窗体.那么A页面所在窗体就是B页面的opener.在B页面通过opener对象能够訪问A页面. parent表示父窗体,比方一 ...

  8. 七. PHP模式设计----运行及描写叙述任务

    1. 解析器模式 //解析器内容类 //用于存放表达式的运算结果,并且能依据传入的表达式返回当初记录的结果 class InterpreterContext{ private $expressions ...

  9. DrawRightEditText自定义EditText实现有内容时右侧图标按钮显示无内容时右侧图标按钮隐藏加上为空时晃动动画(二)

    经过大神指导,上面封装的还不够全面,触摸事件应该也放进自定义中去,那么问题来了,怎么区分呢!,这就涉及到了自定义属性的介绍了 我通过设置属性来判断在onTouch事件中应该进行什么操作,接下来看看改良 ...

  10. Wrapper配置详解及高级应用

      将一个简单的程度如HelloWorld 的应用包装秤Wrapper 服务并不复杂,甚至可以认为非常简单.但是实际项目应用过程中我们的程序一般较庞大,运行环境也较复杂. 通过Wrapper 配置文件 ...