java连接hive_java jdbc连接hive

该博客展示了如何利用SpringBoot应用连接到Impala数据库,并执行查询和插入操作。代码示例中指定了Impala的主机和端口,然后加载Hive JDBC驱动并建立连接,最后执行SQL语句从student2表中读取数据。
摘要由CSDN通过智能技术生成

pom.xml

org.apache.hive

hive-jdbc

1.1.0

org.apache.hive

hive-metastore

1.1.0

org.apache.hive

hive-service

1.1.0

org.apache.hadoop

hadoop-common

2.6.0

代码

import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.*;

@SpringBootApplication

public class ImpalademoApplication {

public static void main(String[] args) {

//SpringApplication.run(ImpalademoApplication.class, args);

test();

}

// set the impalad host

private static final String IMPALAD_HOST = "192.168.91.143";

// port 21050 is the default impalad JDBC port

private static final String IMPALAD_JDBC_PORT = "10000";

private static final String CONNECTION_URL = "jdbc:hive2://" + IMPALAD_HOST + ':' + IMPALAD_JDBC_PORT + "/myhive";

private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";

public static void test() {

System.out.println("\n=============================================");

System.out.println("Cloudera Impala JDBC Example");

System.out.println("Using Connection URL: " + CONNECTION_URL);

Connection con = null;

try {

Class.forName(JDBC_DRIVER_NAME);

con = DriverManager.getConnection(CONNECTION_URL,"hive","");

insert(con);

PreparedStatement stmt = con.prepareStatement("SELECT * FROM student2");

ResultSet rs = stmt.executeQuery();

System.out.println("\n== Begin Query Results ======================");

// print the results to the console

while (rs.next()) {

// the example query returns one String column

System.out.print(rs.getString("id") + "\t");

System.out.print(rs.getString("name") + "\t");

System.out.println(rs.getString("address"));

}

System.out.println("== End Query Results =======================\n\n");

} catch (SQLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

con.close();

} catch (Exception e) {

// swallow

}

}

}

public static void insert(Connection con) {

String sql = " insert into student2 select id,name,address from student";

try {

PreparedStatement stmt = con.prepareStatement(sql);

stmt.execute();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值