HIVE 数据定义 DDL

来源:互联网 发布:ubuntu怎么安装lrzsz 编辑:程序博客网 时间:2024/05/10 16:30

coming form:http://blog.csdn.net/iquicksandi/article/details/8522691


Databases in Hive -- 在HIVE 中使用数据库


Hive offers no support for row-level  inserts, updates, and deletes.

Hive doesn’t support transactions. Hive adds ex-tensions to provide
better performance in the context of Hadoop and to integrate with

custom extensions and even external programs.


Hive 不支持行级插入,更新,删除。也不支持事务


创建数据库

[sql] view plaincopyprint?
  1. hive> CREATE DATABASE financials;  


仓库数据库判断数据库是否存在

[sql] view plaincopyprint?
  1. hive> CREATE DATABASE IF NOT EXISTS financials;  


显示现在有的数据库

[sql] view plaincopyprint?
  1. hive> SHOW DATABASES;  
  2. default  
  3. financials  
  4. hive> CREATE DATABASE human_resources;  
  5. hive> SHOW DATABASES;  
  6. default  
  7. financials  
  8. human_resources  


条件查询数据库

[sql] view plaincopyprint?
  1. hive> SHOW DATABASES LIKE 'h.*';  
  2. human_resources  
  3. hive> ...  

创建指定存放文件位置 数据库

[sql] view plaincopyprint?
  1. hive> CREATE DATABASE financials  
  2.     > LOCATION '/my/preferred/directory';  


创建数据库时 添加注释信息

[sql] view plaincopyprint?
  1. hive> CREATE DATABASE financials  
  2.     > COMMENT 'Holds all financial tables';  
  3. hive> DESCRIBE DATABASE financials;  
  4. financials   Holds all financial tables  
  5.   hdfs://master-server/user/hive/warehouse/financials.db  


创建数据库 添加扩展信息

[sql] view plaincopyprint?
  1. hive> CREATE DATABASE financials  
  2.     > WITH DBPROPERTIES ('creator' = 'Mark Moneybags''date' = '2012-01-02');  
  3. hive> DESCRIBE DATABASE financials;  
  4. financials   hdfs://master-server/user/hive/warehouse/financials.db  
  5. hive> DESCRIBE DATABASE EXTENDED financials;  
  6. financials   hdfs://master-server/user/hive/warehouse/financials.db  
  7.  {date=2012-01-02, creator=Mark Moneybags);  


使用数据库

[sql] view plaincopyprint?
  1. hive> USE financials;  


设置显示当前数据库

[sql] view plaincopyprint?
  1. hive> set hive.cli.print.current.db=true;  
  2. hive (financials)> USE default;  
  3. hive (default)> set hive.cli.print.current.db=false;  
  4. hive> ...  


删除数据库

[sql] view plaincopyprint?
  1. hive> DROP DATABASE IF EXISTS financials;  


当数据库存在表时,先要删除表 再能删除数据库

[sql] view plaincopyprint?
  1. hive> DROP DATABASE IF EXISTS financials CASCADE;  


Alter Database -- 修改数据库


[sql] view plaincopyprint?
  1. hive> ALTER DATABASE financials SET DBPROPERTIES ('edited-by' = 'Joe Dba');  

There is no way to delete or “unset” a DBPROPERTY  没有方法删除或重置 DBPROPERTY 



Creating Tables -- 创建表

[sql] view plaincopyprint?
  1. CREATE TABLE IF NOT EXISTS mydb.employees (  
  2.   name         STRING COMMENT 'Employee name',  
  3.   salary       FLOAT  COMMENT 'Employee salary',  
  4.   subordinates ARRAY<STRING> COMMENT 'Names of subordinates',  
  5.   deductions   MAP<STRING, FLOAT>  
  6.                COMMENT 'Keys are deductions names, values are percentages  
  7.   address      STRUCT<street:STRING, city:STRING, state:STRING, zip:INT>  
  8.                COMMENT 'Home address')  
  9. COMMENT 'Description of the table'  
  10. TBLPROPERTIES ('creator'='me''created_at'='2012-01-02 10:00:00', ...)  
  11. LOCATION '/user/hive/warehouse/mydb.db/employees';  

创建表-复制表结构

[sql] view plaincopyprint?
  1. CREATE TABLE IF NOT EXISTS mydb.employees2  
  2. LIKE mydb.employees;  


显示某个数据库中的表

[sql] view plaincopyprint?
  1. hive> USE mydb;  
  2. hive> SHOW TABLES;  
  3. employees  
  4. table1  
  5. table2  

[sql] view plaincopyprint?
  1. hive> USE default;  
  2. hive> SHOW TABLES IN mydb;  
  3. employees  

显示指定筛选条件 表名

[sql] view plaincopyprint?
  1. hive> USE mydb;  
  2. hive> SHOW TABLES 'empl.*';  
  3. employees  

显示表扩展信息

[sql] view plaincopyprint?
  1. hive> DESCRIBE EXTENDED mydb.employees;  
  2. name    string  Employee name  
  3. salary  float   Employee salary  
  4. subordinates    array<string>   Names of subordinates  
  5. deductions      map<string,float> Keys are deductions names, values are percentages  
  6. address struct<street:string,city:string,state:string,zip:int>  Home address  
  7. Detailed Table Information      Table(tableName:employees, dbName:mydb, owner:me,  
  8. ...  
  9. location:hdfs://master-server/user/hive/warehouse/mydb.db/employees,  
  10. parameters:{creator=me, created_at='2012-01-02 10:00:00',  
  11.             last_modified_user=me, last_modified_time=1337544510,  
  12.             comment:Description of the table, ...}, ...)  


指定显示某个字段的信息

[sql] view plaincopyprint?
  1. hive> DESCRIBE mydb.employees.salary;  
  2. salary  float   Employee salary  


External Tables -- 外部表


外部表,删除表不删除数据

[sql] view plaincopyprint?
  1. CREATE EXTERNAL TABLE IF NOT EXISTS stocks (  
  2.   exchange        STRING,  
  3.   symbol          STRING,  
  4.   ymd             STRING,  
  5.   price_open      FLOAT,  
  6.   price_high      FLOAT,  
  7.   price_low       FLOAT,  
  8.   price_close     FLOAT,  
  9.   volume          INT,  
  10.   price_adj_close FLOAT)  
  11. ROW FORMAT DELIMITED FIELDS TERMINATED BY ','  
  12. LOCATION '/data/stocks';  

复制表结构仓库外部表

[sql] view plaincopyprint?
  1. CREATE EXTERNAL TABLE IF NOT EXISTS mydb.employees3  
  2. LIKE mydb.employees  
  3. LOCATION '/path/to/data';  


Partitioned, Managed Tables --分区表

[sql] view plaincopyprint?
  1. CREATE TABLE employees (  
  2.   name         STRING,  
  3.   salary       FLOAT,  
  4.   subordinates ARRAY<STRING>,  
  5.   deductions   MAP<STRING, FLOAT>,  
  6.   address      STRUCT<street:STRING, city:STRING, state:STRING, zip:INT>  
  7. )  
  8. PARTITIONED BY (country STRING, state STRING);  


However, Hive will now create subdirectories reflecting the partitioning structure. For
example:


[sql] view plaincopyprint?
  1. ...  
  2. .../employees/country=CA/state=AB  
  3. .../employees/country=CA/state=BC  
  4. ...  
  5. .../employees/country=US/state=AL  
  6. .../employees/country=US/state=AK  
  7. ...  

建议安全措施
把HIVE 设置成“严格”模式,禁止分区表的查询没有
一个WHERE子句

[sql] view plaincopyprint?
  1. hive> set hive.mapred.mode=strict;  
  2. hive> SELECT e.name, e.salary FROM employees e LIMIT 100;  
  3. FAILED: Error in semantic analysis: No partition predicate found for  
  4.  Alias "e" Table "employees"  
  5. hive> set hive.mapred.mode=nonstrict;  
  6. hive> SELECT e.name, e.salary FROM employees e LIMIT 100;  

查看现有分区


[sql] view plaincopyprint?
  1. hive> SHOW PARTITIONS employees;  
  2. ...  
  3. Country=CA/state=AB  
  4. country=CA/state=BC  
  5. ...  
  6. country=US/state=AL  
  7. country=US/state=AK  

查看分区详细 分区键


[sql] view plaincopyprint?
  1. hive> SHOW PARTITIONS employees PARTITION(country='US');  
  2. country=US/state=AL  
  3. country=US/state=AK  
  4. ...  
  5. hive> SHOW PARTITIONS employees PARTITION(country='US', state='AK');  
  6. country=US/state=AK  


通过 DESC 显示分区键


[sql] view plaincopyprint?
  1. hive> DESCRIBE EXTENDED employees;  
  2. name         string,  
  3. salary       float,  
  4. ...  
  5. address      struct<...>,  
  6. country      string,  
  7. state        string  
  8. Detailed Table Information...  
  9. partitionKeys:[FieldSchema(name:country, type:string, comment:null),  
  10. FieldSchema(name:state, type:string, comment:null)],  
  11. ...  


从文件读入 分区表

[sql] view plaincopyprint?
  1. LOAD DATA LOCAL INPATH '${env:HOME}/california-employees'  
  2. INTO TABLE employees  
  3. PARTITION (country = 'US', state = 'CA');  


External Partitioned Tables 外部分区表


1.先创建外部表结构

[sql] view plaincopyprint?
  1. CREATE EXTERNAL TABLE IF NOT EXISTS log_messages (  
  2.   hms             INT,  
  3.   severity        STRING,  
  4.   server          STRING,  
  5.   process_id      INT,  
  6.   message         STRING)  
  7. PARTITIONED BY (year INTmonth INTday INT)  
  8. ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';  

2.为外部表增加指定分区

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages ADD PARTITION(year = 2012, month = 1, day = 2)  
  2. LOCATION 'hdfs://master_server/data/log_messages/2012/01/02';  

3.把数据表复制外部表目录结构中

Copy the data for the partition being moved to S3. For example, you can use the
hadoop distcp command:

[sql] view plaincopyprint?
  1. hadoop distcp /data/log_messages/2011/12/02 s3n://ourbucket/logs/2011/12/02  

•Alter the table to point the partition to the S3 location:
[sql] view plaincopyprint?
  1. ALTER TABLE log_messages PARTITION(year = 2011, month = 12, day = 2)  

SET LOCATION 's3n://ourbucket/logs/2011/01/02';
•Remove the HDFS copy of the partition using the hadoop fs -rmr command:
[sql] view plaincopyprint?
  1. hadoop fs -rmr /data/log_messages/2011/01/02  


显示 表分区信息

[sql] view plaincopyprint?
  1. hive> SHOW PARTITIONS log_messages;  
  2. ...  
  3. year=2011/month=12/day=31  
  4. year=2012/month=1/day=1  
  5. year=2012/month=1/day=2  

[sql] view plaincopyprint?
  1. hive> DESCRIBE EXTENDED log_messages;  
  2. ...  
  3. message         string,  
  4. year            int,  
  5. month           int,  
  6. day             int  
  7. Detailed Table Information...  
  8. partitionKeys:[FieldSchema(name:year, type:int, comment:null),  
  9. FieldSchema(name:month, type:int, comment:null),  
  10. FieldSchema(name:day, type:int, comment:null)],  
  11. ...  

[sql] view plaincopyprint?
  1. hive> DESCRIBE EXTENDED log_messages PARTITION (year=2012, month=1, day=2);  
  2. ...  
  3. location:s3n://ourbucket/logs/2011/01/02,  
  4. ...  


Customizing Table Storage Formats -- 表存储格式


[sql] view plaincopyprint?
  1. CREATE TABLE employees (  
  2.   name         STRING,  
  3.   salary       FLOAT,  
  4.   subordinates ARRAY<STRING>,  
  5.   deductions   MAP<STRING, FLOAT>,  
  6.   address      STRUCT<street:STRING, city:STRING, state:STRING, zip:INT>  
  7. )  
  8. ROW FORMAT DELIMITED  
  9. FIELDS TERMINATED BY '\001'  
  10. COLLECTION ITEMS TERMINATED BY '\002'  
  11. MAP KEYS TERMINATED BY '\003'  
  12. LINES TERMINATED BY '\n'  
  13. STORED AS TEXTFILE;  


Dropping Tables -- 删除表


[sql] view plaincopyprint?
  1. DROP TABLE IF EXISTS employees;  

For external tables, the metadata is deleted but the data is not.


Alter Table --修改表结构


ALTER TABLE modifies  table metadata  only. The  data  for  the  table  is
untouched. It’s up to you to ensure that any modifications are consistent
with the actual data.


Renaming a Table -- 修改表名

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages RENAME TO logmsgs;  

Adding, Modifying, and Dropping a Table Partition -- 增加,修改,删除 表分区


[sql] view plaincopyprint?
  1. ALTER TABLE log_messages ADD IF NOT EXISTS  
  2. PARTITION (year = 2011, month = 1, day = 1) LOCATION '/logs/2011/01/01'  
  3. PARTITION (year = 2011, month = 1, day = 2) LOCATION '/logs/2011/01/02'  
  4. PARTITION (year = 2011, month = 1, day = 3) LOCATION '/logs/2011/01/03'  

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages PARTITION(year = 2011, month = 12, day = 2)  
  2. SET LOCATION 's3n://ourbucket/logs/2011/01/02';  

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages DROP IF EXISTS PARTITION(year = 2011, month = 12, day = 2);  


Changing Columns --修改列


[sql] view plaincopyprint?
  1. ALTER TABLE log_messages  
  2. CHANGE COLUMN hms hours_minutes_seconds INT  
  3. COMMENT 'The hours, minutes, and seconds part of the timestamp'  
  4. AFTER severity;  

Adding Columns --增加列

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages ADD COLUMNS (  
  2.  app_name   STRING COMMENT 'Application name',  
  3.  session_id LONG   COMMENT 'The current session id');  

Deleting or Replacing Columns --删除 替换列


[sql] view plaincopyprint?
  1. ALTER TABLE log_messages REPLACE COLUMNS (  
  2.  hours_mins_secs INT    COMMENT 'hour, minute, seconds from timestamp',  
  3.  severity        STRING COMMENT 'The message severity'  
  4.  message         STRING COMMENT 'The rest of the message');  

This statement effectively renames the original hms column and removes the server and
process_id columns from the original schema definition. As for all ALTER statements,
only the table metadata is changed.


Alter Table Properties --修改表属性

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages SET TBLPROPERTIES (  
  2.  'notes' = 'The process id is no longer captured; this column is always NULL');  

Alter Storage Properties --修改存储属性

[sql] view plaincopyprint?
  1. ALTER TABLE log_messages  
  2. PARTITION(year = 2012, month = 1, day = 1)  
  3. SET FILEFORMAT SEQUENCEFILE;  


You can specify a new SerDe along with SerDe properties or change the properties for
the existing SerDe. The following example specifies that a table will use a Java class
named com.example.JSONSerDe to process a file of JSON-encoded records

[sql] view plaincopyprint?
  1. ALTER TABLE table_using_JSON_storage  
  2. SET SERDE 'com.example.JSONSerDe'  
  3. WITH SERDEPROPERTIES (  
  4.  'prop1' = 'value1',  
  5.  'prop2' = 'value2');  

0 0
原创粉丝点击