-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_with_ssl.py
258 lines (214 loc) · 8.61 KB
/
client_with_ssl.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import logging
import random
import os
import hazelcast
from hazelcast.core import HazelcastJsonValue
from hazelcast.discovery import HazelcastCloudDiscovery
"""
This is boilerplate application that configures client to connect Hazelcast Cloud cluster.
See: https://docs.hazelcast.com/cloud/python-client
"""
def city(country: str, name: str, population: int) -> HazelcastJsonValue:
return HazelcastJsonValue({
"country": country,
"city": name,
"population": population,
})
def country(code: str, name: str) -> HazelcastJsonValue:
return HazelcastJsonValue({
"isoCode": code,
"country": name,
})
def map_example(client: hazelcast.HazelcastClient):
"""
This example shows how to work with Hazelcast maps.
"""
cities = client.get_map("cities").blocking()
cities.put("1", city("United Kingdom", "London", 9_540_576))
cities.put("2", city("United Kingdom", "Manchester", 2_770_434))
cities.put("3", city("United States", "New York", 19_223_191))
cities.put("4", city("United States", "Los Angeles", 3_985_520))
cities.put("5", city("Turkey", "Ankara", 5_309_690))
cities.put("6", city("Turkey", "Istanbul", 15_636_243))
cities.put("7", city("Brazil", "Sao Paulo", 22_429_800))
cities.put("8", city("Brazil", "Rio de Janeiro", 13_634_274))
map_size = cities.size()
print(f"'cities' map now contains {map_size} entries.")
print("-"*20)
def sql_example(client: hazelcast.HazelcastClient):
sql_service = client.sql
create_mapping_for_capitals(sql_service)
clear_capitals(sql_service)
populate_capitals(sql_service)
select_all_capitals(sql_service)
select_capital_names(sql_service)
def create_mapping_for_capitals(sql_service: hazelcast.client.SqlService):
print("Creating a mapping...")
# See: https://docs.hazelcast.com/hazelcast/5.1/sql/mapping-to-maps
mapping_query = '''
CREATE OR REPLACE MAPPING capitals
TYPE IMap
OPTIONS (
'keyFormat' = 'varchar',
'valueFormat' = 'varchar'
)
'''
sql_service.execute(mapping_query).result()
print("-" * 20)
def clear_capitals(sql_service: hazelcast.client.SqlService):
print("Deleting data via SQL...")
sql_service.execute("DELETE FROM capitals").result()
print("The data has been deleted successfully.")
print("-" * 20)
def populate_capitals(sql_service: hazelcast.client.SqlService):
print("Inserting data via SQL...")
insert_query = '''
INSERT INTO capitals VALUES
('Australia','Canberra'),
('Croatia','Zagreb'),
('Czech Republic','Prague'),
('England','London'),
('Turkey','Ankara'),
('United States','Washington, DC');
'''
sql_service.execute(insert_query).result()
print("-" * 20)
def select_all_capitals(sql_service: hazelcast.client.SqlService):
print("Retrieving all the data via SQL...")
result = sql_service.execute("SELECT * FROM capitals").result()
for row in result:
country = row.get_object_with_index(0)
city = row.get_object_with_index(1)
print(f"{country} - {city}")
print("-" * 20)
def select_capital_names(sql_service: hazelcast.client.SqlService):
print("Retrieving the capital name via SQL...")
result = sql_service.execute("SELECT __key, this FROM capitals WHERE __key = ?", "United States").result()
for row in result:
country = row.get_object("__key")
city = row.get_object("__key")
print(f"Country name: {country}; Capital name: {city}")
print("-" * 20)
def json_serialization_example(client):
sql_service = client.sql
create_mapping_for_countries(sql_service)
populate_countries_map(client)
select_all_countries(sql_service)
create_mapping_for_cities(sql_service)
populate_cities(client)
select_cities_by_country(sql_service, "AU")
select_countries_and_cities(sql_service)
def create_mapping_for_countries(sql_service: hazelcast.client.SqlService):
# see: https://docs.hazelcast.com/hazelcast/5.1/sql/mapping-to-maps#json-objects
print("Creating mapping for countries...")
mapping_query = """
CREATE OR REPLACE MAPPING country(
__key VARCHAR,
isoCode VARCHAR,
country VARCHAR
)
TYPE IMap OPTIONS(
'keyFormat' = 'varchar',
'valueFormat' = 'json-flat'
);
"""
sql_service.execute(mapping_query).result()
print("Mapping for countries has been created.")
print("-" * 20)
def populate_countries_map(client: hazelcast.HazelcastClient):
# see: https://docs.hazelcast.com/hazelcast/5.1/data-structures/creating-a-map#writing-json-to-a-map
print("Populating 'countries' map with JSON values...")
countries = client.get_map("country").blocking()
countries.put("AU", country("AU", "Australia"))
countries.put("EN", country("EN", "England"))
countries.put("US", country("US", "United States"))
countries.put("CZ", country("CZ", "Czech Republic"))
print("The 'countries' map has been populated.")
print("-" * 20)
def select_all_countries(sql_service: hazelcast.client.SqlService):
sql = "SELECT c.country from country c"
print(f"Select all countries with sql = {sql}", sql)
result = sql_service.execute(sql).result()
for row in result:
print(f"country = {row['country']}")
print("-" * 20)
def create_mapping_for_cities(sql_service: hazelcast.client.SqlService):
# see: https://docs.hazelcast.com/hazelcast/5.1/sql/mapping-to-maps#json-objects
print("Creating mapping for cities...")
mapping_sql = """
CREATE OR REPLACE MAPPING city(
__key INT,
country VARCHAR,
city VARCHAR,
population BIGINT
)
TYPE IMap OPTIONS (
'keyFormat' = 'int',
'valueFormat' = 'json-flat'
);
"""
sql_service.execute(mapping_sql).result()
print("Mapping for cities has been created.")
print("-" * 20)
def populate_cities(client: hazelcast.HazelcastClient):
# see: https://docs.hazelcast.com/hazelcast/5.1/data-structures/creating-a-map#writing-json-to-a-map
print("Populating 'city' map with JSON values...")
cities = client.get_map("city").blocking()
cities.put(1, city("AU", "Canberra", 467_194))
cities.put(2, city("CZ", "Prague", 1_318_085))
cities.put(3, city("EN", "London", 9_540_576))
cities.put(4, city("US", "Washington, DC", 7_887_965))
print("The 'city' map has been populated.")
print("-"*20)
def select_cities_by_country(sql_service: hazelcast.client.SqlService, country: str):
sql = "SELECT city, population FROM city where country=?"
print("-" * 20)
print(f"Select city and population with sql = {sql}")
result = sql_service.execute(sql, country).result()
for row in result:
print(f"city = {row['city']}, population = {row['population']}")
print("-" * 20)
def select_countries_and_cities(sql_service: hazelcast.client.SqlService):
sql = """
SELECT c.isoCode, c.country, t.city, t.population
FROM country c
JOIN city t
ON c.isoCode = t.country;
"""
print("Select country and city data in query that joins tables")
print("%4s | %15s | %20s | %15s |" % ("iso", "country", "city", "population"))
result = sql_service.execute(sql).result()
for row in result:
print("%4s | %15s | %20s | %15s |" %
(row["isoCode"], row["country"], row["city"], row["population"]))
print("-" * 20)
def nonstop_map_example(client: hazelcast.HazelcastClient):
print("Now the map named 'map' will be filled with random entries.")
map = client.get_map("map").blocking()
iteration_counter = 0
while True:
random_key = random.randint(0, 99_999)
map.put(f"key-{random_key}", f"value-{random_key}")
map.get(f"key-{random.randint(0, 99_999)}")
iteration_counter += 1
if iteration_counter == 10:
iteration_counter = 0
print(f"Current map size: {map.size()}")
logging.basicConfig(level=logging.INFO)
HazelcastCloudDiscovery._CLOUD_URL_BASE = "YOUR_DISCOVERY_URL"
client = hazelcast.HazelcastClient(
cluster_name="YOUR_CLUSTER_NAME",
cloud_discovery_token="YOUR_CLUSTER_DISCOVERY_TOKEN",
statistics_enabled=True,
ssl_enabled=True,
ssl_cafile=os.path.abspath("ca.pem"),
ssl_certfile=os.path.abspath("cert.pem"),
ssl_keyfile=os.path.abspath("key.pem"),
ssl_password="YOUR_SSL_PASSWORD",
)
print("Connection Successful!")
map_example(client)
# sql_example(client)
# json_serialization_example(client)
# nonstop_map_example(client)
client.shutdown()