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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//(hive中的数据库和表在HDFS中存放的文件夹的位置)
default
database
for
the warehouse
10000
//(HiveServer2远程连接的端口,默认为10000)
interface
.
Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT
//(hive所在集群的IP地址)
interface
.
Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST
long
.polling.timeout
5000
// (默认为5000L,此处修改为5000,不然程序会报错)
long
polling
//localhost:3306/hive?createDatabaseIfNotExist=true //(Hive的元数据库,我采用的是本地Mysql作为元数据库)
for
a JDBC metastore
//(连接元数据的驱动名)
class
name
for
a JDBC metastore
//(连接元数据库用户名)
// (连接元数据库密码)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@centos7-2 apache-hive-2.1.1-bin]# cd /tmp/root/
[root@centos7-2 root]# ls
|
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
30
31
32
33
34
35
36
37
38
39
40
41
42
|
2016
-
04
-
26
04
:
53
:
24
,
212
INFO [main]: server.HiveServer2 (HiveStringUtils.java:startupShutdownMessage(
605
)) - STARTUP_MSG:
/************************************************************
STARTUP_MSG: Starting HiveServer2
STARTUP_MSG: host = master/(你之前配置的IP)
STARTUP_MSG: args = []
STARTUP_MSG: version = 0.13.0
STARTUP_MSG: classpath = /opt/modules/hadoop-2.2.0/etc/hadoop:/opt/modules/hadoop-2.2.0/share/hadoop/common/lib
//(……中间略掉classpath内容,日志信息太长……)
STARTUP_MSG: build = file:///Users/hbutani/svn/branch-0.13 -r Unknown; compiled by 'hbutani' on Tue Apr 15 13:55:42 PDT 2014
************************************************************/
2016
-
04
-
26
04
:
53
:
24
,
553
WARN [main]: conf.HiveConf (HiveConf.java:initialize(
1390
)) - DEPRECATED: hive.metastore.ds.retry.* no longer has any effect. Use hive.hmshandler.retry.* instead
2016
-
04
-
26
04
:
53
:
25
,
258
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:newRawStore(
494
)) -
0
: Opening raw store with implemenation
class
:org.apache.hadoop.hive.metastore.ObjectStore
2016
-
04
-
26
04
:
53
:
25
,
325
INFO [main]: metastore.ObjectStore (ObjectStore.java:initialize(
245
)) - ObjectStore, initialize called
2016
-
04
-
26
04
:
53
:
28
,
312
WARN [main]: conf.HiveConf (HiveConf.java:initialize(
1390
)) - DEPRECATED: hive.metastore.ds.retry.* no longer has any effect. Use hive.hmshandler.retry.* instead
2016
-
04
-
26
04
:
53
:
28
,
313
INFO [main]: metastore.ObjectStore (ObjectStore.java:getPMF(
314
)) - Setting MetaStore object pin classes with hive.metastore.cache.pinobjtypes=
"Table,StorageDescriptor,SerDeInfo,Partition,Database,Type,FieldSchema,Order"
2016
-
04
-
26
04
:
53
:
31
,
537
INFO [main]: metastore.ObjectStore (ObjectStore.java:setConf(
228
)) - Initialized ObjectStore
2016
-
04
-
26
04
:
53
:
32
,
064
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:createDefaultRoles(
552
)) - Added admin role in metastore
2016
-
04
-
26
04
:
53
:
32
,
079
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:createDefaultRoles(
561
)) - Added
public
role in metastore
2016
-
04
-
26
04
:
53
:
32
,
205
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:addAdminUsers(
589
)) - No user is added in admin role, since config is empty
2016
-
04
-
26
04
:
53
:
33
,
887
INFO [main]: session.SessionState (SessionState.java:start(
358
)) - No Tez session required at
this
point. hive.execution.engine=mr.
2016
-
04
-
26
04
:
53
:
34
,
168
WARN [main]: conf.HiveConf (HiveConf.java:initialize(
1390
)) - DEPRECATED: hive.metastore.ds.retry.* no longer has any effect. Use hive.hmshandler.retry.* instead
2016
-
04
-
26
04
:
53
:
34
,
241
INFO [main]: service.CompositeService (SessionManager.java:init(
70
)) - HiveServer2: Async execution thread pool size:
100
2016
-
04
-
26
04
:
53
:
34
,
241
INFO [main]: service.CompositeService (SessionManager.java:init(
72
)) - HiveServer2: Async execution wait queue size:
100
2016
-
04
-
26
04
:
53
:
34
,
242
INFO [main]: service.CompositeService (SessionManager.java:init(
74
)) - HiveServer2: Async execution thread keepalive time:
10
2016
-
04
-
26
04
:
53
:
34
,
244
INFO [main]: service.AbstractService (AbstractService.java:init(
89
)) - Service:OperationManager is inited.
2016
-
04
-
26
04
:
53
:
34
,
247
INFO [main]: service.AbstractService (AbstractService.java:init(
89
)) - Service:SessionManager is inited.
2016
-
04
-
26
04
:
53
:
34
,
247
INFO [main]: service.AbstractService (AbstractService.java:init(
89
)) - Service:CLIService is inited.
2016
-
04
-
26
04
:
53
:
34
,
247
INFO [main]: service.AbstractService (AbstractService.java:init(
89
)) - Service:ThriftBinaryCLIService is inited.
2016
-
04
-
26
04
:
53
:
34
,
247
INFO [main]: service.AbstractService (AbstractService.java:init(
89
)) - Service:HiveServer2 is inited.
2016
-
04
-
26
04
:
53
:
34
,
248
INFO [main]: service.AbstractService (AbstractService.java:start(
104
)) - Service:OperationManager is started.
2016
-
04
-
26
04
:
53
:
34
,
248
INFO [main]: service.AbstractService (AbstractService.java:start(
104
)) - Service:SessionManager is started.
2016
-
04
-
26
04
:
53
:
34
,
248
INFO [main]: service.AbstractService (AbstractService.java:start(
104
)) - Service:CLIService is started.
2016
-
04
-
26
04
:
53
:
34
,
698
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:addAdminUsers(
589
)) - No user is added in admin role, since config is empty
2016
-
04
-
26
04
:
53
:
34
,
699
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:logInfo(
624
)) -
0
: get_databases:
default
2016
-
04
-
26
04
:
53
:
34
,
701
INFO [main]: HiveMetaStore.audit (HiveMetaStore.java:logAuditEvent(
306
)) - ugi=hh ip=unknown-ip-addr cmd=get_databases:
default
2016
-
04
-
26
04
:
53
:
34
,
725
INFO [main]: metastore.HiveMetaStore (HiveMetaStore.java:newRawStore(
494
)) -
0
: Opening raw store with implemenation
class
:org.apache.hadoop.hive.metastore.ObjectStore
2016
-
04
-
26
04
:
53
:
34
,
728
INFO [main]: metastore.ObjectStore (ObjectStore.java:initialize(
245
)) - ObjectStore, initialize called
2016
-
04
-
26
04
:
53
:
34
,
745
INFO [main]: metastore.ObjectStore (ObjectStore.java:setConf(
228
)) - Initialized ObjectStore
2016
-
04
-
26
04
:
53
:
34
,
795
INFO [main]: service.AbstractService (AbstractService.java:start(
104
)) - Service:ThriftBinaryCLIService is started.
2016
-
04
-
26
04
:
53
:
34
,
796
INFO [main]: service.AbstractService (AbstractService.java:start(
104
)) - Service:HiveServer2 is started.
2016
-
04
-
26
04
:
53
:
34
,
947
WARN [Thread-
5
]: conf.HiveConf (HiveConf.java:initialize(
1390
)) - DEPRECATED: hive.metastore.ds.retry.* no longer has any effect. Use hive.hmshandler.retry.* instead
2016
-
04
-
26
04
:
53
:
35
,
584
INFO [Thread-
5
]: thrift.ThriftCLIService (ThriftBinaryCLIService.java:run(
88
)) - ThriftBinaryCLIService listening on /(你的IP):
10000
|
1
2
|
[hh
@master
Desktop]$ netstat -nl |grep
10000
tcp
0
0
(你的IP):
10000
0.0
.
0.0
:* LISTEN
|
1
2
3
4
5
6
7
8
|
hadoop-
2.2
.
0
/share/hadoop/common/hadoop-common-
2.2
.
0
.jar
$HIVE_HOME/lib/hive-exec-
0.11
.
0
.jar
$HIVE_HOME/lib/hive-jdbc-
0.11
.
0
.jar
$HIVE_HOME/lib/hive-metastore-
0.11
.
0
.jar
$HIVE_HOME/lib/hive-service-
0.11
.
0
.jar
$HIVE_HOME/lib/libfb303-
0.9
.
0
.jar
$HIVE_HOME/lib/commons-logging-
1.0
.
4
.jar
$HIVE_HOME/lib/slf4j-api-
1.6
.
1
.jar
|
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
30
31
32
33
34
35
|
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.SQLException;
public
class
JDBCToHiveUtils {
private
static
String driverName =
"org.apache.hive.jdbc.HiveDriver"
;
private
static
String Url=
"jdbc:hive2://**.**.**.**:10000/default"
; //填写hive的IP,之前在配置文件中配置的IP
private
static
Connection conn;
public
static
Connection getConnnection()
{
try
{
Class.forName(driverName);
conn = DriverManager.getConnection(Url,
"hh"
,
""
);
//此处的用户名一定是有权限操作HDFS的用户,否则程序会提示"permission deny"异常
}
catch
(ClassNotFoundException e) {
e.printStackTrace();
System.exit(
1
);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
conn;
}
public
static
PreparedStatement prepare(Connection conn, String sql) {
PreparedStatement ps =
null
;
try
{
ps = conn.prepareStatement(sql);
}
catch
(SQLException e) {
e.printStackTrace();
}
return
ps;
}
}
|
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
30
31
32
33
34
35
|
import
java.sql.Connection;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
public
class
QueryHiveUtils {
private
static
Connection conn=JDBCToHiveUtils.getConnnection();
private
static
PreparedStatement ps;
private
static
ResultSet rs;
public
static
void
getAll(String tablename)
{
String sql=
"select * from "
+tablename;
System.out.println(sql);
try
{
ps=JDBCToHiveUtils.prepare(conn, sql);
rs=ps.executeQuery();
int
columns=rs.getMetaData().getColumnCount();
while
(rs.next())
{
for
(
int
i=
1
;i<=columns;i++)
{
System.out.print(rs.getString(i));
System.out.print(
"\t\t"
);
}
System.out.println();
}
}
catch
(SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|
1
2
3
4
5
6
7
8
|
public
class
QueryHiveTest {
public
static
void
main(String[] args) {
String tablename=
"test1"
;
QueryHiveUtils.getAll(tablename);
}
}
|
1
2
3
4
5
|
select * from test1
1
张三 男
20.0
2
李四 女
35.0
3
王五 男
null
4
赵六
null
70.0
|
参考:点击打开链接