美文网首页
Hive常用操作

Hive常用操作

作者: _张旭 | 来源:发表于2018-07-14 17:13 被阅读0次

创建表

简单的建表

create table
spec_table_from_textfile(
  id bigint,
  table_code string,
  table_name string,
  table_common string,
  column_code string,
  column_name string,
  column_common string)

从查询结果创建表

create table table2
as select phone_num,real_name,id_card,create_time 
from table1 
where info_from like '%18%'

克隆表

create table like tablename1
  • 此处是指克隆表结构,并不会克隆表数据。

创建从格式化文本文件导入的表

create table
spec_table_from_textfile(
  id bigint,
  table_code string,
  table_name string,
  table_common string,
  column_code string,
  column_name string,
  column_common string)
row format delimited
fields terminated by ','
stored as textfile

创建 orc 表

create table
spec_table_orc(
  id bigint,
  table_code string,
  table_name string,
  table_common string,
  column_code string,
  column_name string,
  column_common string)
clustered by (id) into 3 buckets
stored as orc TBLPROPERTIES ('transactional'='true')
  • ORC表支持行级delete、updata和insert等操作,但需要配置HIVE相关配置文件,具体操作点击链接

导入数据至表

Hive表接受批量插入,一般不支持逐条记录插入的insertSQL语句。

从查询数据导入

若目标表和数据源表的表结构一至:

insert into table xx select * from xx where xxx
  • 字段数据结构不一致Hive会尝试转换,转换不了的置NULL

导入时指定特殊值

insert into
spec_test(
  id,
  table_code,
  table_name,
  filed_code,
  filed_name,
  category
)
select *,'zx'
from spec_table2
  • 一种适用的场景是在表合并时,对目标表新增的字段指定来源。

选择字段导入

insert into table
table1(
  wp_id,
  wp_phone,
  wp_email,
  wp_sex,
  wp_birthday)
select * from table2

从文本文件导入

如上一小节创建从格式化文本文件导入的表,对应的建表后可在hive-shell中指定文本文件路径导入,需要注意的是文本文件中字段和字段之间的分隔符必须和建表语句中指定的一至:

load data local inpath '/home/hive/zx/spec_table_from_textfile.txt' into table spec_table_from_textfile

查询表

与常见的SQL查询语句类似:

select * from spec_table where table_name like 'xx%' and table_name like '00%' limit 1000

修改表

修改名

alter table name old_name to new_name

增加字段

alter table spec_table2 add columns (category string);
  • 在尾部增加

修改表结构

alter table spec_table2
replace columns (
  id bigint,
  table_code string,
  table_name string,
  filed_code string,
  filed_name string
  );

注:这个语句只是修改表结构并不会对数据进行操作,如果表中已经存在数据,你删除一个字段后再恢复会发现数据还在,同样如果你改变字段顺序,会发现数据错位了。

删除表

drop table xx

相关文章

  • 数据查询-Hive基础

    outline 什么是Hive 为什么需要Hive Hive的架构 Hive的常用操作 什么是Hive Hive由...

  • Hive sql常见操作

    基本sql操作 hive表操作 分区操作 Hive内置函数 (1)数学函数 常用的数学函数都有:round、flo...

  • Hive基本操作

    参考:hive 表的常用操作Hive基本操作 1 Thrift Server Thrift 支持多种语言之间的RP...

  • Hive学习-高级版一(其他客户端使用介绍)

    操作hive的方法前面只介绍了hive客户端方式,但是被官方定义为过时(虽然还是最常用的),其他操作hive的方式...

  • Hive常用操作

    1. 绑定数据 1.1 创建表&创建文本文件 表示行格式用逗号来分割字段。 创建文本文件test.txt 我创建在...

  • Hive常用操作

    创建表 简单的建表 从查询结果创建表 克隆表 此处是指克隆表结构,并不会克隆表数据。 创建从格式化文本文件导入的表...

  • hive进阶学习

    创建hive表常用语句: 修改hive表名称: 内部表 vs 外部表: 创建分区表以及补充操作: hive中查询介...

  • Hive中的msck和analyze table的作用

    0. Hive使用中遇到的问题 Hive是常用的数据仓库工具,功能强大,操作简便。在使用Hive的过程中,经常碰见...

  • Hive常用操作汇总

    Hive常用操作汇总 表操作 数据存储位置发生改变,分区名未改变 列操作 修改列 First将列放在第一列,AFT...

  • hive基础语法

    目录 Hive安装和启动 Hive表操作-分区表 Hive表操作-复杂类型操作 Hive 查询语句 Zepplin...

网友评论

      本文标题:Hive常用操作

      本文链接:https://www.haomeiwen.com/subject/sipypftx.html