大数据hive的基本操作以及建立数据库,单词的统计

创建数据库
① 创建数据库hive
hive> create database hive;
② 创建数据库hive,因为hive已经存在,所以会抛出异常,加上if not exists关
键字,则不会抛出异常
hive> create database if not exists hive;
• 创建表
① 在hive数据库中,创建表usr,含三个属性id,name,age
hive> use hive;
hive>create table if not exists usr(id bigint,name string,age int);
② 在hive数据库中,创建表usr,含三个属性id,name,age,存储路径为
“/usr/local/hive/warehouse/hive/usr”
hive>create table if not exists hive.usr(id bigint,name string,age int)
>location ‘/usr/local/hive/warehouse/hive/usr’;

创建表
③ 在hive数据库中,创建外部表usr,含三个属性id,name,age,可以读取路径
“/usr/local/data”下以“,”分隔的数据。
hive>create external table if not exists hive.usr(id bigint,name string,age int)
>row format delimited fields terminated by ','
location ‘/usr/local/data’;
④ 在hive数据库中,创建分区表usr,含三个属性id,name,age,还存在分区字
段sex。
hive>create table hive.usr(id bigint,name string,age int) partition by(sex boolean);
⑤ 在hive数据库中,创建分区表usr1,它通过复制表usr得到。
hive> use hive;
hive>create table if not exists usr1 like usr;
• 创建视图
① 创建视图little_usr,只包含usr表中id,age属性
hive>create view little_usr as select id,age from usr;

(3)进入hive命令行界面,编写HiveQL语句实现WordCount算
法,命令如下:
$ hive
hive> create table docs(line string);
hive> load data inpath 'input' overwrite into table docs;
hive>create table word_count as
select word, count(1) as count from
(select explode(split(line,' '))as word from docs) w
group by word
order by word;
执行完成后,用select语句查看运行结果如下:

本周学习时长17小时

下周开学,已经完成9周的学习时间。