hive操作数据表之DML操作 hive的很多操作方式是与sql是一样的,这里先说一下DML操作的语句
数据库操作 数据库列表 1 hive (default)> show databases;
创建数据库 1 2 3 4 5 6 7 8 CREATE DATABASE [IF NOT EXISTS ] database_name[COMMENT database_comment] [LOCATION hdfs_path] [WITH DBPROPERTIES (property_name=property_value, ...)];
1 2 # CREATE DATABASE [IF NOT EXISTS] database_name; CREATE DATABASE IF NOT EXISTS study_hive;
查看数据库详细信息 1 2 3 4 5 6 7 8 hive (default)> desc database study_hive; OK db_name comment location owner_name owner_type parameters study_hive hdfs://localhost:9000/user/hive/warehouse/study_hive.db zhanghe USER # 也可以使用 desc database extended study_hive;
这里默认是存储在hdfs://localhost:9000/user/hive/warehouse/下,目录名为数据库名.db
可以在创建数据库的时候进行设置
如 CREATE DATABASE IF NOT EXISTS study_hive location ‘/11’;
删除数据库 1 2 3 4 # 注意:只能删除空数据库,如果不是空数据库会报错FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database study_hive1 is not empty. One or more tables exist.) hive (study_hive)> drop database study_hive1; # 删除非空的数据库 hive (study_hive)> drop database study_hive1 cascade;
数据表操作 创建表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 CREATE [EXTERNAL ] TABLE [IF NOT EXISTS ] table_name[(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] [ROW FORMAT row_format] [STORED AS file_format] [LOCATION hdfs_path] [TBLPROPERTIES (property_name=property_value, ...)] [AS select_statement]
删除表
修改表名 1 ALTER TABLE `user` RENAME TO `user1`
修改表中列 1 2 ALTER TABLE `user` CHANGE COLUMN id user_id int ;
增加表中列 1 2 ALTER TABLE `user` ADD COLUMNS (user_name string );