将node.js应用程序连接到配置单元时出错

时间:2016-07-30 05:45:41

标签: node.js configuration hive node-modules

我已经使用node-hive和thrift将我的节点js应用程序连接到hive,但它们都不起作用。是否还有其他节点模块可以连接到配置单元?

2 个答案:

答案 0 :(得分:3)

由于节点node-thrift-hivenode-hive两个模块都被放弃了,而且还没有提交4到5岁,我有同样的问题,我使用的是Node 0.12,以下是我的解决方案。

步骤-1:你必须设置Hiveserver2,我的hive版本是1.2和hadoop版本2.7

https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2

如果您不想按照步骤1进行操作,请将节点js升级到4.5>并使用jshs2 npm

在我的情况下,我不想升级我的节点版本,所以我尝试通过JDBC驱动程序连接JDBC npm

步骤2:使用下面的代码连接配置单元并获取数据

var JDBC = require('jdbc');
var jinst = require('jdbc/lib/jinst');

// isJvmCreated will be true after the first java call.  When this happens, the
// options and classpath cannot be adjusted.
if (!jinst.isJvmCreated()) {
  // Add all java options required by your project here.  You get one chance to
  // setup the options before the first java call.
  jinst.addOption("-Xrs");
  // Add all jar files required by your project here.  You get one chance to
  // setup the classpath before the first java call.
  jinst.setupClasspath(['./drivers/hsqldb.jar',
                        './drivers/derby.jar',
                        './drivers/derbyclient.jar',
                        './drivers/derbytools.jar',
                        './lib/drivers/hive-jdbc-1.2.1.jar',
                        './lib/drivers/hive-exec-1.2.1.jar',
                        './lib/drivers/hive-common-1.2.1.jar',
                        './lib/drivers/hive-metastore-1.2.1.jar',
                        './lib/drivers/hive-service-1.2.1.jar',
                        './lib/drivers/httpclient-4.3.jar',
                        './lib/drivers/httpcore-4.3.jar',
                        './lib/drivers/libthrift-0.9.1.jar',
                        './lib/drivers/libfb303-0.9.0.jar',
                        './lib/drivers/hadoop-common-2.7.1.jar',
                        './lib/drivers/slf4j-api-1.7.21.jar',
                        './lib/drivers/org-apache-commons-logging.jar'
                        ]);
}

var config = {
  url: 'jdbc:hive2://127.0.0.1:10000',
  user : 'demo',
  password: '',
  minpoolsize: 2,
  maxpoolsize: 3
};

var testpool = null;
var testconn = null;
var hsqldb = new JDBC(config);

hsqldb.initialize(function(err) {
  if (err) {
    console.log(err);
  }
});

hsqldb.reserve(function(err, connObj) {
     console.log("Using connection: " + connObj.uuid);
     var conn = connObj.conn;
     conn.createStatement(function(err, statement) {
        statement.executeQuery("select * from test1 limit 1",function(err,resultSet){
            //console.log(resultSet);
            resultSet.toObjArray(function(err, results) {
                console.log(results);
            });

        });
     });    
}); 

答案 1 :(得分:1)

如果无法使用Java,并且必须通过SASL身份验证连接到Hive服务器,则可以使用npm lib hive-driver

以下是用法示例:

const hive = require('hive-driver');
const { TCLIService, TCLIService_types } = hive.thrift;
const client = new hive.HiveClient(
    TCLIService,
    TCLIService_types
);
const utils = new hive.HiveUtils(
    TCLIService_types
);

client.connect(
    {
        host: 'localhost',
        port: 10000
    },
    new hive.connections.TcpConnection(),
    new hive.auth.PlainTcpAuthentication({
        username: 'user name',
        password: 'password'
    })
).then(async client => {
    const session = await client.openSession({
        client_protocol: TCLIService_types.TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V10
    });
    const operation = await session.executeStatement(
        'CREATE TABLE pokes ( foo INT, bar INT )'
    );
    await utils.waitUntilReady(operation, false, () => {});

    await operation.close();
    await session.close();
});