案例: jdbc 操作hive数据库
1.依赖jar包:注意版本冲突的问题
hive-exec-0.13.1.jar
hive-jdbc-0.13.1.jar
hive-metastore-0.13.1.jar
hive-service-0.13.1.jar
hadoop-client-1.2.1.jar
hadoop-common-2.3.0.jar
编写 jdbc 代码:
public class HiveJdbcTest {
static {
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws SQLException {
try {
Statement stmt = null;
try {
Connection con = DriverManager.getConnection("jdbc:hive2://hive数据库主机:hive数据库端口/hive数据库名称", "用户名", "密码");
stmt = con.createStatement();
} catch (Exception e) {
e.printStackTrace();
return;
}
String createSQL = "create table table01(id string,name string )";
boolean res = stmt.execute(createSQL);
System.out.println(" createTableInHive: end exec !! res:" + !res);
// String sql =
// "create view if not exists 库名.view03 (id ,name) comment '批量' as select id,name from 库名.table01";
// System.out.println("Running3: " + sql);
// boolean temp = stmt.execute(sql);
// System.out.println(" createViewInHive: end exec !! temp:" + !temp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
还没有评论,来说两句吧...