hive常用命令

hive>create database if not exists db_hive;
hive>desc database extended db_hive;
如果数据库不为空,可以采用cascade命令,强制删除
hive> drop database db_hive cascade;

创建分区表和相关操作:

建表语法:
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name 
[(col_name data_type [COMMENT col_comment], ...)] 
[COMMENT table_comment] 
[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] 
[CLUSTERED BY (col_name, col_name, ...) 
[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] 
[ROW FORMAT row_format] 
[STORED AS file_format] 
[LOCATION hdfs_path]
实例:
create external table if not exists student(
id int,
name string,
classNo string)
partitioned by (entry_year string)
row format delimited fields terminated by '\t'
stored as textfile
加载本地数据:不会删除源数据
hive> load data local inpath '/root/testdata/student.txt' into table student partition(entry_year=2018);
加载hdfs中的数据:一定注意这种方式会把原有数据剪切到对应的hdfs目录,如果和别人的表共享一个数据源,那么就不能这么操作,而要通过alter table student add partition(entry_year='sss') location '/testdata'的方式
hive> load data inpath '/testdata/student' into table student partition(entry_year=2019);
也可以通过上传数据到对应的hdfs文件目录,再动态添加分区:
[root@mini01 testdata]# hadoop fs -mkdir -p /user/hive/warehouse/db_hive.db/student/entry_year=2020 
[root@mini01 testdata]# hadoop fs -put /root/testdata/student.txt /user/hive/warehouse/db_hive.db/student/entry_year=2020
hive> alter table student add partition(entry_year='2020');
如果元数据在hdfs上的某个目录(不是在hive元数据目录),用下面语句,注意location后面是目录而不是文件,原有数据不会更改,但是如果原有数据丢失,那么对应表中数据也就丢失了,如果。
hive> alter table student drop if exists partition(entry_year='2021');
hive> alter table student add partition(entry_year='2021') location '/testdata';
删除分区:
 alter table dept_partition drop partition (month='201705'), partition (month='201706');

数据导入方式:

语法:
hive>load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
也可以通过读其他表到目标表
hive> insert overwrite table student partition(entry_year='2022') select id,name,classno  from student where entry_year='2021'

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ORA-00001: 违反唯一约束条件 (.) 错误说明:当在唯一索引所对应的列上键入重复值时,会触发此异常。 O...
    我想起个好名字阅读 5,475评论 0 9
  • 好记性不如烂笔头,何况记性不好本文都来自hive文档, 记下来方便查询。 Database操作 create da...
    aaron1993阅读 2,048评论 0 1
  • 一、创建表 创建新表hive> CREATE TABLE t_hive (a int, b int, c int)...
    BestFei阅读 3,413评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,069评论 2 89