我知道这个 jshs2 已经过时了,但它适用于我的用例。我npm run test:msg
做了测试,它工作正常:
{
"auth": "NOSASL",
"host": "127.0.0.1",
"port": 10000,
"timeout": 10000,
"username": "hive",
"password": "hive",
"hiveType": 1,
"hiveVer": "0.13.1",
"cdhVer": "5.3.0",
"thriftVer": "0.9.2",
"maxRows": 5120,
"nullStr": "NULL",
"i64ToString": true
}
但是我很难获得使用 await/async 工作的更现代的方式:检索 jshs2 的示例
我的代码:
const {
Configuration,
HiveConnection,
IDLContainer,
} = require('jshs2');
const options = {
"auth": "NOSASL",
"host": "127.0.0.1",
"port": 10000,
"timeout": 10000,
"username": "hive",
"password": "hive",
"hiveType": 1,
"hiveVer": "0.13.1",
"cdhVer": "5.3.0",
"thriftVer": "0.9.2",
"maxRows": 5120,
"nullStr": "NULL",
"i64ToString": true
};
const hiveConfig = new Configuration(options);
const idl = new IDLContainer();
console.log(options);
async function main() {
console.log('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');
const res = await cursor.execute('show databases');
// console.log(res);
if (res.hasResultSet) {
const fetchResult = await cursor.fetchBlock();
fetchResult.rows.forEach((row) => {
console.log(row);
});
} else {
console.log('Error: res...');
console.log(res);
}
cursor.close();
connection.close();
console.log('close');
}
main().then(() => {
console.log('Finished.');
});
我得到以下错误:
[root@demo testjshs2]# node test1.js
{ auth: 'NOSASL',
host: '127.0.0.1',
port: 10000,
hiveType: 1,
hiveVer: '0.13.1',
cdhVer: '5.3.0',
thriftVer: '0.9.2' }
main
(node:50708) UnhandledPromiseRejectionWarning: Error: Expected state FINISHED, but found RUNNING
-- Error caused from HiveServer2
*org.apache.hive.service.cli.HiveSQLException:Expected state FINISHED, but found RUNNING:27:26
org.apache.hive.service.cli.operation.Operation:assertState:Operation.java:199
org.apache.hive.service.cli.operation.SQLOperation:getNextRowSet:SQLOperation.java:445
org.apache.hive.service.cli.operation.OperationManager:getOperationNextRowSet:OperationManager.java:294
org.apache.hive.service.cli.session.HiveSessionImpl:fetchResults:HiveSessionImpl.java:769
sun.reflect.NativeMethodAccessorImpl:invoke0:NativeMethodAccessorImpl.java:-2
sun.reflect.NativeMethodAccessorImpl:invoke:NativeMethodAccessorImpl.java:57
sun.reflect.DelegatingMethodAccessorImpl:invoke:DelegatingMethodAccessorImpl.java:43
java.lang.reflect.Method:invoke:Method.java:606
org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:78
org.apache.hive.service.cli.session.HiveSessionProxy:access$000:HiveSessionProxy.java:36
org.apache.hive.service.cli.session.HiveSessionProxy$1:run:HiveSessionProxy.java:63
java.security.AccessController:doPrivileged:AccessController.java:-2
javax.security.auth.Subject:doAs:Subject.java:415
org.apache.hadoop.security.UserGroupInformation:doAs:UserGroupInformation.java:1917
org.apache.hive.service.cli.session.HiveSessionProxy:invoke:HiveSessionProxy.java:59
com.sun.proxy.$Proxy26:fetchResults::-1
org.apache.hive.service.cli.CLIService:fetchResults:CLIService.java:462
org.apache.hive.service.cli.thrift.ThriftCLIService:FetchResults:ThriftCLIService.java:694
org.apache.hive.service.cli.thrift.TCLIService$Processor$FetchResults:getResult:TCLIService.java:1553
org.apache.hive.service.cli.thrift.TCLIService$Processor$FetchResults:getResult:TCLIService.java:1538
org.apache.thrift.ProcessFunction:process:ProcessFunction.java:39
org.apache.thrift.TBaseProcessor:process:TBaseProcessor.java:39
org.apache.hive.service.auth.TSetIpAddressProcessor:process:TSetIpAddressProcessor.java:56
org.apache.thrift.server.TThreadPoolServer$WorkerProcess:run:TThreadPoolServer.java:286
java.util.concurrent.ThreadPoolExecutor:runWorker:ThreadPoolExecutor.java:1145
java.util.concurrent.ThreadPoolExecutor$Worker:run:ThreadPoolExecutor.java:615
java.lang.Thread:run:Thread.java:745
at Client.FetchResults (/root/testjshs2/node_modules/jshs2/lib/HiveCursor.js:270:18)
at client._reqs.(anonymous function) (/root/testjshs2/node_modules/thrift/lib/nodejs/lib/thrift/connection.js:151:13)
at exports.Client.TCLIServiceClient.recv_FetchResults (/root/testjshs2/node_modules/jshs2/idl/Thrift_0.9.2_Hive_0.13.1_CDH_5.3.0/TCLIService.js:2934:12)
at /root/testjshs2/node_modules/thrift/lib/nodejs/lib/thrift/connection.js:157:41
at Socket.<anonymous> (/root/testjshs2/node_modules/thrift/lib/nodejs/lib/thrift/buffered_transport.js:48:5)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
(node:50708) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:50708) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
^C
这是我的 Hiveserver2 日志:
2018-04-10 16:29:30,648 INFO [HiveServer2-Handler-Pool: Thread-52]: session.SessionState (SessionState.java:createPath(703)) - Created local directory: /tmp/e8fe98e5-39ac-438b-8a33-36cf0da898dd_resources
2018-04-10 16:29:30,653 INFO [HiveServer2-Handler-Pool: Thread-52]: session.SessionState (SessionState.java:createPath(703)) - Created HDFS directory: /tmp/hive/anonymous/e8fe98e5-39ac-438b-8a33-36cf0da898dd
2018-04-10 16:29:30,656 INFO [HiveServer2-Handler-Pool: Thread-52]: session.SessionState (SessionState.java:createPath(703)) - Created local directory: /tmp/hive/e8fe98e5-39ac-438b-8a33-36cf0da898dd
2018-04-10 16:29:30,666 INFO [HiveServer2-Handler-Pool: Thread-52]: session.SessionState (SessionState.java:createPath(703)) - Created HDFS directory: /tmp/hive/anonymous/e8fe98e5-39ac-438b-8a33-36cf0da898dd/_tmp_space.db
2018-04-10 16:29:30,666 INFO [HiveServer2-Handler-Pool: Thread-52]: session.SessionState (SessionState.java:start(587)) - No Tez session required at this point. hive.execution.engine=mr.
2018-04-10 16:29:30,674 INFO [HiveServer2-Handler-Pool: Thread-52]: ql.Driver (Driver.java:compile(497)) - Compiling command(queryId=hive_20180410162929_d52c8fad-b5ef-4797-a168-5be155876c09): show databases
2018-04-10 16:29:30,741 INFO [HiveServer2-Handler-Pool: Thread-52]: ql.Driver (Driver.java:compile(570)) - Semantic Analysis Completed
2018-04-10 16:29:30,742 INFO [HiveServer2-Handler-Pool: Thread-52]: ql.Driver (Driver.java:getSchema(303)) - Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:database_name, type:string, comment:from deserializer)], properties:null)
2018-04-10 16:29:30,743 INFO [HiveServer2-Handler-Pool: Thread-52]: ql.Driver (Driver.java:compile(690)) - Completed compiling command(queryId=hive_20180410162929_d52c8fad-b5ef-4797-a168-5be155876c09); Time taken: 0.069 seconds
2018-04-10 16:29:30,746 WARN [HiveServer2-Handler-Pool: Thread-52]: security.UserGroupInformation (UserGroupInformation.java:doAs(1920)) - PriviledgedActionException as:anonymous (auth:SIMPLE) cause:org.apache.hive.service.cli.HiveSQLException: Expected state FINISHED, but found RUNNING
2018-04-10 16:29:30,746 WARN [HiveServer2-Handler-Pool: Thread-52]: thrift.ThriftCLIService (ThriftCLIService.java:FetchResults(703)) - Error fetching results:
org.apache.hive.service.cli.HiveSQLException: Expected state FINISHED, but found RUNNING
at org.apache.hive.service.cli.operation.Operation.assertState(Operation.java:199)
at org.apache.hive.service.cli.operation.SQLOperation.getNextRowSet(SQLOperation.java:445)
at org.apache.hive.service.cli.operation.OperationManager.getOperationNextRowSet(OperationManager.java:294)
at org.apache.hive.service.cli.session.HiveSessionImpl.fetchResults(HiveSessionImpl.java:769)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hive.service.cli.session.HiveSessionProxy.invoke(HiveSessionProxy.java:78)
at org.apache.hive.service.cli.session.HiveSessionProxy.access$000(HiveSessionProxy.java:36)
at org.apache.hive.service.cli.session.HiveSessionProxy$1.run(HiveSessionProxy.java:63)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1917)
at org.apache.hive.service.cli.session.HiveSessionProxy.invoke(HiveSessionProxy.java:59)
at com.sun.proxy.$Proxy26.fetchResults(Unknown Source)
at org.apache.hive.service.cli.CLIService.fetchResults(CLIService.java:462)
at org.apache.hive.service.cli.thrift.ThriftCLIService.FetchResults(ThriftCLIService.java:694)
at org.apache.hive.service.cli.thrift.TCLIService$Processor$FetchResults.getResult(TCLIService.java:1553)
at org.apache.hive.service.cli.thrift.TCLIService$Processor$FetchResults.getResult(TCLIService.java:1538)
at org.apache.thrift.ProcessFunction.process(ProcessFunction.java:39)
at org.apache.thrift.TBaseProcessor.process(TBaseProcessor.java:39)
at org.apache.hive.service.auth.TSetIpAddressProcessor.process(TSetIpAddressProcessor.java:56)
at org.apache.thrift.server.TThreadPoolServer$WorkerProcess.run(TThreadPoolServer.java:286)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
2018-04-10 16:29:30,749 INFO [HiveServer2-Background-Pool: Thread-63]: ql.Driver (Driver.java:checkConcurrency(223)) - Concurrency mode is disabled, not creating a lock manager
2018-04-10 16:29:30,749 INFO [HiveServer2-Background-Pool: Thread-63]: ql.Driver (Driver.java:execute(1656)) - Executing command(queryId=hive_20180410162929_d52c8fad-b5ef-4797-a168-5be155876c09): show databases
2018-04-10 16:29:30,750 INFO [HiveServer2-Background-Pool: Thread-63]: ql.Driver (Driver.java:launchTask(2050)) - Starting task [Stage-0:DDL] in serial mode
2018-04-10 16:29:30,762 INFO [HiveServer2-Background-Pool: Thread-63]: ql.Driver (Driver.java:execute(1958)) - Completed executing command(queryId=hive_20180410162929_d52c8fad-b5ef-4797-a168-5be155876c09); Time taken: 0.013 seconds
2018-04-10 16:29:30,762 INFO [HiveServer2-Background-Pool: Thread-63]: ql.Driver (SessionState.java:printInfo(1087)) - OK
知道吗?
更新 1:
原来这东西跑得太快了:
const res = await cursor.execute('show databases');
将导致if (res.hasResultSet)
错误
如果我在中间放一些东西,例如for (i=0;i<100000;i++) {console.log();}
延迟一点,它工作正常。
但我认为异步/等待应该处理这些事情???
试试这个
var jshs2 = require('jshs2');
var HS2Util = jshs2.HS2Util;
await idl.initialize(hiveConfig);
const connection = await new HiveConnection(hiveConfig, idl);
let serviceType = idl.ServiceType;
let cursor = await connection.connect();
const ress = await cursor.execute(queryString);
for (let i = 0, len = 1000; i < len; i += 1) {
const status = await cursor.getOperationStatus();
const log = await cursor.getLog();
console.log('Status: ' + HS2Util.getState(serviceType, status));
console.log('Log: ' + log);
if (HS2Util.isFinish(cursor, status)) {
break;
}
await HS2Util.sleep(5000); }