hive库基础命令

本文介绍Hive SQL的基本操作,包括数据库和表的创建、数据加载、表结构修改、数据查询等关键步骤,并展示了如何进行连接查询及使用部分函数。

创建数据库
hive> create database base1;

使用数据库
hive> use base1;

创建表
hive> create table table1(cols string,cols1 string);

创建一个表与当前hive存在的表结构一样
hive> create table table1 like table2;

创建分区表
hive> create table table1(cols string,cols2 string) partitioned by (cols3 string,cols4 string);

加载分区表数据
hive> load data local inpath ‘/home/hadoop/input/hive/partitions/file1’ into table table1 partition (cols=’1998-01-01’,cols1=’SZ’);

从本地文件加载数据
hive> load data local inpath ‘/d:/123/123.txt’ overwrite into table table1;

展示表中分区
hive> show partitions table1;

展示所有数据库
hive> show databases;

展示所有表
hive> show tables;

展示后缀为S的表
hive> show table’.*s’;

显示表结构
hive> describe table1;

重命名表名称
hive> alter table tables1 rename to table2;

添加新一列
hive> alter table table1 add columns (other string comment ‘其他’);

删除表
hive> drop table table1;

删除表且保持表结构表定义
hive> dfs -rmr /home/hive/base1/table1;

显示所有函数
hive> show functions;

查看函数用法
hive> describe function xx;

查看数组,map,结构
hive> select cols[0],cols1[‘a’],cols.b from complex;

内连接
hive> select sales.,things. from sales join things on (sales.id = things.id);

外连接
hive> selectsales.*,things.*from sales left outer join things on(sales.id = things.id);
hive> select sales., things. fromsales right outer join things on(sales.id = things.id);
hive> select sales., things. Fromsales full outer join things on(sales.id = things.id);

in查询
hive> select * from thingsleft semi join sales on (sales.id = things.id);

map连接(hive可以把较小的表放入每个mapper的内存来执行连接)

insertoverwrite table ..select:新表预先存在
hive> from records2

insert overwritetable stations_by_year select year,count(distinct station) group by year
insert overwrite table records_by_year select year,count(1) groupby year
insert overwrite table good_records_by_year selectyear,count(1) where temperature != 9999 and (quality = 0 or quality = 1 orquality =4 or quality = 5 or quality = 9) group by year;

create table … as select:新表表预先不存在
hive>create table target as select cols1,cols2 from source;

创建视图
hive> create view valid_records as select *from records2 where temperature!=9999;

查看视图详细信息
hive> describe extended valid_records;

查看hive为某个查询使用多少个mapreduce作业
hive> explain select sales.*, things.*from sales join things on (sales.id =things.id);

Hive一个基于Hadoop的数据工具,用于进行大规模数据分析和查询。下面是Hive的一些基本操作命令: 1. 使用命令`show databases;`可以查看当前所有的数据库。 2. 使用命令`CREATE DATABASE park;`可以创建一个名为park的数据库。实际上,创建数据库相当于在Hadoop的HDFS文件系统中创建一个目录节点,统一存在`/usr/hive/warehouse`目录下。 3. 使用命令`USE park;`可以进入park数据库。 4. 使用命令`show tables;`可以查看当前数据库下的所有表。 5. 使用命令`CREATE TABLE stu (id INT, name STRING);`可以创建一个名为stu的表,其中包含id和name两个字段。在Hive中,使用的是STRING类型来表示字符,而不是CHAR或VARCHAR类型。所创建的表实际上也是HDFS中的一个目录节点。默认情况下,所有在default数据库创建的表都直接存在`/usr/hive/warehouse`目录下。 6. 使用命令`INSERT INTO TABLE stu VALUES (1, 'John');`可以向stu表中插入数据。HDFS不支持数据的修改和删除,但在Hive 2.0版本后开始支持数据的追加,可以使用`INSERT INTO`语句执行追加操作。Hive支持查询和行级别的插入,但不支持行级别的删除和修改。实际上,Hive的操作是通过执行MapReduce任务来完成的。插入数据后,我们可以在HDFS的stu目录下发现多了一个文件,其中存储了插入的数据。因此,可以得出结论:Hive存储的数据是通过HDFS的文件来存储的。 7. 使用命令`SELECT id FROM stu;`可以查询stu表中的数据,并只返回id字段的值。 需要注意的是,如果想在HDFS目录下自己创建一个分区,并在该分区下上传文件,需要执行一些额外的操作。首先,手动创建的分区在Hive中是无法使用的,因为原数据库中没有记录该分区的信息。要让自己创建的分区被识别,需要执行命令`ALTER TABLE book ADD PARTITION (category = 'jp') LOCATION '/user/hive/warehouse/park.db/book/category=jp';`。这条命令的作用是在原数据表book中创建对应的分区信息。另外,还可以使用命令`ALTER TABLE book****** 'nn');`来修改分区。 希望以上信息能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值