javahive,javahive的config

本文目录一览:

如何在Java中执行Hive命令或HiveQL

这里所说的在Java中执行Hive命令或HiveQL并不是指Hive

Client通过JDBC的方式连接HiveServer(or

HiveServer2)执行查询,而是简单的在部署了HiveServer的服务器上执行Hive命令。当然这是一个简单的事情,平常我们通过Hive做简单的数据分析实验的时候,都是直接进入Hive执行HiveQL

通过进入Hive执行HiveQL,只能将分析结果打印到屏幕或是存入临时表,如果想把分析结果写入文件,或者对分析结果做进一步的分析,用程序做分析,就是为什么要在Java中执行Hive命令。

Java在1.5过后提供了ProcessBuilder根据运行时环境启动一个Process调用执行运行时环境下的命令或应用程序(1.5以前使用Runtime),关于ProcessBuilder请参考Java相关文档。调用代码如下:

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);

}其中command可以是其它Hive命令,不一定是HiveQL。

java中怎么实现查询出hive下所有数据库下表名

try {

Class.forName(“org.apache.hadoop.hive.jdbc.HiveDriver”);

String selectSql = “select * from db.data where address = ‘11111111’”;

Connection connect = DriverManager.getConnection(“jdbc:hive://192.168.xx.xx:10000/db”, “xxx”, “xxx”);

PreparedStatement state = null;

state = connect.prepareStatement(selectSql);

ResultSet resultSet = state.executeQuery();

while (resultSet != null resultSet.next()) {

System.out.println(resultSet.getString(1) + ” ” + resultSet.getString(2));

}

} catch (Exception e) {

e.printStackTrace();

}

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的远程服务接口命令行执行: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);

// 第二步:不存在就创建

createTable(stmt, tableName);

// 第三步:查看创建的表

showTables(stmt, tableName);

// 执行describe table操作

describeTables(stmt, tableName);

// 执行load data into table操作

loadData(stmt, tableName);

// 执行 select * query 操作

selectData(stmt, tableName);

// 执行 regular hive query 统计操作

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(“执行“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(“执行 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(“执行 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(“执行 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 {

// 创建的表名

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等, 这种方式很常用,可以在网上随便找到,就不再累赘了。不稳定,经常会被大数据量冲挂,不建议使用。 3、这种方式是直接利用Hive的 Driver class 来直接连接,感觉这种方式不通过JDBC,应该速度会比较快一点(未经验证)。我只是在local模式下测试过。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/181552.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-11-23 06:41
下一篇 2024-11-23 06:41

相关推荐

  • git config user.name的详解

    一、为什么要使用git config user.name? git是一个非常流行的分布式版本控制系统,很多程序员都会用到它。在使用git commit提交代码时,需要记录commi…

    编程 2025-04-25
  • Javahive:打破桎梏,自由编程

    一、Javahive是什么 Javahive是一款集成化的、能够进行多语言编程的开发平台。它支持Java、Python、JavaScript等多种编程语言,同时具备强大的编程辅助工…

    编程 2025-04-24
  • 深入理解nginx.config配置文件

    一、nginx.config文件结构 nginx是一款高性能的Web服务器,运行在Linux下,其配置文件位于“/etc/nginx/nginx.conf”。nginx.confi…

    编程 2025-04-23
  • 深入理解postcss.config.js

    一、postcss.config.js是什么 postcss.config.js是一个JavaScript文件,它用于配置PostCSS的插件和选项。它可以被用来处理CSS,从而增…

    编程 2025-02-25
  • Python Config模块

    一、 Config模块概述 Python的config模块是一个很常用的处理配置文件的库。 Python config模块提供了一个方便的方法来读写 INI 配置文件。 Pytho…

    编程 2025-01-20
  • config.json文件(configjson报错)

    本文目录一览: 1、config.json是什么文件 2、服务器参数配置文件config.json该怎么设置 3、微信小程序(上) 4、python中四种配置文件 5、OrthoF…

    编程 2025-01-11
  • config.php配置mysql(configsamplephp)

    本文目录一览: 1、apache php mysql配置 2、安装Phorum论坛时,怎样在config.php配置mysql连接? 3、config.php文件的配置问题!! 4…

    编程 2025-01-05
  • Git查看config

    一、基本介绍 在使用git进行版本控制时,我们需要使用git config来配置全局以及项目级别的变量,比如用户名和邮箱等。那么如何查看已经配置好的git config呢? 首先,…

    编程 2025-01-05
  • 包含config.json门罗币的词条

    本文目录一览: 1、xml币怎么挖 2、config.json是什么文件 3、如何修改游戏mod中后缀为json的文件? xml币怎么挖 具体步骤如下: 门罗币xmr挖矿需要准备两…

    编程 2024-12-29
  • layui.config详解

    一、layui.config什么意思 在了解layui.config之前,我们需要先了解 layui 静态文件加载器。它是一个简化的资源加载器,可以自动为模块分析和处理其依赖关系。…

    编程 2024-12-26

发表回复

登录后才能评论