forked from gridgain/python-thin-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_sql.py
More file actions
120 lines (106 loc) · 4.69 KB
/
Copy pathasync_sql.py
File metadata and controls
120 lines (106 loc) · 4.69 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#
# Copyright 2022 GridGain Systems, Inc. and Contributors.
#
# Licensed under the GridGain Community Edition License (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import asyncio
from helpers.sql_helper import TableNames, Query, TestData
from pygridgain import AioClient
async def main():
# establish connection
client = AioClient()
async with client.connect('127.0.0.1', 10800):
# create tables
for query in [
Query.COUNTRY_CREATE_TABLE,
Query.CITY_CREATE_TABLE,
Query.LANGUAGE_CREATE_TABLE,
]:
await client.sql(query)
# create indices
for query in [Query.CITY_CREATE_INDEX, Query.LANGUAGE_CREATE_INDEX]:
await client.sql(query)
# load data concurrently.
await asyncio.gather(*[
client.sql(Query.COUNTRY_INSERT, query_args=row) for row in TestData.COUNTRY
])
await asyncio.gather(*[
client.sql(Query.CITY_INSERT, query_args=row) for row in TestData.CITY
])
await asyncio.gather(*[
client.sql(Query.LANGUAGE_INSERT, query_args=row) for row in TestData.LANGUAGE
])
# 10 most populated cities (with pagination)
async with client.sql('SELECT name, population FROM City ORDER BY population DESC LIMIT 10') as cursor:
print('Most 10 populated cities:')
async for row in cursor:
print(row)
# Most 10 populated cities:
# ['Mumbai (Bombay)', 10500000]
# ['Shanghai', 9696300]
# ['New York', 8008278]
# ['Peking', 7472000]
# ['Delhi', 7206704]
# ['Chongqing', 6351600]
# ['Tianjin', 5286800]
# ['Calcutta [Kolkata]', 4399819]
# ['Wuhan', 4344600]
# ['Harbin', 4289800]
print('-' * 20)
# 10 most populated cities in 3 countries (with pagination and header row)
most_populated_in_3_countries = '''
SELECT country.name as country_name, city.name as city_name, MAX(city.population) AS max_pop FROM country
JOIN city ON city.countrycode = country.code
WHERE country.code IN ('USA','IND','CHN')
GROUP BY country.name, city.name ORDER BY max_pop DESC LIMIT 10
'''
async with client.sql(most_populated_in_3_countries, include_field_names=True) as cursor:
print('Most 10 populated cities in USA, India and China:')
table_str_pattern = '{:15}\t| {:20}\t| {}'
print(table_str_pattern.format(*await cursor.__anext__()))
print('*' * 50)
async for row in cursor:
print(table_str_pattern.format(*row))
# Most 10 populated cities in USA, India and China:
# COUNTRY_NAME | CITY_NAME | MAX_POP
# **************************************************
# India | Mumbai (Bombay) | 10500000
# China | Shanghai | 9696300
# United States | New York | 8008278
# China | Peking | 7472000
# India | Delhi | 7206704
# China | Chongqing | 6351600
# China | Tianjin | 5286800
# India | Calcutta [Kolkata] | 4399819
# China | Wuhan | 4344600
# China | Harbin | 4289800
print('-' * 20)
# show city info
async with client.sql('SELECT * FROM City WHERE id = ?', query_args=[3802], include_field_names=True) as cursor:
field_names = await cursor.__anext__()
field_data = await cursor.__anext__()
print('City info:')
for field_name, field_value in zip(field_names * len(field_data), field_data):
print('{}: {}'.format(field_name, field_value))
# City info:
# ID: 3802
# NAME: Detroit
# COUNTRYCODE: USA
# DISTRICT: Michigan
# POPULATION: 951270
# clean up concurrently.
await asyncio.gather(*[
client.sql(Query.DROP_TABLE.format(table_name.value)) for table_name in TableNames
])
if __name__ == '__main__':
asyncio.run(main())