Skip to main content
Viki_Nodejs
Silver ⭐️⭐️
September 7, 2024
Question

Couldn't establish Hive DB session in nodejs

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

Viki_Nodejs
Silver ⭐️⭐️
September 8, 2024

Adding message Tags to reach better view to get answer

Translate
VidyaSargur
Community Manager
Community Manager
September 9, 2024

@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.

Translate
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.
smruti
Gold ⭐️⭐️⭐️
Gold ⭐️⭐️⭐️
September 9, 2024

@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 => {

 

Translate
Viki_Nodejs
Silver ⭐️⭐️
September 10, 2024

Hi Smruti,

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

Viki_Nodejs_0-1725949707191.png

 

Translate
smruti
Gold ⭐️⭐️⭐️
Gold ⭐️⭐️⭐️
September 10, 2024

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'

Translate
sravansrk
Silver ⭐️
September 25, 2024

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

Translate
Vicky_node
Silver ⭐️
December 16, 2024

Hi sravansrk,

Still, the same issue persists.

Translate
Shelton
Gold ⭐️⭐️⭐️
December 17, 2024

@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

Translate