show databases;
使用正则筛选数据库名(使用x开头的数据库)
show databases like 'x.*';
创建数据库create database xtt;
防止报错
create database if not exists xtt;
添加描述
create database xtt2 comment 'is xtt2';
查看数据库descride database xtt;
删除数据库只能删除没有表的数据库
drop database xtt;
有表的数据库要添加cascade关键字,hive会先删除数据库中 表,然后就可以删除数据库了
drop database if exists xtt cascade;
使用数据库use xtt;
小练习 创建并使用school数据库,创建内部表student(id bigint, name string, score double, age int) 并加载数据文件student.txt到student表
建表语句
create table student(id bigint, name string, score double, age int) row format delimited fields terminated by ',' location '/student';
加载数据
load data local inpath '/root/student.txt' into table student;
在school数据库创建外部表student_external(id bigint, name string, score double, age int) ,并加载数据文件student.txt到student_external表
建表语句
create external table student_external(id bigint, name string, score double, age int) row format delimited fields terminated by ',' location '/student_external';
加载数据
load data local inpath '/root/student.txt' into table student_external;
创建并使用shop数据库,以sex string列为分区列创建分区表customer_partition(name string, age int)
创建并使用数据库
create database if not exists shop;use shop;
建表语句
create table customer_partition(name string, age int) partitioned by(sex string) row format delimited fields terminated by 't';
以静态分区方式先将数据文件customer.txt加载到分区表customer_partition的指定分区(sex=man)中,再将数据文件ccustomer_1.txt加载到分区表customer_partition的指定分区(sex=woman)中
load data local inpath '/root/customer.txt' into table customer_partition partition(sex='man');load data local inpath '/root/customer_1.txt' into table customer_partition partition(sex='woman');
创建外部表customer_external(name string, age int) 并加载数据文件customer_external.txt到customer_external表
create external table customer_external(name string, age int) row format delimited fields terminated by 't';load data local inpath '/root/customer_external.txt' into table customer_external;
以sex和native 两列为分区列创建分区表customer_partion_dynamic;再以动态分区方式,将customer_external表的数据动态插入到分区表customer_partition_dynamic
第三题不对劲,好像搞错了,这题写不了
先创建并使用sogou数据库,再创建内部表sogou500w( ts string, uid string, keyword string, rank int, orders int, url string) ,再加载本地数据文件sogou.500w.utf8到sogou500w表
create database sogou;
use sogou;
create table if not exists sogou500w( ts string, uid string, keyword string, rank int, orders int, url string) row format delimited fields terminated by 't' ;
load data local inpath '/root/sogou.500w.utf8' overwrite into table sogou500w;
2 查询sogou500w表,用where语句筛选出关键词keword包含"baidu"的前10行数据
select * from sogou500w where keyword like 'baidu' limit 10;
3 使用group语句、having语句和count函数,统计keyword包含"baidu"的每个用户uid的搜索次数大于10次的uid和搜索次数
select uid, count(*) as cnt from sogou500w where keyword like 'baidu' group by uid, keyword having cnt>10;
4 使用order by语句,统计所有用户uid的搜索次数,并列出搜索次数排前10位的用户uid和搜索次数
select uid, count(*) as cnt from sogou500w group by uid order by cnt desc limit 10;
逢考必过一键三连