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

vzgqcmou  于 2021-06-28  发布在  Hive
关注(0)|答案(2)|浏览(431)

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

szqfcxe2

szqfcxe21#

由于node-node-thrift-hive和node-hive这两个模块都被放弃了,还没有提交4到5年,我有同样的问题,我正在使用node 0.12和以下是我的解决方案。
第一步:你必须安装hiveserver2,我的配置单元版本是1.2,hadoop版本是2.7
https://cwiki.apache.org/confluence/display/hive/setting+up+hiveserver2
如果您不想遵循步骤1,那么请将节点js升级到4.5>并使用jshs2npm
在我的例子中,我不想升级我的节点版本,所以我尝试通过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);
            });

        });
     });    
});
gstyhher

gstyhher2#

如果不可能使用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();
});

相关问题