Java 调用 Impala - JDBC 调用Impala

版权声明:原创文章,欢迎转载,转载请注明出处 https://blog.csdn.net/zhangshenghang/article/details/88686614

java通过JDBC 调用Impala服务

  • Maven项目中 pom.xml引用
        <dependency>
			<groupId>org.apache.hadoop</groupId>
			<artifactId>hadoop-common</artifactId>
			<version>2.7.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-metastore</artifactId>
			<version>1.2.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-service</artifactId>
			<version>1.2.1</version>
		</dependency>
  • Java代码
package com.izhonghong.report;

/**
 * @ClassName: xxxx
 * @Description: TODO JDBC Impala Demo
 * @author: Zsh
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Demo{
	
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		AnnualReport ar = new AnnualReport();
		long currentTimeMillis = System.currentTimeMillis();
		ar.count();
		System.out.println("耗时:"+(System.currentTimeMillis() - currentTimeMillis));
	}
	
    public  Connection getConnection() throws ClassNotFoundException, SQLException{
        String driver = "org.apache.hive.jdbc.HiveDriver";
        String JDBCUrl = "jdbc:hive2://ip:21050/;auth=noSasl";
        String username = "";
        String password = "";
        Connection conn = null;
        Class.forName(driver);
        conn = (Connection) DriverManager.getConnection(JDBCUrl,username,password);
        return conn;
    }
    
    public void count() throws ClassNotFoundException, SQLException{
        Connection conn = getConnection();
        String sql = "select count(1) from ods_xxx.xxx_other_article_hive where created_date >= 20180101 and created_date <=20181231 and text regexp '(厦门大学|厦大).{0,6}(女研究生|博士生|学生|女学生|吉吉良)' and text regexp '精日|辱华|两面人';";
        System.out.println("查询语句:"+sql);
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
       int columnCount = rs.getMetaData().getColumnCount();
        while (rs.next()){
            for(int i=1;i<=columnCount;i++){
                System.out.print(rs.getString(i)+"\t");
            }
            System.out.println("");
        }
    }
}
  • 注意:端口默认为21050,是Imapla向 HiveServer2 客户端请求提供服务所使用的端口。

CDH 中查看/修改

展开阅读全文

没有更多推荐了,返回首页