java代碼連接hive java代碼連接redis集群

怎么用java編寫spark鏈接hive的程序

返回結果 將返回結果放到spark rdd 例如: JavaSparkContext sc = new JavaSparkContext(conf);

10余年的原陽網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整原陽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“原陽網(wǎng)站設計”,“原陽網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

如何在Java中執(zhí)行Hive命令或HiveQL

可以參照以下代碼來實現(xiàn):

String?sql="show?tables;?select?*?from?test_tb?limit?10";??

ListString?command?=?new?ArrayListString();??

command.add("hive");??

command.add("-e");??

command.add(sql);??

ListString?results?=?new?ArrayListString();??

ProcessBuilder?hiveProcessBuilder?=?new?ProcessBuilder(command);??

hiveProcess?=?hiveProcessBuilder.start();??

BufferedReader?br?=?new?BufferedReader(new?InputStreamReader(??

hiveProcess.getInputStream()));??

String?data?=?null;??

while?((data?=?br.readLine())?!=?null)?{??

results.add(data);??

}

hive 需要寫java代碼嗎

如果你的項目是java項目的話,就需要使用hive提供的java api,如下代碼:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.apache.log4j.Logger;

/**

* Hive的JavaApi

*

* 啟動hive的遠程服務接口命令行執(zhí)行:hive --service hiveserver /dev/null 2/dev/null

*

* @author 吖大哥

*

*/

public class HiveJdbcCli {

private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

private static String url = "jdbc:hive://hadoop3:10000/default";

private static String user = "hive";

private static String password = "mysql";

private static String sql = "";

private static ResultSet res;

private static final Logger log = Logger.getLogger(HiveJdbcCli.class);

public static void main(String[] args) {

Connection conn = null;

Statement stmt = null;

try {

conn = getConn();

stmt = conn.createStatement();

// 第一步:存在就先刪除

String tableName = dropTable(stmt);

// 第二步:不存在就創(chuàng)建

createTable(stmt, tableName);

// 第三步:查看創(chuàng)建的表

showTables(stmt, tableName);

// 執(zhí)行describe table操作

describeTables(stmt, tableName);

// 執(zhí)行l(wèi)oad data into table操作

loadData(stmt, tableName);

// 執(zhí)行 select * query 操作

selectData(stmt, tableName);

// 執(zhí)行 regular hive query 統(tǒng)計操作

countData(stmt, tableName);

} catch (ClassNotFoundException e) {

e.printStackTrace();

log.error(driverName + " not found!", e);

System.exit(1);

} catch (SQLException e) {

e.printStackTrace();

log.error("Connection error!", e);

System.exit(1);

} finally {

try {

if (conn != null) {

conn.close();

conn = null;

}

if (stmt != null) {

stmt.close();

stmt = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

private static void countData(Statement stmt, String tableName)

throws SQLException {

sql = "select count(1) from " + tableName;

System.out.println("Running:" + sql);

res = stmt.executeQuery(sql);

System.out.println("執(zhí)行“regular hive query”運行結果:");

while (res.next()) {

System.out.println("count ------" + res.getString(1));

}

}

private static void selectData(Statement stmt, String tableName)

throws SQLException {

sql = "select * from " + tableName;

System.out.println("Running:" + sql);

res = stmt.executeQuery(sql);

System.out.println("執(zhí)行 select * query 運行結果:");

while (res.next()) {

System.out.println(res.getInt(1) + "\t" + res.getString(2));

}

}

private static void loadData(Statement stmt, String tableName)

throws SQLException {

String filepath = "/home/hadoop01/data";

sql = "load data local inpath '" + filepath + "' into table "

+ tableName;

System.out.println("Running:" + sql);

res = stmt.executeQuery(sql);

}

private static void describeTables(Statement stmt, String tableName)

throws SQLException {

sql = "describe " + tableName;

System.out.println("Running:" + sql);

res = stmt.executeQuery(sql);

System.out.println("執(zhí)行 describe table 運行結果:");

while (res.next()) {

System.out.println(res.getString(1) + "\t" + res.getString(2));

}

}

private static void showTables(Statement stmt, String tableName)

throws SQLException {

sql = "show tables '" + tableName + "'";

System.out.println("Running:" + sql);

res = stmt.executeQuery(sql);

System.out.println("執(zhí)行 show tables 運行結果:");

if (res.next()) {

System.out.println(res.getString(1));

}

}

private static void createTable(Statement stmt, String tableName)

throws SQLException {

sql = "create table "

+ tableName

+ " (key int, value string) row format delimited fields terminated by '\t'";

stmt.executeQuery(sql);

}

private static String dropTable(Statement stmt) throws SQLException {

// 創(chuàng)建的表名

String tableName = "testHive";

sql = "drop table " + tableName;

stmt.executeQuery(sql);

return tableName;

}

private static Connection getConn() throws ClassNotFoundException,

SQLException {

Class.forName(driverName);

Connection conn = DriverManager.getConnection(url, user, password);

return conn;

}

}

java連接Hive的幾種方式

2、JDBC連接的方式,當然還有其他的連接方式,比如ODBC等, 這種方式很常用,可以在網(wǎng)上隨便找到,就不再累贅了。不穩(wěn)定,經(jīng)常會被大數(shù)據(jù)量沖掛,不建議使用。 3、這種方式是直接利用Hive的 Driver class 來直接連接,感覺這種方式不通過JDBC,應該速度會比較快一點(未經(jīng)驗證)。我只是在local模式下測試過。

網(wǎng)站標題:java代碼連接hive java代碼連接redis集群
文章起源:http://www.muchs.cn/article20/hjeico.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、軟件開發(fā)外貿(mào)建站自適應網(wǎng)站、定制網(wǎng)站企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司