「hive和java」hive和Java

博主:adminadmin 2022-12-02 00:08:09 48

今天给各位分享hive和java的知识,其中也会对hive和Java进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何在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的几种方式

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

想问一下数据仓库(Hive)和Java 开发哪个更有前途?跪求答案,越详细越好

hive是数据分析,java是程序设计;两者就业率都不错,

去搞数据吧。工资高,加班少,比程序有前途;

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;

}

}

我想学习hive,请问安装hive之前,必须安装centos、hadoop、java这些吗?

安装需要

java 1.6,java 1.7或更高版本。

Hadoop 2.x或更高, 1.x. Hive 0.13 版本也支持 0.20.x, 0.23.x

Linux,mac,windows操作系统。以下内容适用于linux系统。

安装打包好的hive

需要先到apache下载已打包好的hive镜像,然后解压开该文件

$ tar -xzvf hive-x.y.z.tar.gz

设置hive环境变量

$ cd hive-x.y.z$ export HIVE_HOME={{pwd}}

设置hive运行路径

$ export PATH=$HIVE_HOME/bin:$PATH

编译Hive源码

下载hive源码

此处使用maven编译,需要下载安装maven。

以Hive 0.13版为例

编译hive 0.13源码基于hadoop 0.23或更高版本

$cdhive$mvncleaninstall-Phadoop-2,dist$cdpackaging/target/apache-hive-{version}-SNAPSHOT-bin/apache-hive-{version}-SNAPSHOT-bin$lsLICENSENOTICEREADME.txtRELEASE_NOTES.txtbin/(alltheshellscripts)lib/(requiredjarfiles)conf/(configurationfiles)examples/(sampleinputandqueryfiles)hcatalog/(hcataloginstallation)scripts/(upgradescriptsforhive-metastore)

编译hive 基于hadoop 0.20

$cdhive$antcleanpackage$cdbuild/dist#lsLICENSENOTICEREADME.txtRELEASE_NOTES.txtbin/(alltheshellscripts)lib/(requiredjarfiles)conf/(configurationfiles)examples/(sampleinputandqueryfiles)hcatalog/(hcataloginstallation)scripts/(upgradescriptsforhive-metastore)

运行hive

Hive运行依赖于hadoop,在运行hadoop之前必需先配置好hadoopHome。

export HADOOP_HOME=hadoop-install-dir

在hdfs上为hive创建\tmp目录和/user/hive/warehouse(akahive.metastore.warehouse.dir) 目录,然后你才可以运行hive。

在运行hive之前设置HiveHome。

$ export HIVE_HOME=hive-install-dir

在命令行窗口启动hive

$ $HIVE_HOME/bin/hive

若执行成功,将看到类似内容如图所示

hive是sql还是java语言

hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行

hive和java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于hive和Java、hive和java的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-02,除非注明,否则均为首码项目网原创文章,转载请注明出处。