1 ,pom 文件 :
<dependencies>
<dependency>
<groupId>lifecycle_commons</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</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.1.0</version>
</dependency>
</dependencies>
2 ,java 代码连接 hive
package com.lifecycle.myTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestHive {
private static String ip = "ec2-161-189-165-115.cn-northwest-1.compute.amazonaws.com.cn";
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://"+ip+":10000/da";
private static String user = "root";
private static String password = "root";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) throws Exception {
init();
selectData();
finish();
}
public static void init() throws Exception {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
}
public static void createDatabase() throws Exception {
String sql = "create database hive_jdbc_test";
System.out.println("Running: " + sql);
stmt.executeQuery(sql);
}
public static void dropDatabase() throws Exception {
String sql = "drop database if exists hive_jdbc_test";
System.out.println("Running: " + sql);
stmt.execute(sql);
}
public static void showDatabases() throws Exception {
String sql = "show databases";
System.out.println("Running: " + sql + "\n");
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
public static void createTable() throws Exception {
String sql = "create table t2(id int ,name String) row format delimited fields terminated by ',';";
System.out.println("Running: " + sql);
stmt.execute(sql);
}
public static void loadData() throws Exception {
String filePath = "s3://";
String sql = "load data local inpath '" + filePath + "' overwrite into table t2";
System.out.println("Running: " + sql);
stmt.execute(sql);
}
public static void selectData() throws Exception {
String sql = "select * from saleseason";
System.out.println("Running: " + sql);
stmt.execute("use da");
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
}
}
public static void drop(Statement stmt) throws Exception {
String dropSQL = "drop table t2";
boolean bool = stmt.execute(dropSQL);
System.out.println("删除表是否成功:" + bool);
}
public static void finish() throws Exception {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}