XY个人笔记

1.Java自定义hive函数

2.hiveserver2服务与jdbc客户端

3.常用的Hql语句



Java自定义hive函数

一、首先创建一个maven项目

    创建maven项目,其名曰:hive。

二、导入jar 添加pom.xml

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-client</artifactId>
  <version>2.7.3</version>
</dependency>
<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-exec</artifactId>
  <version>1.2.1</version>
</dependency>
<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-jdbc</artifactId>
  <version>1.2.1</version>
</dependency>

    可添加阿里镜像

<repositories>
  <repository>
    <id>nexus-aliyun</id>
    <name>Nexus aliyun</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
  </repository>
</repositories>


三、创建Java文件


package com.hive.udf;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;

public class TestHiveUDF extends UDF {
	
	public Text evaluate(Text str){
		return this.evaluate(str, new IntWritable(0));
	}
	
	public Text evaluate(Text str,IntWritable flag){
		Text val = new Text("");
		
		if(str != null){
			int fg = flag.get();
			
			if(fg==0){
				val = new Text(str.toString().toLowerCase());
			} else if(fg==0) {
				val = new Text(str.toString().toUpperCase());
			} else {
				val = new Text(str.toString().toUpperCase());
			}
		}
		return val;
	}
	
}

四、打成jar包并上传到hive

    先上传至datas文件夹,使用hive命令添加jar

hive> add jar /opt/datas/hive_udf.jar;  #添加jar 
hive> list jar;    #查看jar 
hive> delete jar /opt/datas/hive_udf.jar;    #删除

五、创建、使用函数

hive> create temporary function my_lower as 'com.hive.udf.TestHiveUDF';    #创建函数
hive (db_mysql)> show functions;    #查看函数 
hive (db_mysql)> select dname,my_lower(dname,0) from dept;    #使用函数查询

    结果:


ceph java 写入 java写入hive_常用的HQL




hiveserver2服务与jdbc客户端

一、启动hiveserver2服务

$ bin/hiveserver2 &    #其中 & 代表后台启动 --启动服务端

hiveserver2服务的进程为RunJar

ceph java 写入 java写入hive_hive自定义函数_02

二、配置、启动beelin客户端

$ bin/beelin  #启动客户端

ceph java 写入 java写入hive_ceph java 写入_03

    启动完成后发现不能正常使用,那么怎么才能正常使用,链接到服务端呢?继续看……

    一般链接的话会有两种方式,官网提供方法如下

ceph java 写入 java写入hive_ceph java 写入_04

    方法1:

beeline> !connect jdbc:hive2://hadoop01.com:10000 ijeffrey 123456 
beeline> show databases; 
beeline> use db_mysql; 
beeline> show tables;
beeline> !quit  #退出

    

ceph java 写入 java写入hive_hive自定义函数_05

    方法2:

$ bin/beeline -u jdbc:hive2://hadoop01.com:10000 -n ijeffrey -p 123456   # -n 用户名 -p 密码 -w 指向文件

    

ceph java 写入 java写入hive_ceph java 写入_06

    这是用户的权限登录:

<property>
  <name>beeline.hs2.jdbc.url.tcpUrl</name>
  <value>jdbc:hive2://bigdata01.com:10000/default;user=hadoop;password=123456</value>
</property>

三、Java使用JDBC链接数据库

在官网上也提供了Java链接JDBC的代码这里就不在记录了

        https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-Beeline%E2%80%93CommandLineShell        JDBC Client Sample Code


四、Hive的配置模式分为三种
        --依据Hive的安装和metastore的设置机器而言
    1.嵌入模式:使用自带的derby数据库
    2.本地模式:将metastore放在mysql,并且mysql和hive安装在同一台机器上
    3.远程模式:将metastore放在mysql, mysql和hive安装在不同的机器上,元数据分离更加安全

    metastore服务提供了hiveserver2和mysql的链接服务

五、Fetch的三种模式
    1.none:     不管写什么sql都会去跑mapreduce
    2.minmal:      当select *、针对分区字段进行过滤、limit不跑mapreduce
    3.more          当select *、过滤、limit不跑mapreduce

set hive.metastore.uris  # 可以用过此处设置
<property>
  <name>
    hive.fetch.task.conversion
  </name>
  <value>
    more
  </value>
  <description>
    Expects one of [none, minimal, more].
    Some select queries can be converted to single FETCH task minimizing latency.
    Currently the query should be single sourced not having any subquery and should not have
    any aggregations or distincts (which incurs RS), lateral views and joins.
    0. none : disable hive.fetch.task.conversion
    1. minimal : SELECT STAR, FILTER on partition columns, LIMIT only
    2. more    : SELECT, FILTER, LIMIT only (support TABLESAMPLE and virtual columns)
  </description>
</property>



六、常用的Hql语句

【过滤】

select * from emp where sal > 3000;
select * from emp limit 5;
select distinct deptno from emp;
select * from emp where sal between 1300 and 3000;
select * from emp where sal >=1000 and  sal <= 3000;
select empno,ename from emp where comm is null;
select empno,ename from emp where comm is not null;
having和where可以一起使用,先where ,后执行having

【聚合函数】

count()、max()、min()、sum()、avg()、group by
select max(sal) from emp ;
select deptno max(sal) from emp ;
**select中出现的字段,需要用聚合函数包裹或者放入group by 当中
select deptno ,max(sal) from emp  group by deptno;
select max(deptno) ,max(sal) from emp ;

【join】

left join 、right join、inner join(等值)、full join(全)
等值join:
select a_test.id,a_test.name,b_test.id,b_test.phone from a a_test  join  b b_test  on  a_test.id=b_test.id;
左join:以左表为基准,没有匹配到的字段为NULL
select a_test.id,a_test.name,b_test.id,b_test.phone from a a_test  left join  b b_test  on  a_test.id=b_test.id;
右join:以右表为基准,没有匹配到的字段为NULL
select a_test.id,a_test.name,b_test.id,b_test.phone from a a_test  right join  b b_test  on  a_test.id=b_test.id;
全join:所有的字段都会出现,没有匹配到的字段为NULL
select a_test.id,a_test.name,b_test.id,b_test.phone from a a_test  full join  b b_test  on  a_test.id=b_test.id;
两张表做笛卡尔积:不写连接条件
select a_test.id,a_test.name,b_test.id,b_test.phone from a a_test  join  b b_test ;