HQL执行的三种方式

发布 : 2016-03-20 分类 : 大数据 浏览 :
1
2
3
4
5
1> hive -e 'hql'

2> hive -f 'hql.file'

3> hive jdbc代码执行脚本

Hive jdbc代码执行脚本

1.导入hive安装文件lib目录下的所有jar包

2.启动hive server

1
2
3
[root@node1 hive-1.2.1]# ./bin/hive --service hiveserver2
[root@node1 hive-1.2.1]# netstat -nptl | grep 10000
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN 31374/java

3.远程连接hive,在node1主机进入/opt/modules/hive-1.2.1目录,输入./bin/beeline

1
2
[root@node1 ~]# cd /opt/modules/hive-1.2.1
[root@node1 hive-1.2.1]# ./bin/beeline

1
beeline> !connect jdbc:hive2://192.168.230.10:10000 scott root org.apache.hive.jdbc.HiveDriver

3.1.查看hive中的表

1
2
0: jdbc:hive2://192.168.230.10:10000> show tables;
![](http://7xqkk1.com1.z0.glb.clouddn.com/16-3-13/61115028.jpg)

3.2.查询emp表

1
0: jdbc:hive2://192.168.230.10:10000> select * from emp;

4.在Eclipse上使用JDBC连接hive

TestHive.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.matrix.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TestHive {

public static void main(String[] args) {

Statement stat = null;
ResultSet rs = null;

try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn = DriverManager.getConnection("jdbc:hive2://node1:10000/default", "hive", "");
stat = conn.createStatement();
String sql = "select * from emp";
rs = stat.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getObject(1));
}
} catch (Exception e) {
e.printStackTrace();
}

}
}

本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2016/03/20/HQL执行的三种方式/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹