forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapDExample.py
More file actions
106 lines (87 loc) · 3.55 KB
/
MapDExample.py
File metadata and controls
106 lines (87 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
from thrift.protocol import TBinaryProtocol
from thrift.protocol import TJSONProtocol
from thrift.transport import TSocket
from thrift.transport import THttpClient
from thrift.transport import TTransport
from mapd import MapD
from mapd import ttypes
import time
'''
Contact support@mapd.com with any questions
Python 2.x instructions (with fix for recursive data structs)
pip install redbaron
pip install thrift
thrift -gen py mapd.thrift
mv gen-py/mapd/ttypes.py gen-py/mapd/ttypes-backup.py
python fix_recursive_structs.py gen-py/mapd/ttypes-backup.py gen-py/mapd/ttypes.py
export PYTHONPATH=/path/to/mapd2-1.x-gen-py:$PYTHONPATH
Python 3.x instructions (manual build of latest Thrift)
sudo apt-get install build-essential
git clone https://git-wip-us.apache.org/repos/asf/thrift.git
cd thrift/lib/py
pip install .
thrift -gen py mapd.thrift
export PYTHONPATH=/path/to/mapd2-1.x-gen-py:$PYTHONPATH
Example:
python MapDExample.py
Connection samples:
HTTP client - get_client('http://test.mapd.com:9091', portno, True)
Binary protocol - get_client('locahost', 9091, False)
'''
def get_client(host_or_uri, port, http):
if http:
transport = THttpClient.THttpClient(host_or_uri)
protocol = TJSONProtocol.TJSONProtocol(transport)
else:
socket = TSocket.TSocket(host_or_uri, port)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = MapD.Client(protocol)
transport.open()
return client
def main():
db_name = 'mapd'
user_name = 'mapd'
passwd = 'HyperInteractive'
hostname = 'test.mapd.com'
portno = 9091
client = get_client(hostname, portno, False)
session = client.connect(user_name, passwd, db_name)
print 'Connection complete'
query = 'select a,b from table1 limit 25;'
print 'Query is : ' + query
# always use True for is columnar
results = client.sql_execute(session, query, True, None, -1)
dates = ['TIME', 'TIMESTAMP', 'DATE']
if results.row_set.is_columnar == True:
number_of_rows = list(range(0, len(results.row_set.columns[0].nulls)))
number_of_columns = list(range(0, len(results.row_set.row_desc)))
for n in number_of_rows:
for i in number_of_columns:
column_type = ttypes.TDatumType._VALUES_TO_NAMES[
results.row_set.row_desc[i].col_type.type]
column_name = results.row_set.row_desc[i].col_name
column_array = results.row_set.row_desc[i].col_type.is_array
if not column_array:
if column_type in ['SMALLINT', 'INT', 'BIGINT', 'TIME', 'TIMESTAMP', 'DATE', 'BOOL']:
column_value = results.row_set.columns[i].data.int_col[n]
if column_type in ['FLOAT', 'DECIMAL', 'DOUBLE']:
column_value = results.row_set.columns[i].data.real_col[n]
if column_type in ['STR']:
column_value = results.row_set.columns[i].data.str_col[n]
else:
column_value = results.row_set.columns[i].data.arr_col[n].data.str_col
print(column_name)
if column_type in dates:
print(time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(column_value)))
else:
print(column_value)
else:
print('Please use columns not rows in query execution')
client.disconnect(session)
quit()
client.disconnect(session)
if __name__ == '__main__':
main()