「java写入hive」java写入helloworld时错误

博主:adminadmin 2022-12-19 15:18:09 41

本篇文章给大家谈谈java写入hive,以及java写入helloworld时错误对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求助java怎么把HBase数据读出并导入Hive中

方法有两种: 1.一种是通过Java,直接读取数据,然后在插入hive中 2.第二种是整合hbase,hive。 整合后,再创建一个表,然后将数据导入新建的hive表中

「java写入hive」java写入helloworld时错误

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命令或HiveQL

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。

IDEA下写hive的udf(踩坑教程)

配置maven的过程网上有很多这里就不写了。

UDF

用户自定义函数(user defined function)–针对单条记录。

创建函数流程

1、自定义一个Java类

2、继承UDF类

3、重写evaluate方法 (必须重写这个方法)

4、打成jar包

6、在hive执行add jar方法

7、在hive执行创建模板函数

8、hql中使用

打包后的文件在你的项目的target当中,上传那个几kb的original-int2inetaddressUDF-1.0-SNAPSHOT.jar那个文件,上传到服务器上随便一个目录

add jar 你的文件路径 /original-int2inetaddressUDF-1.0-SNAPSHOT.jar;

create temporary function myudf as "UDFDemo.int2inetaddress";

select myudf(XXX) from xxxx

可以参考这里的视频:

java连接Hive的几种方式

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

关于java写入hive和java写入helloworld时错误的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

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