hive入门二

分桶

分桶是根据某一字段进行分的,根据这一字段的hash值然后对分桶数取模,进入一个reducer,这个字段分的是mapreduce中的分区,将数据分开由不同的reduce计算。

创建分桶分区的表:

create table ip_table4(ip string, region string, country string,province string,city string,area string)

partitioned by(company string)

clustered by(province) into 2 buckets

row format delimited fields terminated by '|';

导入分桶分区的表

set hive.exec.dynamic.partition=true;

set hive.exec.dynamic.partition.mode=nonstrict;

set hive.enforce.bucketing = true;

insert overwrite table ip_table4 partition (company) select ip,country,province,area,city,region,company from ip_table1;

区分分桶和分区

分区是根据某一字段,相同的会汇总到一个reducer中,有点shuffle的意思,再一个表中,某一个字段有多少种值,就会有多少个分区。如果是根据两个字段分区,这两个字段值的种类有多少种组合就会有多少分区。比如在数据表中有个这样的字段word字段和sex字段,在建表的时候,partitioned by(word string,sex Sring)这两个字段分区。然后word字段一共有a,b,c,sex字段有female,male两个值,那个在分区的时候就会有6中分区,分别:word=a/sex=female, word=b/sex=female, word=c/sex=female, word=a/sex=male, word=b/sex=male, word=c/sex=male。当然只有有这个数据组合的时候才会在hdfs创建相关的文件,没有不会创建。

数据倾斜

数据倾斜是指,根据某一字段分区,某一值过多,导致数据都往一个分区里跑。

(很重要,需要深入去了解)

启动服务

服务端:

nohup hive --service metastore -p 9083 >/dev/null &   

nohup hive --service metastore -p 10050 >/dev/null &   

hive --service hiveserver2 默认端口10000

后台启动

nohup ./hive --service hiveserver2 >/dev/null &

jps后会有一个runjar程序就是它了

客户端:

./beeline

!connect jdbc:hive2://1.1.1.1:10000

处理错误:

User: adminuser is not allowed to impersonate adminuser (state=08S01,code=0)

Hdfs权限的原因:在core-site.xml 增加

<property>

<name>hadoop.proxyuser.(启动hdfs的用户).hosts</name>

<value>*</value>

</property>

<property>

<name>hadoop.proxyuser.(启动hdfs的用户).groups</name>

<value>*</value>

</property>

脚本运行

可以创建一个脚本,将sql语言写入到里面

例如hive.sh

#!/bin/bash

hive –e “create table ip_table(name string,sex string)”

hive –e “insert into ip_table select * from ip_table2”

执行:sh hive.sh

 

配置安装

配置hive-env.sh

# Set HADOOP_HOME to point to a specific hadoop install directory

HADOOP_HOME=/home/adminuser/app/hadoop

 

# Hive Configuration Directory can be controlled by:

export HIVE_CONF_DIR=/home/adminuser/app/hive/conf

 

# Folder containing extra libraries required for hive compilation/execution can be controlled by:

export HIVE_AUX_JARS_PATH=/home/adminuser/app/hive/lib

 

配置hive-site.xml

<configuration>

<property>

<name>hive.default.fileformat</name>

<value>TextFile</value>

</property>

<property>

<name>javax.jdo.option.ConnectionURL</name>

<value>jdbc:mysql://5.2.19.1:3306/hive2?createDatabaseIfNotExist=true</value>

</property>

<property>

<name>javax.jdo.option.ConnectionDriverName</name>

<value>com.mysql.jdbc.Driver</value>

</property>

<property>

<name>javax.jdo.option.ConnectionUserName</name>

<value>root</value>

</property>

<property>

<name>javax.jdo.option.ConnectionPassword</name>

<value>min123</value>

</property>

 

<property>

<name>hive.server2.thrift.port</name>

<value>10000</value>

</property>

<property>

<name>hive.server2.thrift.bind.host</name>

<value>localhost</value>

</property>

 

</configuration>

 

初始化hive

./schematool -initSchema -dbType mysql

 

Java客户端开发

启动服务./hiveserver2,端口默认10000

public class HiveSqlCli {
   
public static void main(String[] args) throws Exception {
       
//Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
       
Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection conn = DriverManager
                .getConnection(
"jdbc:hive2://1.23.4.5:10000", "adminuser", "");
        Statement st = conn.createStatement();
       
// select * from hive_test.ip_table4
       
ResultSet rs = st.executeQuery("show databases");

       
while (rs.next()){
            String string = rs.getString(
1);
            System.
out.println(string);
        }
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西门吹水之城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值