繁体   English   中英

Hadoop Hive - 如何“添加jar”以与Hive JDBC客户端一起使用?

[英]Hadoop Hive - How can I 'add jar' for use with the Hive JDBC client?

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

现在,我添加了一个Hive用户定义函数(UDF)。 它在CLI中运行良好...我甚至通过.hiverc文件自动加载jar和相关函数。 但是,我无法使用hive jdbc驱动程序来实现此功能。 我认为它也会使用.hiverc文件(默认情况下,位于/ usr / lib / hive / bin /),但它似乎不起作用。 我也尝试通过'add jar'SQL命令添加它作为第一件事,但无论我把jar文件放在哪里,我在hive.log中都遇到错误,无法找到该文件。

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

提前致谢。

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

    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驱动程序连接到Hive。 我将jar压缩到集群的主节点上,这也是安装Hive的地方,然后在我的add jar命令中使用文件的绝对路径(在主节点上)。 我通过JDBC驱动程序发出add jar命令,就像任何其他HQL命令一样。

我认为JDBC驱动程序使用Thrift,这意味着JAR可能需要在Thrift服务器(您在conn字符串中连接到的hive服务器)和hive类路径中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM