相关知识
为了完成本关任务,你需要掌握:1. 如何导入本地数据到
hive
的表中,2. 如何创建索引,3. 如何删除索引。
导入本地数据到 hive 表中
/home/shoppings.txt
目录下数据格式如下:
- 在数据库
shopping
中根据数据分隔方式创建表items_info
:1.CREATE TABLE IF NOT EXISTS shopping.items_info(
2.id INT COMMENT 'item id',
3.name STRING COMMENT 'item name',
4.price FLOAT COMMENT 'item price',
5.category STRING COMMENT 'item category',
6.brand STRING COMMENT 'item brand',
7.stock INT COMMENT 'item stock',
8.address STRUCT<city:STRING, country:STRING, zip:INT> COMMENT 'item sales address')
9.COMMENT 'goods information table'
10.row format delimited fields terminated by ',' //字段以‘,’分隔
11.collection items terminated by '-' //集合以‘-’分隔
12.TBLPROPERTIES ('creator'='Xiaoming','date'='2019-01-01');
- 进入到数据库
shopping
中:use shopping;
- 导入数据到表
items_info
中:1.load data local inpath '/home/shoppings.txt'
2.overwrite into table items_info;
- 查看导入的数据:
select * from items_info;
Create 创建索引
创建索引的语法为:
CREATE INDEX index_name ON TABLE base_table_name (col_name,…)
AS index_type
[With DEFERRED REBUILD]
[INDXPROPERTIES (property_name=property_value,…)]
[IN TABLE index_table_name]
[[ROW FORMAT …] STORED AS … | STORED BY]
[LOCATION hdfs_path] [TBLPROPERTIES (…)] [COMMENT "index comment"];
属性参数说明:
With DEFERRED REBUILD
:用于构建一个空索引。
例子:
- 创建索引
items_index
:
create index items_index on table items_info(id)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
IN TABLE items_index_table;
Alter 修改索引
修改索引的语法为:
ALTER INDEX index_name ON table_name [PARTITION partition_spec] REBUILD;
ALTER INDEX REBUILD
用于重建之前创建索引时使用关键字
WITH DEFERREDREBUILD
建立的所有或之前建立的索引,若指定关键字
PARTITION
,则只针对相应分区建立索引。
DROP 删除索引
删除索引的语法为:
DROP INDEX [IF EXISTS] index_name ON table_name;
- 删除索引
items_index
:
drop index if exists items_index on items_info;
编程要求
student.txt
中的数据格式如下:
- 创建
test2
数据库; - 根据以上数据创建
student
表; - 将
/home/student.txt
中的数据导入到表student
中; - 根据学号
Sno
创建索引student_index
; - 删除索引
student_index
。
#********* Begin *********#
echo "
create database if not exists test2
LOCATION '/hive/test2'
WITH DBPROPERTIES('creator'='Floret','date'='2020-11-16');
CREATE TABLE IF NOT EXISTS test2.student(
Sno INT COMMENT 'student sno',
name STRING COMMENT 'student name',
age INT COMMENT 'student age',
sex STRING COMMENT 'student sex',
score STRUCT<Chinese:FLOAT,Math:FLOAT,English:FLOAT> COMMENT 'student score')
COMMENT 'students information table'
row format delimited fields terminated by ','
collection items terminated by '-'
TBLPROPERTIES ('creator'='Floret','date'='2020-11-16');
load data local inpath '/home/student.txt'
overwrite into table student;
create index student_index on table student(Sno)
as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild
IN TABLE student_index_table;
drop index if exists student_index on student;
"
#********* End *********#
版权归原作者 是草莓熊吖 所有, 如有侵权,请联系我们删除。