常用的配置信息含义:
hive.metastore.warehouse.dir 数据仓库存放在位置
set hive.matestore.warehouse.dir=/user/hive/warehouse
mysql存放元数据,如表的分区和模式。
hive –e “语句” 执行一条语句;一次使用的的命令
hive –f “文件”执行一个文件;从文件中执行hive查询
hive> source /path/withquery.hql 在hive中也可 source来执行文件
hive>dfs –ls hive> !pwd 注释使用“--”
set hive.cli.print.current.db=true 显示数据库的; set hive.cli.print.header=true 显示字字段名
-------------------------------------------------------------------------------------------------------------------------------
数据类型:tinyint 1byte, smalint 2byte , int 4 byte , bigint 8byte,
boolean, float, double, string, timestamp, binary 集合类型:struct ,map,array
类型之间的转化:cast( s as int ) 使用 cast关键字。
------------------------------------------------------------------------------------------------------------------------------
create database { if not exists }test_database
show database
show database like ‘h.*’
create database financials location ‘/my/data/dictory’
alter databasefinancials set dbproperties(‘edit-by’=’vincent’) 数据库的元数据不可修改,包括数据库名、数据库所在位置。
表:创建:
use ${hive_db};
DROP TABLE ${table_name};
CREATE EXTERNAL TABLE ${table_name}
(
uuid string comment '',
cookie_list string comment 'cookie列表',
place_list string comment '产地信息',
)PARTITIONED BY(dtSTRING)
ROW FORMAT DELIMITEDFIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
STORED AS INPUTFORMAT'com.hadoop.mapred.DeprecatedLzoTextInputFormat'
OUTPUTFORMAT'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION'${HDFS_DIR}/${table_level}/${table_name}';
ALTER TABLE ${table_name} SETSERDEPROPERTIES('serialization.null.format'='');
显示表的结构:desc formatted vincent.test_table;
管理表(内部表),这些表的存储由配置项 hive.metastore.warehouse.dir下的目录下。
外部表:createexternal table if not exists stocks(exchange string…..) location ‘/data/stocks’ 外部表被删除时,这个数据并不会被删除,不过,描述表的元数据信息会被删除掉。
分区表,(外部分区表,内部分区表)对数据进行分区,最重要的原因就是为了更快的查询。
若hive设置为‘strict(严格)’模式,则如果对分区进行查询时,where子句中没有加分区过滤的话,是无法提交这个任务的。
hive>set hive.mapred.mode=strict hive>set hive.mapred.mode=nonstrict hive> show partitions employees 查看表中存在的所有分区。
hadoop fs –cp 源文件 目标文件 对于小数据集的复制,hadoop distcp 源文件目标文件
hadoop distcp是大数据的复制,它是多线程的,是基于MR来实现的。
自定义表的存储格式:
1) 序列化与反序列化:(缩写为 serDe) stored as textfile outputformat “……”
删除表:hive>drop table if exists employees; 对于管理表,元数据和表内的数据都会被删除,而对于外部表,仅是元数据会删除,但表内的数据不会被删除。
提示:用户可以开启hadoop回收站的功能fs.trash.interval ,时间默认是 1440即24小时
表命名:alter table log_message rename to logmes
增加,修改,删除分区:
alter table logmes add if not existe partition (….) location ‘….’
alter table logmes partition (….) set location ‘….’
alter table logmes drop if exists partition (….)
修改列信息:
alter table logmes change colume …… after …..
alter table logmes add columns (…);
alter table logmes replace columns(….)
修改存储属性:
alter tablelogmes partition (…) set fileformat sequencefile;
加载数据:
load data local input‘/usr/data/myfiles’overwrite into tableemployees partition(…)
load data input‘hdfs://master:90000/user/data’overwrite into table employees partition(…)
insert overwrite/into tableemployeespartition(….) select * from emp where ….
导出数据:hadoop fs –cp source_path target_path 或
insert overwrite local directory ‘/tmp/employees’select name,… from emplyees where …
这会有一个或多个文件写入到employees中,具体的个数,取决于reduce的个数。