Before this, the Hive should be correctly installed. This not only means that you can enter hive, but also your hive can interact with the mysql you configured.
1. Jars needed. It’s better you import all .jar files in /Hive/lib. Besides, you need hadoop-common-2.3.0.jar and slf4j-api-1.6.6.jar.Include them:
activation-1.1.jar
ant-1.9.1.jar
ant-launcher-1.9.1.jar
antlr-2.7.7.jar
antlr-runtime-3.4.jar
asm-commons-3.1.jar
asm-tree-3.1.jar
avro-1.7.5.jar
bonecp-0.8.0.RELEASE.jar
commons-cli-1.2.jar
commons-codec-1.4.jar
commons-collections-3.1.jar
commons-compress-1.4.1.jar
commons-httpclient-3.0.1.jar
commons-io-2.4.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
datanucleus-api-jdo-3.2.6.jar
datanucleus-core-3.2.10.jar
datanucleus-rdbms-3.2.9.jar
derby-10.10.1.1.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-jaspic_1.0_spec-1.0.jar
geronimo-jta_1.1_spec-1.1.1.jar
groovy-all-2.1.6.jar
guava-11.0.2.jar
hadoop-common-2.3.0.jar
hamcrest-core-1.1.jar
hive-ant-0.13.1.jar
hive-beeline-0.13.1.jar
hive-cli-0.13.1.jar
hive-common-0.13.1.jar
hive-contrib-0.13.1.jar
hive-exec-0.13.1.jar
hive-hbase-handler-0.13.1.jar
hive-hwi-0.13.1.jar
hive-jdbc-0.13.1.jar
hive-metastore-0.13.1.jar
hive-serde-0.13.1.jar
hive-service-0.13.1.jar
hive-shims-0.13.1.jar
hive-shims-0.20-0.13.1.jar
hive-shims-0.20S-0.13.1.jar
hive-shims-0.23-0.13.1.jar
hive-shims-common-0.13.1.jar
hive-shims-common-secure-0.13.1.jar
hive-testutils-0.13.1.jar
httpclient-4.2.5.jar
httpcore-4.2.5.jar
jdo-api-3.0.1.jar
jetty-6.1.26.jar
jetty-all-7.6.0.v20120127.jar
jetty-util-6.1.26.jar
jline-0.9.94.jar
jpam-1.1.jar
jsr305-1.3.9.jar
jta-1.1.jar
junit-4.10.jar
libfb303-0.9.0.jar
libthrift-0.9.0.jar
log4j-1.2.16.jar
mail-1.4.1.jar
oro-2.0.8.jar
paranamer-2.3.jar
servlet-api-2.5-20081211.jar
servlet-api-2.5.jar
slf4j-api-1.6.6.jar
snappy-java-1.0.5.jar
ST4-4.0.4.jar
stax-api-1.0.1.jar
stringtemplate-3.2.1.jar
tempus-fugit-1.1.jar
velocity-1.5.jar
xz-1.0.jar
zookeeper-3.4.5.jar
2. Run the following code, and you can find your result.
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class HiveJdbcConenection {
- private static String driverName=”org.apache.hadoop.hive.jdbc.HiveDriver”;
- private static String url=”jdbc:hive://192.168.1.160:10000/default”;
- private static String userName=””;
- private static String password=””;
- public static void main(String[] args) {
- try {
- Class.forName(driverName);
- Connection conn = DriverManager.getConnection(url, “”, “”);
- Statement stmt = conn.createStatement();
- String sql = “select * from hive_1_1”;
- ResultSet rs = stmt.executeQuery(sql);
- while(rs.next()){
- String line = rs.getString(1) +”\t”+rs.getString(2)+”\t”+rs.getString(3);
- System.out.println(line);
- }
- } catch (SQLException e) {
- System.out.println(“url, userName or password is wrong!”);
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- System.out.println(“Driver is not found!”);
- e.printStackTrace();
- }
- }
- }
3. Result
Debug:
In my case, I got the error message when I ran my code.
org.apache.thrift.transport.TTransportException: java.net.SocketException: Connection reset
I used netstat -tulpn, I saw a process is taking up that port. The easiest way is that you kill it, and start the hive server again:
kill -15 PID
hive –service hiveserver &
Or, you can establish the hiveserver with a different port:
hive –service hiveserver -p 10001 &
The smybol ‘&’ means it starts in background. With it, you can still use your CLI while the Hiveserver is running.