大数据学习 hive

继续以前MapReduce以后的大数据学习python

 

 

################################数据库

hive的安装:tar -zxcf hive.gz -C /app  安装hive;json

改配置hive-defualt.xmlapp

在hive/bin加jdbc jareclipse

在hadoop加jline.jar包ide

启动hdfs和yarn函数

启动hive,hive/binoop

 

 

hive> show databases; 查看数据库学习

           truncate table 表名;清空表;大数据

            drop table 表名;删除表;

create table User_b(id int,name string)  //定义好数据库表的格式
    > row format delimited
    > fields terminated by ',';

hadoop fs -put unicom.dat /user/hive/warehouse/bigdata.db/user_b //将按照格式要求的文件写好放入hdfs中

 select id,name from user_b order by name;  //hive利用hdfs存储数据,启用MapReduce查询数据;

 

hive用服务方式启动:

开启服务:bin/hiveserver

开启客户端  ./beeline

beeline>!connect jdbc:hive2//localhost:10000

 

hive 细节:

hive> create external table ext_user_b(id int ,name string)
    > row format delimited fields terminated by '\t'  //分隔字段
    > stored as textfile   //存储文件为textfile类型
    > location '/class03';  //存储位置

DML上传数据:

hive>load data local inpath '/home/hadoop/aaa.dat' into(插入,overwrite为覆盖) table ext_user_b;

 

drop table 外部表;//删除外部表,实体文件不会被删除;

 

分区:

create table part_user_b(id int ,name string)

partitioned by (country string) //区分属性,要和表中定义的属性不一样

row format delimited 

fields terminated by ',';

hive>load data local inpath '/home/hadoop/aaaChina.dat' into(插入,overwrite为覆盖) table part_user_b partition(country=‘China’);

hive>load local inpath '/home/hadoop/aaajapan.dat' into(插入,overwrite为覆盖) table part_user_b partition(country=‘Japan’);

select * from part_user_b where country='Japan'; //经过where,对不一样分区进行查询

 增长分区

alter table part_user_b add(输出为drop)partition(country=‘america’)

show partition part_user_b; //查看分区

select * from part_user_b where country='China'; //查询分区

 

分桶:

用查询结果存入分桶表

建立表:

create table buck_user_b(id int ,string name)

clustered by(id)

into 4 buckets

row format delimited fields terminated by ',' 

 

设置:

hive>set hive.enforce.bucketing = ture;

hive>set mapred.reduce.tasks=10; //分桶数要与建立表的设置同样-- 设置reduce的数量                                                               

 

 

插入数据

insert into table bubk

select id,name from stedent cluster by (id) ; 

或者 select id ,name from sudent distribute by (Sno) sort by (Sno);

 

查询结果保存:

create table 新建表 as select * from 查询表;

insert overwrite(into)table 表名;


join查询:

select * from a inner join b on a.id=b.id; //将两张表中id相同的条目聚合成一张表

//a.id a.name b.id b.name 

    2        xx        2        yy

    3        xx        3        rr

    4        xx        4        dd

select * from a left join b on a.id=b.id; //以a表为基准,a和b连成一张表;

//a.id a.name b.id b.name 

    1        xe        NULL    NULL

    2        xx        2        yy

    3        xx        3        rr

    4        xx        4        dd

select * from a semi join b on a.id=b.id; //至关于inner,只返回左边一半

select * from a full join b on a.id=b.id; //返回所有

思考:
查询与“刘晨”在同一个系学习的学生
  hive> select s1.Sname from student s1 left semi join student s2 on s1.Sdept=s2.Sdept and s2.Sname='刘晨';

 

hive函数:

查看官方文档;

自定义函数简单例子:

在eclipse写好功能后打成jar包;

hive>add jar /home/hadoop/document/weide_udf_touppercase.jar;//加入jar包

hive>create temporary function toup as 'cn.unicom.bigdata.hive.func.ToUppercase';//将jia包和自定义类名关联

 

函数处理json数据:

内置函数 get_json_object(line,$.id) as newid,get_json_object(line,$.id) as newid;

多层的json数据须要用自定会函数处理;

 

transform:

能够用python脚本做为处理文件处理数据;

相关文章
相关标签/搜索