`

HIVE的安装与使用

 
阅读更多

 

1 解压到目录

2 配置文件修改元数据保存到mysql 

3 创建表,就是在hdfs中创建一个文件夹,load数据就是将数据文件拷贝到hdfs表对应的目录下面。

 

4 创建表(默认为表类型为MANAGED_TABLE,数据是在表的目录下面):

  数据就是文件,它可以一次插入一个文件数据,但不能插入一条数据。

CREATE TABLE page_vie (viewTime int ,useid BIGINT,

page_url STRING,referrer_url STRING,

ip STRING COMMENT 'IP Address of the User')

COMMENT 'This is the page view table'

PARTITIONED BY (dt STRING,country STRING)  //分区 

ROW FORMAT DELIMITED  FIELDS TERMINATED BY '\001' //数据是用什么来分割

   STORED AS SEQUENCEFILE; //这个可以不加,默认是文本格式

 

创建内部表:

 CREATE  table t_order(id int ,name string ,rongliang string,price double )

 row format delimited fields terminated by '\t'

 

 

 

5 创建External表 (1 数据可以在hdfs中任何地方,不必要放到表的文件夹中,创建的时候要指定文件夹)

 CREATE external table t_order_ex(id int ,name string ,rongliang string,price double )

 row format delimited fields terminated by '\t'

 location '/hive_ext'

 

 6 MANAGED_TABLE 删除的时候会删除表的目录以及目录中的内容,和表的元数据 

External表 删除的时候只会删除表的元数据,文件不会变。

 

create table t_order_2 (id int ,name string ,rongliang string ,price double)

row format delimited 

fields terminated by '\t'

 

8 数据导入

 

将本地文件导入到表中:

load data local inpath '/web/test_hive_data/phone.txt' into table t_order_2

将hdfs中的文件移动到到表中:

load data inpath '/phone3.txt' into table t_order_2 ;

 

9 用一个表创建另一个表,查询结果会单独存储 (会执行mapreduce,比较慢,内部表和外部表都会创建文件)

 

 1)用于创建临时表,存储中间结果

 create table t_order_s as

 select id ,name ,price from t_order ;

  2)创建一个表,和另一个表一样(表复制)

  create table t_order_2 like t_order ; 

  3)表中添加查询出来的数据,表中以前的数据会清除掉(就是将查询出来的数据弄成文件放到文件夹下面) 

  insert  overwrite table t_order_ow 

  select * from t_order ;

   4)查询出来的数据添加到表中,数据追加(查询结果可以直接放到本地文件或者hdfs文件中)

  insert  into table t_order_ow 

  select * from t_order ;

 

10 分区使用:分区字段可以是表中没有的字段 (数据保存就是在表下面有个分区的文件夹,然后里面放内容)

create table t_order_pt(id int ,name string ,rongliang string,price double )      

partitioned  by (month string)                                                    

row format delimited fields terminated by '\t' ; 

  分区数据加载:

  load data local inpath '/web/test_hive_data/phone.txt' into table t_order_pt partition(month='20150115') ;

  针对分区做查询与统计的时候,可以把分区作为字段直接where 如:

  select count(*) from t_order_pt where month='201502' //month是分区字段 

  可以动态增加分区 ,

 11 数据支持数组  

//array 

数据:

tony,wang,xiaoming  189,123,456,123

abc,lili,maya   7823,123,34

 

create table tab_array(a array<int>,b array<string>)

row format delimited

fields terminated by '\t'

collection items terminated by ',';

 

12 支持Map数据 

  wang   age:17;size:12A;addr:usr

  tony  age:23;size:32A;addr:bd 

//map

create table tab_map(name string,info map<string,string>)

row format delimited

fields terminated by '\t'

collection items terminated by ','

map keys terminated by ':';

13 结构类型:

create table tab_struct(name string,info struct<age:int,tel:string,addr:string>)

row format delimited

fields terminated by '\t'

collection items terminated by ','

 

14 hive脚本语言执行sql(pathon,shell等) ; 

   hive -S -e 'select * from db_test.t_order' ;  

 

15 自定义hive函数(JAVA)

1 写Java类,定义函数逻辑

2 打成jar包,上传到hive的lib中

3 在hive中创建函数 getArea,跟jar包

 

  1)创建java工程,实现UDF类,重写evaluate方法打成jar包,引入hive中lib下面的包

  2)自定义的jar包加入到classpath中:

add jar /web/hive-0.12.0/lib/hive_area.jar

 3) 创建函数与jar包关联:

     create TEMPORARY FUNCTION my_area AS 'jar类的全名称'

     如: create TEMPORARY FUNCTION get_area AS 'com.wzt.func.PoneToArea'

  4)创建表,并且准备数据:

执行sql:select get_area(phone) ,upflow,dflow from t_flow ;

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics