Hcatalog 简明教程
HCatalog - Show Partitions
分区是用于创建单独表格或视图的表格数据条件。SHOW PARTITIONS列出给定基础表的所有现有分区。分区按字母顺序列出。在Hive 0.6之后,还可以指定分区规范的部分以筛选结果列表。
A partition is a condition for tabular data which is used for creating a separate table or view. SHOW PARTITIONS lists all the existing partitions for a given base table. Partitions are listed in alphabetical order. After Hive 0.6, it is also possible to specify parts of a partition specification to filter the resulting list.
可以使用SHOW PARTITIONS命令查看特定表格中存在的分区。本章介绍如何列出HCatalog中特定表格的分区。
You can use the SHOW PARTITIONS command to see the partitions that exist in a particular table. This chapter describes how to list out the partitions of a particular table in HCatalog.
Show Partitions Statement
其语法如下:
The syntax is as follows −
SHOW PARTITIONS table_name;
以下查询删除名为 employee 的表格−
The following query drops a table named employee −
./hcat –e "Show partitions employee;"
成功执行查询后,你会看到以下响应−
On successful execution of the query, you get to see the following response −
OK
Designation = IT
Time taken: 5.3 seconds
Dynamic Partition
HCatalog将表格组织成分区。这是一种基于分区列,如日期、城市和部门的值,将表格划分为相关部分的方式。使用分区,很容易查询部分数据。
HCatalog organizes tables into partitions. It is a way of dividing a table into related parts based on the values of partitioned columns such as date, city, and department. Using partitions, it is easy to query a portion of the data.
例如,名为 Tab1 的表格包含员工数据,如id、姓名、部门和yoj(即参加年份)。假设你需要检索2012年加入的所有员工的详细信息。查询搜索整个表格以获取所需信息。然而,如果您使用年份对员工数据进行分区并将其存储在单独的文件中,则会减少查询处理时间。以下示例显示如何对文件及其数据进行分区 −
For example, a table named Tab1 contains employee data such as id, name, dept, and yoj (i.e., year of joining). Suppose you need to retrieve the details of all employees who joined in 2012. A query searches the whole table for the required information. However, if you partition the employee data with the year and store it in a separate file, it reduces the query processing time. The following example shows how to partition a file and its data −
以下文件包含 employeedata 表格。
The following file contains employeedata table.
Adding a Partition
我们可以通过更改表格来向表格添加分区。让我们假设我们有一个名为 employee 的表格,其中包含诸如Id、姓名、工资、职务、部门和yoj等字段。
We can add partitions to a table by altering the table. Let us assume we have a table called employee with fields such as Id, Name, Salary, Designation, Dept, and yoj.
Syntax
ALTER TABLE table_name ADD [IF NOT EXISTS] PARTITION partition_spec
[LOCATION 'location1'] partition_spec [LOCATION 'location2'] ...;
partition_spec:
: (p_column = p_col_value, p_column = p_col_value, ...)
以下查询用于向 employee 表格添加分区。
The following query is used to add a partition to the employee table.
./hcat –e "ALTER TABLE employee ADD PARTITION (year = '2013') location '/2012/part2012';"
Renaming a Partition
可以使用RENAME-TO命令重命名分区。它的语法如下−
You can use the RENAME-TO command to rename a partition. Its syntax is as follows −
./hact –e "ALTER TABLE table_name PARTITION partition_spec RENAME TO PARTITION partition_spec;"
以下查询用于重命名分区 −
The following query is used to rename a partition −
./hcat –e "ALTER TABLE employee PARTITION (year=’1203’) RENAME TO PARTITION (Yoj='1203');"
Dropping a Partition
用于删除分区的命令的语法如下−
The syntax of the command that is used to drop a partition is as follows −
./hcat –e "ALTER TABLE table_name DROP [IF EXISTS] PARTITION partition_spec,.
PARTITION partition_spec,...;"
以下查询用于删除分区−
The following query is used to drop a partition −
./hcat –e "ALTER TABLE employee DROP [IF EXISTS] PARTITION (year=’1203’);"