Created
May 21, 2014 11:31
-
-
Save wyukawa/a6e525c7dc5c04ed6927 to your computer and use it in GitHub Desktop.
python+hiveserver2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import os | |
| sys.path.append('/opt/cloudera/parcels/CDH/share/hue/apps/beeswax/gen-py') | |
| from TCLIService import TCLIService | |
| from TCLIService.ttypes import TOpenSessionReq, TGetTablesReq, TFetchResultsReq,\ | |
| TStatusCode, TGetResultSetMetadataReq, TGetColumnsReq, TType,\ | |
| TExecuteStatementReq, TGetOperationStatusReq, TFetchOrientation,\ | |
| TCloseSessionReq, TGetSchemasReq, TCancelOperationReq | |
| from thrift import Thrift | |
| from thrift.transport import TSocket | |
| from thrift.transport import TTransport | |
| from thrift.protocol import TBinaryProtocol | |
| def get_value(colValue): | |
| if colValue.boolVal is not None: | |
| return colValue.boolVal.value | |
| elif colValue.byteVal is not None: | |
| return colValue.byteVal.value | |
| elif colValue.i16Val is not None: | |
| return colValue.i16Val.value | |
| elif colValue.i32Val is not None: | |
| return colValue.i32Val.value | |
| elif colValue.i64Val is not None: | |
| return colValue.i64Val.value | |
| elif colValue.doubleVal is not None: | |
| return colValue.doubleVal.value | |
| elif colValue.stringVal is not None: | |
| return colValue.stringVal.value | |
| socket = TSocket.TSocket('localhost', 10000) | |
| transport = TTransport.TBufferedTransport(socket) | |
| protocol = TBinaryProtocol.TBinaryProtocol(transport) | |
| client = TCLIService.Client(protocol) | |
| transport.open() | |
| kwargs = {'username': '...', 'configuration': {}} | |
| kwargs['configuration'].update({'hive.server2.proxy.user': '...'}) | |
| request=TOpenSessionReq(**kwargs) | |
| res = client.OpenSession(request) | |
| session = res.sessionHandle | |
| hql = "select * from ..." | |
| query = TExecuteStatementReq(session, statement=hql, confOverlay={}) | |
| res = client.ExecuteStatement(query) | |
| fetch_req = TFetchResultsReq(operationHandle=res.operationHandle, orientation=TFetchOrientation.FETCH_NEXT, maxRows=100) | |
| resultsRes = client.FetchResults(fetch_req) | |
| rows = [] | |
| for row in resultsRes.results.rows: | |
| rowData= [] | |
| for i, col in enumerate(row.colVals): | |
| rowData.append(get_value(col)) | |
| rows.append(rowData) | |
| if len(resultsRes.results.rows) == 0: | |
| break | |
| for row in rows: | |
| print row |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment