Support Questions

Find answers, ask questions, and share your expertise

Couldn't establish Hive DB session in nodejs

avatar
New Contributor

Hi Team,

Good Day..

I'm beginner in Node.JS trying to connect Hive DB  using

hive-driver npm from Node.JS code.

I couldn't establish the db connection.

Any help would be much appreciated...

Note: Our Hive DB has ssl connection, jks trust store password, HTTP protocol , httppath=cliservice

 

NODE.JS  SAMPLE CODE:

client.connect(
{
host: 'jdbc:hive2://abc.com:2181/dev_db;ssl=true;sslTrustStore=/etc/hive-jks/hivetrust.jks;trustStorePassword=pro@!23;transportMode=http;httpPath=cliservice;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2',
port: 2181
},
new hive.connections.HttpConnection(),
new hive.auth.PlainHttpAuthentication({
username: 'username',
password: 'password'
})
).then(async client => {

console.log(client);

const session = await client.openSession({
client_protocol: TCLIService_types.TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V10
});

}).catch(error => {
console.log(error);
});

 

when I console.log client It's printing but I tried to open client.open session I've been facing issues 

Kindly check the host url string  which I've given right or not.

Could you please help on this issue.

PFB Error.

 

Viki_Nodejs_0-1725729303792.png

Viki_Nodejs_2-1725729422452.png

Thank you..

 

 

 

8 REPLIES 8

avatar
New Contributor

Adding message Tags to reach better view to get answer

avatar
Community Manager

@Viki_Nodejs, Welcome to our community! To help you get the best possible answer, I have tagged our Hive experts @asish @smruti  who may be able to assist you further.

Please feel free to provide any additional information or details about your query. We hope that you will find a satisfactory solution to your question.



Regards,

Vidya Sargur,
Community Manager


Was your question answered? Make sure to mark the answer as the accepted solution.
If you find a reply useful, say thanks by clicking on the thumbs up button.
Learn more about the Cloudera Community:

avatar
Master Collaborator

@Viki_Nodejs It seems like the hostname resolution fails. The url you have shared is rather jdbc compliant. 

Could you give the following a try?

client.connect(
  {
    host: 'abc.com',
    port: 2181
  },
  new hive.connections.HttpConnection({
    transportMode: 'http', 
    httpPath: 'cliservice',
    serviceDiscoveryMode: 'zooKeeper', 
    zooKeeperNamespace: 'hiveserver2'
  }),
  new hive.auth.PlainHttpAuthentication({
    username: 'username',
    password: 'password'
  }),
  {
    ssl: true,
    sslTrustStore: '/etc/hive-jks/hivetrust.jks',
    trustStorePassword: 'pro@!23'
  }
).then(async client => {

 

avatar
New Contributor

Hi Smruti,

Thanks for your reply. I've used the above snippet. PFB Error

Viki_Nodejs_0-1725949707191.png

 

avatar
Master Collaborator

Do we get this error after it gets connected? Could there be a socket timeout error set at client side? I am not aware if the hive driver has a default timeout value. 
Instead of ZooKeeper, could you try connecting to HS2 directly.
e.g.
host: "hs2 node fqdn"
port: 10001

Remove
serviceDiscoveryMode: 'zooKeeper',
zooKeeperNamespace: 'hiveserver2'

avatar
New Contributor

@Viki_Nodejs 
Hi Viki, were you able to resolve the issue. I am also encountering same issue

avatar
New Contributor

Hi sravansrk,

Still, the same issue persists.

avatar
Master Mentor

@Viki_Nodejs 
if you haven't resolved this issue could you try the below steps and revert.

1. Install the Required NPM Packages

Use the hive-driver package for Node.js, which supports HiveServer2 over HTTP/HTTPS.

Spoiler
npm install hive-driver

2. Prerequisites

Ensure you have:

  • HiveServer2 URL: Includes the hostname and port.
  • SSL Configuration: Paths to your .jks trust store and its password.
  • Hive httppath: Set to cliservice.
  • Authentication details (if required): Username/password or Kerberos configuration.

3. Configure the Connection

Here's an example of how to set up the connection using the hive-driver:

 

Spoiler

const { HiveClient, TCLIServiceTypes } = require('hive-driver');

async function connectToHive() {
const client = new HiveClient(TCLIServiceTypes);

// Configure the Hive connection
const connection = client.connect({
host: '<HIVE_SERVER_HOSTNAME>', // e.g., hive.example.com
port: 10001, // HiveServer2 port, typically 10001 for HTTPS
options: {
path: '/cliservice', // HTTP path to HiveServer2
ssl: true, // Enable SSL
sslOptions: {
rejectUnauthorized: true, // Ensure certificates are verified
ca: '<path/to/truststore.pem>' // Convert your JKS truststore to PEM format
},
// Authentication
username: '<YOUR_USERNAME>',
password: '<YOUR_PASSWORD>',
// You can add session configurations here
}
});

try {
// Open the connection
await connection.openSession();
console.log('Connected to Hive');

// Example query
const result = await connection.executeStatement('SELECT * FROM your_table LIMIT 10');
console.log(result);

// Close the session
await connection.closeSession();
} catch (error) {
console.error('Error connecting to Hive:', error);
} finally {
// Ensure the connection is closed
await connection.close();
}
}

connectToHive();

4. Key Point to Note !!!!!!!!!

  1. SSL Truststore [Very Important]

    • Hive uses .jks files for its truststore, but hive-driver requires a .pem file for SSL. Convert your .jks file to .pem using the following commands:
      Spoiler
      keytool -importkeystore -srckeystore truststore.jks -destkeystore truststore.p12 -deststoretype PKCS12 openssl pkcs12 -in truststore.p12 -out truststore.pem -nokeys
      I also saw an EAI_FAIL  error in the screenshot this is related to not being able to resolve the DNS.

      Hope this helps