真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

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

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

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

可以參照以下代碼來(lái)實(shí)現(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代碼嗎

如果你的項(xiàng)目是java項(xiàng)目的話,就需要使用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

*

* 啟動(dòng)hive的遠(yuǎn)程服務(wù)接口命令行執(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)計(jì)操作

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”運(yùn)行結(jié)果:");

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 運(yùn)行結(jié)果:");

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 運(yùn)行結(jié)果:");

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 運(yùn)行結(jié)果:");

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連接的方式,當(dāng)然還有其他的連接方式,比如ODBC等, 這種方式很常用,可以在網(wǎng)上隨便找到,就不再累贅了。不穩(wěn)定,經(jīng)常會(huì)被大數(shù)據(jù)量沖掛,不建議使用。 3、這種方式是直接利用Hive的 Driver class 來(lái)直接連接,感覺(jué)這種方式不通過(guò)JDBC,應(yīng)該速度會(huì)比較快一點(diǎn)(未經(jīng)驗(yàn)證)。我只是在local模式下測(cè)試過(guò)。


當(dāng)前題目:java代碼連接hive java代碼連接redis集群
分享地址:http://weahome.cn/article/hjeico.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部