Hive jdbc 连接 Oracle数据库

2018-12-31 01:01 

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xzg1109/article/details/78831350

首先要启动 hiveserver2
[root@master ~]# hiveserver2
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hive2.1.1/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hive2.1.1/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
package jason.hive.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;

public class HiveJdbc {
	private static final String URL = "jdbc:hive2://192.168.50.130:10000";
	private static PreparedStatement ps;

	public static void main(String[] args) {
		try {
			Class cl = Class.forName("org.apache.hive.jdbc.HiveDriver");
			System.out.print(cl.getName());
			Connection conn = DriverManager.getConnection(URL, "root", "root"); // 这里的用户名使用和数据表在hdfs上的文件拥有者一致,以便在sql语句中调用hive的函数不会出现sql语句异常
			System.out.println("链接成功");
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(" select count(*)  from pdca_mes_line_t_test1");
			if (rs.next()) {
				System.out.println(rs.getInt(1));
			}

			String sql = "select * from pdca_mes_line_t_test1 ";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				System.out.println(rs.getObject(1) + "|" + rs.getObject(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

发表评论

您必须 登录 才能发表留言!