Creating External Table - ignacio-alorre/Hive GitHub Wiki
Note: LOCATION is mandatory for creating external tables
CREATE EXTERNAL TABLE [IF NOT EXISTS] [db_name.] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[COMMENT table_comment]
[ROW FORMAT row_format]
[FIELDS TERMINATED BY char]
[STORED AS file_format]
[LOCATION hdfs_path];
Example
CREATE EXTERNAL TABLE IF NOT EXISTS test_ext
(ID int,
DEPT int,
NAME string
)
COMMENT 'This is an example table'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION '/data/test';
Complete template here...
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name -- (Note: TEMPORARY available in Hive 0.14.0 and later)
[(col_name data_type [column_constraint_specification] [COMMENT col_comment], ... [constraint_specification])]
[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]
[SKEWED BY (col_name, col_name, ...) -- (Note: Available in Hive 0.10.0 and later)]
ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
[STORED AS DIRECTORIES]
[
[ROW FORMAT row_format]
[STORED AS file_format]
| STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] -- (Note: Available in Hive 0.6.0 and later)
]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)] -- (Note: Available in Hive 0.6.0 and later)
[AS select_statement]; -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
LIKE existing_table_or_view_name
[LOCATION hdfs_path];
This clause allows you to tag the table definition with your own metadata key/value pairs. Some predefined table properties also exist, such as last_modified_user and last_modified_time which are automatically added and managed by Hive. Other predefined table properties include:
- TBLPROPERTIES ("comment"="table_comment")
- TBLPROPERTIES ("hbase.table.name"="table_name") – HBase Integration
- TBLPROPERTIES ("immutable"="true") or ("immutable"="false") – Inserting Data into Hive Tables from Queries
- TBLPROPERTIES ("orc.compress"="ZLIB") or ("orc.compress"="SNAPPY") or ("orc.compress"="NONE") and other ORC properties – ORC Files
- TBLPROPERTIES ("transactional"="true") or ("transactional"="false") the default is "false" – Hive Transactions
- TBLPROPERTIES ("NO_AUTO_COMPACTION"="true") or ("NO_AUTO_COMPACTION"="false"), the default is "false" – Hive Transactions
- TBLPROPERTIES ("compactor.mapreduce.map.memory.mb"="mapper_memory") – see Hive Transactions.
- TBLPROPERTIES ("compactorthreshold.hive.compactor.delta.num.threshold"="threshold_num") – Hive Transactions
- TBLPROPERTIES ("compactorthreshold.hive.compactor.delta.pct.threshold"="threshold_pct") – Hive Transactions
- TBLPROPERTIES ("auto.purge"="true") or ("auto.purge"="false") – Insert Overwrite
- TBLPROPERTIES ("EXTERNAL"="TRUE") – Change a managed table to an external table and vice versa for "FALSE".
- TBLPROPERTIES ("external.table.purge"="true") when set on external table would delete the data as well.
-
STORED AS TEXTFILE:
- Stored as plain text files.
- TEXTFILE is the default file format, unless the configuration parameter hive.default.fileformat has a different setting.
- Use the DELIMITED clause to read delimited files.
- Enable escaping for the delimiter characters by using the 'ESCAPED BY' clause (such as ESCAPED BY '')
- Escaping is needed if you want to work with data that can contain these delimiter characters.
- A custom NULL format can also be specified using the 'NULL DEFINED AS' clause (default is '\N').
-
STORED AS SEQUENCEFILE
- Stored as compressed Sequence File.
-
STORED AS ORC
- Stored as ORC file format.
- Supports ACID Transactions & Cost-based Optimizer (CBO).
- Stores column-level metadata.
-
STORED AS PARQUET
- Stored as Parquet format for the Parquet columnar storage format in Hive 0.13.0 and later;
- Use ROW FORMAT SERDE ... STORED AS INPUTFORMAT ... OUTPUTFORMAT syntax ... in Hive 0.10, 0.11, or 0.12.
-
STORED AS AVRO
- Stored as Avro format. Avro SerDe
-
STORED AS RCFILE
- Stored as Record Columnar File format.
-
STORED AS JSONFILE
- Stored as Json file format
-
STORED BY
- Stored by a non-native table format. To create or link to a non-native table, for example a table backed by HBase or Druid or Accumulo.
- More information on this option in StorageHandlers.
-
INPUTFORMAT and OUTPUTFORMAT
- In the file_format to specify the name of a corresponding InputFormat and OutputFormat class as a string literal.
- For example, 'org.apache.hadoop.hive.contrib.fileformat.base64.Base64TextInputFormat'.
- For LZO compression, the values to use are 'INPUTFORMAT "com.hadoop.mapred.DeprecatedLzoTextInputFormat" OUTPUTFORMAT "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat"'
Tables can also be created and populated by the results of a query in one create-table-as-select (CTAS) statement. The table created by CTAS is atomic, meaning that the table is not seen by other users until all the query results are populated. So other users will either see the table with the complete results of the query or will not see the table at all.
There are two parts in CTAS, the SELECT part can be any SELECT statement supported by HiveQL. The CREATE part of the CTAS takes the resulting schema from the SELECT part and creates the target table with other table properties such as the SerDe and storage format.
CTAS has these restrictions:
- The target table cannot be an external table.
- The target table cannot be a list bucketing table.
CREATE TABLE new_key_value_store
ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe"
STORED AS RCFile
AS
SELECT (key % 1024) new_key, concat(key, value) key_value_pair
FROM key_value_store
SORT BY new_key, key_value_pair;
The above CTAS statement creates the target table new_key_value_store
with the schema (new_key DOUBLE, key_value_pair STRING
) derived from the results of the SELECT statement. If the SELECT statement does not specify column aliases, the column names will be automatically assigned to _col0, _col1, and _col2 etc. In addition, the new target table is created using a specific SerDe and a storage format independent of the source tables in the SELECT statement.
The LIKE form of CREATE TABLE allows you to copy an existing table definition exactly (without copying its data). In contrast to CTAS, the statement below creates a new empty_key_value_store table whose definition exactly matches the existing key_value_store in all particulars other than table name. The new table contains no rows.
CREATE TABLE empty_key_value_store
LIKE key_value_store [TBLPROPERTIES (property_name=property_value, ...)];
CREATE TABLE LIKE view_name creates a table by adopting the schema of view_name (fields and partition columns) using defaults for SerDe and file formats.
CREATE TABLE page_view(viewTime INT, userid BIGINT,
page_url STRING, referrer_url STRING,
ip STRING COMMENT 'IP Address of the User')
COMMENT 'This is the page view table'
PARTITIONED BY(dt STRING, country STRING)
CLUSTERED BY(userid) SORTED BY(viewTime) INTO 32 BUCKETS
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\001'
COLLECTION ITEMS TERMINATED BY '\002'
MAP KEYS TERMINATED BY '\003'
STORED AS SEQUENCEFILE;
In the example above, the page_view
table is bucketed (clustered by) userid
and within each bucket the data is sorted in increasing order of viewTime
.
Such an organization allows the user to do efficient sampling on the clustered column - in this case userid. The sorting property allows internal operators to take advantage of the better-known data structure while evaluating queries, also increasing efficiency. MAP KEYS and COLLECTION ITEMS keywords can be used if any of the columns are lists or maps.
The CLUSTERED BY and SORTED BY creation commands do not affect how data is inserted into a table – only how it is read. This means that users must be careful to insert data correctly by specifying the number of reducers to be equal to the number of buckets, and using CLUSTER BY and SORT BY commands in their query.
This feature can be used to improve performance for tables where one or more columns have skewed values. By specifying the values that appear very often (heavy skew) Hive will split those out into separate files (or directories in case of list bucketing) automatically and take this fact into account during queries so that it can skip or include the whole file (or directory in case of list bucketing) if possible.
This can be specified on a per-table level during table creation.
The following example shows one column with three skewed values, optionally with the STORED AS DIRECTORIES clause which specifies list bucketing.
CREATE TABLE list_bucket_single (key STRING, value STRING)
SKEWED BY (key) ON (1,5,6) [STORED AS DIRECTORIES];
And here is an example of a table with two skewed columns.
CREATE TABLE list_bucket_multiple (col1 STRING, col2 int, col3 STRING)
SKEWED BY (col1, col2) ON (('s1',1), ('s3',3), ('s13',13), ('s78',78)) [STORED AS DIRECTORIES];
Alter table to add or remove Skewed options
A table that has been created as a temporary table will only be visible to the current session. Data will be stored in the user's scratch directory, and deleted at the end of the session.
If a temporary table is created with a database/table name of a permanent table which already exists in the database, then within that session any references to that table will resolve to the temporary table, rather than to the permanent table. The user will not be able to access the original table within that session without either dropping the temporary table, or renaming it to a non-conflicting name.
Temporary tables have the following limitations:
- Partition columns are not supported.
- No support for creation of indexes.
CREATE TEMPORARY TABLE list_bucket_multiple (col1 STRING, col2 int, col3 STRING);
A table that supports operations with ACID semantics. More details about transactional tables here.
CREATE TRANSACTIONAL TABLE transactional_table_test(key string, value string) PARTITIONED BY(ds string) STORED AS ORC;
Hive includes support for non-validated primary and foreign key constraints. Some SQL tools generate more efficient queries when constraints are present. Since these constraints are not validated, an upstream system needs to ensure data integrity before it is loaded into Hive.
create table pk(id1 integer, id2 integer,
primary key(id1, id2) disable novalidate);
create table fk(id1 integer, id2 integer,
constraint c1 foreign key(id1, id2) references pk(id2, id1) disable novalidate);
Hive includes support for UNIQUE, NOT NULL, DEFAULT and CHECK constraints. Beside UNIQUE all three type of constraints are enforced.
create table constraints1(id1 integer UNIQUE disable novalidate, id2 integer NOT NULL,
usr string DEFAULT current_user(), price double CHECK (price > 0 AND price <= 1000));
create table constraints2(id1 integer, id2 integer,
constraint c1_unique UNIQUE(id1) disable novalidate);
create table constraints3(id1 integer, id2 integer,
constraint c1_check CHECK(id1 + id2 > 0));
Sources