Error in connecting node.js application to hive

2019-02-18 17:57发布

问题:

I have used node-hive and thrift for connecting my node js application to hive, but none of them works. Are there any other node modules for getting connected to hive ?

回答1:

As node node-thrift-hive and node-hive both module are abandoned, not commit still 4 to 5 year old, I have same issue and I am using Node 0.12 and below is my solutions.

Step -1: You have to setup Hiveserver2 , my hive version is 1.2 and hadoop version 2.7

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

If you don't want to follow Step-1 then upgrade your node js to 4.5 > and use jshs2 npm

In my case I don't want to upgrade my node version so I try to connect via JDBC drivers JDBC npm

Step-2: Use Below code to connect hive and fetch the data

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);
            });

        });
     });    
});