node.js - 如何使用node js连接hive服务器
问题描述
我正在尝试使用 jshs2 连接 Hive,但无法建立连接。使用这个 npm 连接 hive 是正确的方法吗?我的源代码如下:
const jshs2 = require('jshs2');
const options = {};
options.auth = 'NOSASL';
options.host = 'host name';
options.port = '10000';
options.username = 'user name';
options.password = 'password';
options.hiveType = 2;
const hiveConfig = new Configuration(options);
const idl = new IDLContainer();
async function main() {
await idl.initialize(hiveConfig);
const connection = await new HiveConnection(hiveConfig, idl);
const cursor = await connection.connect();
const res = await cursor.execute('SELECT * FROM orders LIMIT 10');
if (res.hasResultSet) {
const fetchResult = await cursor.fetchBlock();
fetchResult.rows.forEach((row) => {
console.log(row);
});
}
cursor.close();
connection.close();
}
main().then(() => {
console.log('Finished.');
});
解决方案
正如您在评论中提到的那样,您使用 SASL 身份验证,但jshs2的作者提到该库不支持 SASL 身份验证(链接)
您可以使用java jdbc或hive-driver在 nodejs 中使用 SASL 连接 Hive
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: 'host name',
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(
'SELECT * FROM orders LIMIT 10'
);
await utils.waitUntilReady(operation, false, () => {});
await utils.fetchAll(operation);
console.log(
utils.getResult(selectDataOperation).getValue()
);
await operation.close();
await session.close();
});
推荐阅读
- pyomo - 定义可变矩阵并在运行期间手动设置值
- julia - Julia 中的 NonlinearConstraintIndex 是什么?
- java - 如何在map-reduce的multipleinput中将虚拟变量设置为路径
- python - Pywinauto - 如何获取将动态变化的元素(进度条)的“值”属性
- reactjs - 如何将输入数据传递给反应中的另一个组件
- python - 在 pyqt5 Qprinter 中转换为日期格式
- android - 依次加载图像列表。扑
- firebase - 我如何编写另一个用户只能更新其他用户的一个变量的 Firestore 规则?
- javascript - TypeError:无法读取未定义 Discord.js javascript 的属性“添加”
- sql - 选择对两列求和并希望将结果用作选择条件的查询