jdbc - Hadoop Hive - 如何将 'add jar' 用于 Hive JDBC 客户端?

标签 jdbc hadoop hive hdfs

所以,我让 hdfs 和 hive 一起工作。我还有用于 Hive 运行的 jdbc 驱动程序,以便我可以进行远程 jdbc 调用。

现在,我添加了一个 Hive 用户定义函数 (UDF)。它在 CLI 中运行良好……我什至通过 .hiverc 文件自动加载 jar 和相关函数。但是,我无法使用配置单元 jdbc 驱动程序让它工作。我以为它也会使用 .hiverc 文件(默认情况下,位于/usr/lib/hive/bin/),但它似乎不起作用。我还尝试通过“添加 jar”SQL 命令添加它作为第一件事,但无论我将 jar 文件放在哪里,我都会在 hive.log 中收到错误消息,指出找不到该文件。

有人知道怎么做吗?我正在使用 Cloudera Distribution (CDH3u2),它使用 Hive-0.7.1。

提前致谢。

最佳答案

根据 Hive 开发人员邮件列表,在当前的 Hive 版本 (0.9) 中没有针对此问题的解决方案。为了解决这个问题,我使用了一个连接工厂类,它在每次连接 session 启动时正确注册 jars 和函数。下面的代码非常有效:

    package com.rapidminer.operator.bigdata.runner.helpers;
import java.sql.*;

/** A Hive connection factory utility 
@author Marcelo Beckmann
*/
public class ConnectionFactory {

private static ConnectionFactory instance;

/** Basic attributes to make the connection*/
public String url = "jdbc:hive://localhost:10000/default";
public final String DRIVER = "org.apache.hadoop.hive.jdbc.HiveDriver";

public static ConnectionFactory getInstance(){
    if (instance==null)
        instance = new ConnectionFactory();
    return instance;
}
private ConnectionFactory()
{}
/**
 * Obtains a hive connection.
 * Warning! To use simultaneous connection from the Thrift server, you must change the
 * Hive metadata server from Derby to other database (MySQL for example).
 * @return
 * @throws Exception
 */
public Connection getConnection() throws Exception {

    Class.forName(DRIVER);

    Connection connection = DriverManager.getConnection(url,"","");

    runInitializationQueries(connection);
    return connection;
}

/**
 * Run initialization queries after the connection be obtained. This initialization was done in order
 * to workaround a known Hive bug (HIVE-657).
 * @throws SQLException
 */
private void runInitializationQueries(Connection connection) throws SQLException
{
    Statement stmt = null;
    try {
        //TODO Get the queries from a .hiverc file
        String[] args= new String[3];
        args[0]="add jar /home/hadoop-user/hive-0.9.0-bin/lib/hive-beckmann-functions.jar";  
        args[1]="create temporary function row_number as 'com.beckmann.hive.RowNumber'"; 
        args[2]="create temporary function sequence as 'com.beckmann.hive.Sequence'";
        for (String query:args)
        {
            stmt.execute(query);
        }
    }
    finally {
        if (stmt!=null)
            stmt.close();
    }

}
}

关于jdbc - Hadoop Hive - 如何将 'add jar' 用于 Hive JDBC 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8898908/

相关文章:

如果密码包含 % 符号,Java 无法连接到 MySQL

java - RDBMS:执行多项操作的最佳方式是什么

sql - HIVE ERROR : I am getting EOF error at 1, 对于第一个 LEFT OUTER JOIN 的 ON 子句之后的 WHERE 子句,对于配置单元中的以下代码

java - 奇怪的 JDBC 执行查询异常

java - Spring JDBC批量更新查询,无需准备语句

hadoop - 无法在配置单元中创建 Parquet 文件

sql - 您如何在 HiveQL 中获取“事件日期 > 当前日期 - 10 天)?

Python 连接到 Hive 使用 pyhs2 和 Kerberos 身份验证

oracle - 如何将大表从 oracle db 到 hdfs?

exception - 如何在 Oozie 中获取有关已终止作业的更具体的错误信息