![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img2.ph.126.net/HL493ps92W9AcHqaPIB8Lg==/2178053369887087781.jpg)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/twNQeDdVmxSRJ-DGSSMPYw==/1856045996530102405.png)
3、创建表及插入数据
create table t(id int,msg string) row format delimited fields terminated by '\006' stored as textfile;
关于详细语法,可以看帮助文档或者网上搜一下。这里简单提点一下
row format delimited 指定分隔符,这里指定了006不可见字符作为分隔符
stored用来设置加在的数据类型,一般都是textfile。运行如下:
加载数据,已经被成功的分成了两列
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/JC8JPmjWohXijEeIX7NLRQ==/6597389724542430940.png)
4、分区表
Hive也能分区,这点真的非常强大。(如果对分区不了解,建议先看看oracle分区的相关知识)
create table t2(id int,msg string) partitioned by( indate string) row format delimited fields terminated by '\006' stored as textfile;
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/CFE7QyT9QrnUkDzSedrvHA==/698902367272789399.png)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/CRSMVKSoeIH7Of_lwF1KFQ==/6597615124425774811.png)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/af8PndwL0z7qRYyMy6aPYA==/849210004836085453.png)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img0.ph.126.net/sLX9GnwJs1N4ODwcSNWDhQ==/6598283627495382363.png)
分区原理:还没时间仔细看,粗看看应该是在hdfs里面生成了两个独立的文件,则运行时,只要运行对应的文件就可以了。
不过在我们集群上一般也就用到2-3台机器,就算加点数据,顶多再加个map,运行时间不会显著增加。所以个人感觉分区对我们现在业务帮助不大
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/IPVDIdMmarGZKwIwKgmdLA==/6608177033119856815.png)
5、数据导出
insert overwrite local directory '/tmp/t' select * from t;
数据时导出到指定目录下的文件
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/SWGvplvMzVq9AK9mocQ8Ow==/2143150472775132322.png)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img1.ph.126.net/okMCovY3CeKKFxgtwKgHpg==/1343761538916694552.png)
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img0.ph.126.net/TCmym1I62w8tGnug_K7rPw==/806144333399358066.png)
创建的时候没用MR,查询的时候启动了,所以可以推测创建时只保存了元数据,没有真正运行。只有查询的时候才启动Hadoop
7、执行计划
hive语句也有执行计划,雷到了
hive (test)> explain select count(1) from t a join t2 b on (a.id=b.id);
OK
ABSTRACT SYNTAX TREE:
(TOK_QUERY (TOK_FROM (TOK_JOIN (TOK_TABREF (TOK_TABNAME t) a) (TOK_TABREF (TOK_TABNAME t2) b) (= (. (TOK_TABLE_OR_COL a) id) (. (TOK_TABLE_OR_COL b) id)))) (TOK_INSERT (TOK_DESTINATION (TOK_DIR TOK_TMP_FILE)) (TOK_SELECT (TOK_SELEXPR (TOK_FUNCTION count 1)))))
STAGE DEPENDENCIES:
Stage-1 is a root stage
Stage-2 depends on stages: Stage-1
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-1
Map Reduce
Alias -> Map Operator Tree: --map阶段
a
TableScan
alias: a
Reduce Output Operator
key expressions:
expr: id
type: int
sort order: +
Map-reduce partition columns:
expr: id
type: int
tag: 0
b
TableScan
alias: b
Reduce Output Operator
key expressions:
expr: id
type: int
sort order: +
Map-reduce partition columns:
expr: id
type: int
tag: 1
Reduce Operator Tree: --reduce阶段
Join Operator
condition map:
Inner Join 0 to 1
condition expressions:
0
1
handleSkewJoin: false
Select Operator
Group By Operator
aggregations:
expr: count(1)
bucketGroup: false
mode: hash
outputColumnNames: _col0
File Output Operator
compressed: false
GlobalTableId: 0
table:
input format: org.apache.hadoop.mapred.SequenceFileInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveSequenceFileOutputFormat
Stage: Stage-2
Map Reduce
Alias -> Map Operator Tree:
hdfs://hc1:9000/tmp/hive-hc/hive_2013-12-30_14-05-40_946_5561877030225943803/-mr-10002
Reduce Output Operator
sort order:
tag: -1
value expressions:
expr: _col0
type: bigint
Reduce Operator Tree:
Group By Operator
aggregations:
expr: count(VALUE._col0)
bucketGroup: false
mode: mergepartial
outputColumnNames: _col0
Select Operator
expressions:
expr: _col0
type: bigint
outputColumnNames: _col0
File Output Operator
compressed: false
GlobalTableId: 0
table:
input format: org.apache.hadoop.mapred.TextInputFormat
output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Stage: Stage-0
Fetch Operator
limit: -1
Time taken: 0.576 seconds
然后导出成jar类,放到hive的一个节点下
- package com.zy.hive.function;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.hadoop.hive.ql.exec.UDF;
- public class range extends UDF{
- //构建IP查询库
- private static List<IpRange> ipLib = new ArrayList<IpRange>();
- static{
- for(int i=0;i<5695104;i++){
- ipLib.add(new IpRange(i,i+5,"USA"+i));
- }
- }
- //调用时执行的函数
- public String evaluate(int ip){
- IpRange ir;
- for(int i=0;i<ipLib.size();i++){
- ir = ipLib.get(i);
- if(ip>=ir.getStartip() && ip <= ir.getEndip()){
- return ir.getCountry();
- }
- }
- return null;
- }
- public static void main(String[] args) {
- range a = new range();
- for(int i=0;i<100;i++)
- System.out.println(a.evaluate(2));
- }
- }
- class IpRange{
- private int startip;
- private int endip;
- private String country;
- public IpRange(int startip, int endip, String country) {
- this.startip = startip;
- this.endip = endip;
- this.country = country;
- }
- public int getStartip() {
- return startip;
- }
- public void setStartip(int startip) {
- this.startip = startip;
- }
- public int getEndip() {
- return endip;
- }
- public void setEndip(int endip) {
- this.endip = endip;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- }
![Hive 基本使用 - scjthree - 亚存的博客 Hive 基本使用 - scjthree - 亚存的博客](http://img0.ph.126.net/zweisSzRj55UD1VK511NHg==/3131972065959811605.png)