生活随笔
收集整理的這篇文章主要介紹了
Java 调用 Impala - JDBC 调用Impala
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java通過JDBC 調用Impala服務
<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>
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 中查看/修改
總結
以上是生活随笔為你收集整理的Java 调用 Impala - JDBC 调用Impala的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。