hive查询语句入门(hive DDL)

hive DDL

  • 启动hadoop
/apps/hadoop/sbin/start-all.sh
  • 开启MySQL库,用于存放hive的元数据
sudo service mysql start
  • 启动hive
hive
  • 在/data/hive3下下载数据库数据
mkdir /data/hive3
cd data/hive3
wget http://192.168.1.100:60000/allfiles/hive3/buyer_log  
wget http://192.168.1.100:60000/allfiles/hive3/buyer_favorite 
  • 在hive中创建数据库并以'\t'为分隔符
create table buyer_log(id string,buyer_id string,dt string,ip string,opt_type string) row format delimited fields terminated by '\t' stored as textfile;
  • 将/data/hive3下的数据导入到hive中
load data local inpath '/data/hive3/buyer_log' into table buyer_log;
load data local inpath '/data/hive3/buyer_favorite' into table buyer_favorite;
  • 普通查询
select * from buyer_log limit 10;
  • 别名查询
select b.id,b.ip from buyer_log b limit 10;
  • 限定查询
select buyer_id from buyer_log where opt_type=1 limit 10;
  • 两表或多表联合查询
select l.dt,f.goods_id from buyer_log l,buyer_favorite f where l.buyer_id=f.buyer_id limit 10;
  • 多表插入
create table buyer_log1 like buyer_log;
create table buyer_log2 like buyer_log;
from buyer_log insert overwrite table buyer_log1 select * 
insert overwrite table buyer_log2 select *;
  • 多目录输出文件
from buyer_log
insert overwrite local directory '/data/hive3/out' select *;
insert overwrite local directory '/data/hive3/out1' select *;
  • 使用本地shell脚本调用hive查询语句
#!/bin/bash
cd /apps/hive/sbin;
hive -e 'show tables;'

chmod +x sh1
./sh1

猜你喜欢

转载自www.cnblogs.com/hannahzhao/p/11762691.html