/
HiveServer2JUnitRule.java
108 lines (95 loc) · 3.02 KB
/
HiveServer2JUnitRule.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright (C) 2015-2021 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hotels.beeju;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import org.apache.hive.jdbc.HiveDriver;
import org.junit.runner.Description;
import com.hotels.beeju.core.HiveServer2Core;
/**
* A JUnit Rule that creates a HiveServer2 service and Thrift Metastore service backed by a Hive Metastore using an
* HSQLDB in-memory database.
* <p>
* A fresh database instance will be created for each test method.
* </p>
* <p>
* Note that this class is meant to be used for DDL operations only. Any attempt to run DML operations may result in
* errors.
* </p>
*/
public class HiveServer2JUnitRule extends BeejuJUnitRule {
private HiveServer2Core hiveServer2Core = new HiveServer2Core(core);
/**
* Create a HiveServer2 service with a pre-created database "test_database".
*/
public HiveServer2JUnitRule() {
this("test_database");
}
/**
* Create a HiveServer2 service with a pre-created database using the provided name.
*
* @param databaseName Database name.
*/
public HiveServer2JUnitRule(String databaseName) {
this(databaseName, null);
}
/**
* Create a HiveServer2 service with a pre-created database using the provided name and configuration.
*
* @param databaseName Database name.
* @param configuration Hive configuration properties.
*/
public HiveServer2JUnitRule(String databaseName, Map<String, String> configuration) {
super(databaseName, configuration);
}
@Override
public void starting(Description description) {
try {
hiveServer2Core.startServerSocket();
} catch (IOException e) {
throw new UncheckedIOException("Error starting HiveServer2 server socket", e);
}
super.starting(description);
try {
hiveServer2Core.initialise();
} catch (InterruptedException e) {
throw new RuntimeException("Error initialising HiveServer2 core", e);
}
}
@Override
public void finished(Description description) {
try {
hiveServer2Core.shutdown();
} finally {
super.finished(description);
}
}
/**
* @return the name of the Hive JDBC driver class used to access the database.
*/
@Override
public String driverClassName() {
return HiveDriver.class.getName();
}
/**
* @return {@link com.hotels.beeju.core.HiveServer2Core#getJdbcConnectionUrl()}.
*/
@Override
public String connectionURL() {
return hiveServer2Core.getJdbcConnectionUrl();
}
}