Hive SQL 操作示例

1、Create/Drop/Alter Database
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  [COMMENT database_comment]
  [LOCATION hdfs_path]
  [WITH DBPROPERTIES (property_name=property_value, ...)];
ABSTRACT SYNTAX TREE:
  (TOK_CREATEDATABASE db_name)


DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
ABSTRACT SYNTAX TREE:
  (TOK_DROPDATABASE db_name TOK_IFEXISTS)




ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);   -- (Note: SCHEMA added in Hive 0.14.0)
ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role;
hive0.14版本以后才支持


USE database_name
ABSTRACT SYNTAX TREE:
  (TOK_SWITCHDATABASE huanggang)


2、Create/Drop/Truncate Table
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 [COMMENT col_comment], ...)]
  [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, ...) ON ([(col_value, col_value, ...), ...|col_value, col_value, ...])
     [STORED AS DIRECTORIES]    -- (Note: Available in Hive 0.10.0 and later)]
  [
   [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];
 
data_type
  : primitive_type
  | array_type
  | map_type
  | struct_type
  | union_type  -- (Note: Available in Hive 0.7.0 and later)
 
primitive_type
  : TINYINT
  | SMALLINT
  | INT
  | BIGINT
  | BOOLEAN
  | FLOAT
  | DOUBLE
  | STRING
  | BINARY      -- (Note: Available in Hive 0.8.0 and later)
  | TIMESTAMP   -- (Note: Available in Hive 0.8.0 and later)
  | DECIMAL     -- (Note: Available in Hive 0.11.0 and later)
  | DECIMAL(precision, scale)  -- (Note: Available in Hive 0.13.0 and later)
  | VARCHAR     -- (Note: Available in Hive 0.12.0 and later)
  | CHAR        -- (Note: Available in Hive 0.13.0 and later)
 
array_type
  : ARRAY < data_type >
 
map_type
  : MAP < primitive_type, data_type >
 
struct_type
  : STRUCT < col_name : data_type [COMMENT col_comment], ...>
 
union_type
   : UNIONTYPE < data_type, data_type, ... >  -- (Note: Available in Hive 0.7.0 and later)
 
row_format
  : DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
        [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
        [NULL DEFINED AS char]   -- (Note: Available in Hive 0.13 and later)
  | SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)]
 
file_format:
  : SEQUENCEFILE
  | TEXTFILE    -- (Default, depending on hive.default.fileformat configuration)
  | RCFILE      -- (Note: Available in Hive 0.6.0 and later)
  | ORC         -- (Note: Available in Hive 0.11.0 and later)
  | AVRO        -- (Note: Available in Hive 0.14.0 and later)
  | INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname
ABSTRACT SYNTAX TREE:
  (TOK_CREATETABLE (TOK_TABNAME page_view) TOK_LIKETABLE (TOK_TABCOLLIST (TOK_TABCOL viewTime TOK_INT) (TOK_TABCOL userid TOK_BIGINT) (TOK_TABCOL page_url TOK_STRING) (TOK_TABCOL referrer_url TOK_STRING) (TOK_TABCOL ip TOK_STRING 'IP Address of the User')) (TOK_TABLECOMMENT 'This is the page view table') (TOK_TABLEPARTCOLS (TOK_TABCOLLIST (TOK_TABCOL dt TOK_STRING) (TOK_TABCOL country TOK_STRING))) (TOK_TABLEROWFORMAT (TOK_SERDEPROPS (TOK_TABLEROWFORMATFIELD '\001'))) TOK_TBLSEQUENCEFILE)


DROP TABLE [IF EXISTS] table_name;
ABSTRACT SYNTAX TREE:
  (TOK_DROPTABLE (TOK_TABNAME huanggang tbl) TOK_IFEXISTS)


TRUNCATE TABLE table_name [PARTITION partition_spec];
ABSTRACT SYNTAX TREE:
  (TOK_TRUNCATETABLE (TOK_TABLE_PARTITION page_view (TOK_PARTSPEC (TOK_PARTVAL dt 'dd') (TOK_PARTVAL country "dd"))))


ALTER TABLE table_name RENAME TO new_table_name;
ABSTRACT SYNTAX TREE:
  (TOK_ALTERTABLE_RENAME pokes new_name)


ALTER TABLE table_name SET TBLPROPERTIES table_properties;
ABSTRACT SYNTAX TREE:
  (TOK_ALTERTABLE_PROPERTIES pokes (TOK_TABLEPROPERTIES (TOK_TABLEPROPLIST (TOK_TABLEPROPERTY 'comment' 'fd'))))


3、Create/Drop/Alter View
暂时不用




4、Create/Drop/Alter Index
暂时不用


5、Create/Drop/Grant/Revoke Roles and Privileges
基于unix用户体系的权限, 用处不大


6、Loading files into tables
LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename [PARTITION (partcol1=val1, partcol2=val2 ...)]
ABSTRACT SYNTAX TREE:
  (TOK_LOAD "file:///root/29269.0002.txt" (TOK_TAB (TOK_TABNAME pokes)) local overwrite)


7、Inserting data into Hive Tables from queries
FROM from_statement
INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol2=val2 ...) [IF NOT EXISTS]] select_statement1
ABSTRACT SYNTAX TREE:
  (TOK_QUERY (TOK_FROM (TOK_TABREF (TOK_TABNAME pokes) p)) (TOK_INSERT (TOK_DESTINATION (TOK_TAB (TOK_TABNAME pokes1))) (TOK_SELECT (TOK_SELEXPR (. (TOK_TABLE_OR_COL p) foo)) (TOK_SELEXPR (. (TOK_TABLE_OR_COL p) bar)))))




8、Update/Delete
目前hive0.13版本以上支持


9、Import/Export
EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
  TO 'export_target_path'
ABSTRACT SYNTAX TREE:
  (TOK_EXPORT (TOK_TAB (TOK_TABNAME pokes)) "/fdsfd/")


10、Select
SELECT [ALL | DISTINCT] select_expr, select_expr, ...
FROM table_reference
[WHERE where_condition]
[GROUP BY col_list]
[CLUSTER BY col_list
  | [DISTRIBUTE BY col_list] [SORT BY col_list]
]
[LIMIT number]
ABSTRACT SYNTAX TREE:
  (TOK_QUERY (TOK_FROM (TOK_TABREF (TOK_TABNAME default test_2))) (TOK_INSERT (TOK_DESTINATION (TOK_DIR TOK_TMP_FILE)) (TOK_SELECT (TOK_SELEXPR TOK_ALLCOLREF))))

你可能感兴趣的:(hive)