Skip to content

Commit 566260e

Browse files
committed
Change configuration to keyword arguments
This PR replaces Java-style `ClientConfig` class with keyword-arguments. It also updates README, code samples and tests with the new configuration style. The full list of supported keyword arguments can be seen on the `hazelcast.config._Config` class. Apart from them, - `enum`s are changed to `EnumName.ITEM_NAME` format. - IDEs now show the possible values of `enum`s. - All config options now assume time unit in seconds. - `cloud_config.enabled` is removed. Cloud discovery is enabled once the discovery token is set. - Extra tests are added for `_IndexConfig` and `_IndexUtil`. - Load balancers are moved to the `util`, similar to the Java client.
1 parent e070e70 commit 566260e

File tree

130 files changed

+3737
-2836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+3737
-2836
lines changed

README.md

Lines changed: 373 additions & 337 deletions
Large diffs are not rendered by default.

benchmarks/simple_map_nearcache_bench.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66

77
import hazelcast
8-
from hazelcast.config import NearCacheConfig, IN_MEMORY_FORMAT
8+
from hazelcast.config import NearCacheConfig, InMemoryFormat
99
from hazelcast import six
1010
from hazelcast.six.moves import range
1111

@@ -32,7 +32,7 @@ def init():
3232
config.network.addresses.append("127.0.0.1")
3333

3434
near_cache_config = NearCacheConfig(MAP_NAME)
35-
near_cache_config.in_memory_format = IN_MEMORY_FORMAT.OBJECT
35+
near_cache_config.in_memory_format = InMemoryFormat.OBJECT
3636
config.add_near_cache_config(near_cache_config)
3737

3838
try:

examples/cloud-discovery/hazelcast_cloud_discovery_example.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
import hazelcast
22

3-
config = hazelcast.ClientConfig()
4-
5-
# Set up cluster name for authentication
6-
config.cluster_name.name = "YOUR_CLUSTER_NAME"
7-
8-
# Enable Hazelcast.Cloud configuration and set the token of your cluster.
9-
config.network.cloud.enabled = True
10-
config.network.cloud.discovery_token = "YOUR_CLUSTER_DISCOVERY_TOKEN"
11-
12-
# If you have enabled encryption for your cluster, also configure TLS/SSL for the client.
13-
# Otherwise, skip this step.
14-
config.network.ssl.enabled = True
15-
config.network.ssl.cafile = "/path/to/ca.pem"
16-
config.network.ssl.certfile = "/path/to/cert.pem"
17-
config.network.ssl.keyfile = "/path/to/key.pem"
18-
config.network.ssl.password = "YOUR_KEY_STORE_PASSWORD"
19-
20-
# Start a new Hazelcast client with this configuration.
21-
client = hazelcast.HazelcastClient(config)
3+
client = hazelcast.HazelcastClient(
4+
# Set up cluster name for authentication
5+
cluster_name="YOUR_CLUSTER_NAME",
6+
# Set the token of your cloud cluster
7+
cloud_discovery_token="YOUR_CLUSTER_DISCOVERY_TOKEN",
8+
# If you have enabled encryption for your cluster, also configure TLS/SSL for the client.
9+
# Otherwise, skip options below.
10+
ssl_enabled=True,
11+
ssl_cafile="/path/to/ca.pem",
12+
ssl_certfile="/path/to/cert.pem",
13+
ssl_keyfile="/path/to/key.pem",
14+
ssl_password="YOUR_KEY_STORE_PASSWORD"
15+
)
2216

2317
my_map = client.get_map("map-on-the-cloud").blocking()
2418
my_map.put("key", "value")
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import hazelcast
22

3-
config = hazelcast.ClientConfig()
4-
flake_id_generator_config = hazelcast.FlakeIdGeneratorConfig()
5-
6-
# Default value is 600000 (10 minutes)
7-
flake_id_generator_config.prefetch_validity_in_millis = 30000
8-
9-
# Default value is 100
10-
flake_id_generator_config.prefetch_count = 50
11-
12-
config.add_flake_id_generator_config(flake_id_generator_config)
13-
client = hazelcast.HazelcastClient(config)
3+
client = hazelcast.HazelcastClient(flake_id_generators={
4+
"id-generator": {
5+
"prefetch_count": 50,
6+
"prefetch_validity": 30,
7+
}
8+
})
149

1510
generator = client.get_flake_id_generator("id-generator").blocking()
1611

1712
for _ in range(100):
18-
print("Id: {}".format(generator.new_id()))
13+
print("Id:", generator.new_id())
1914

2015
client.shutdown()
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import hazelcast
22

3-
# Create configuration for the client
4-
config = hazelcast.ClientConfig()
5-
print("Cluster name: {}".format(config.cluster_name))
6-
7-
# Add member's host:port to the configuration.
8-
# For each member on your Hazelcast cluster, you should add its host:port pair to the configuration.
9-
config.network.addresses.append("127.0.0.1:5701")
10-
config.network.addresses.append("127.0.0.1:5702")
11-
12-
# Create a client using the configuration above
13-
client = hazelcast.HazelcastClient(config)
3+
# Create a client using the configuration below
4+
client = hazelcast.HazelcastClient(
5+
# Add member's host:port to the configuration.
6+
# For each member on your Hazelcast cluster, you should add its host:port pair to the configuration.
7+
# If the port is not specified, by default 5701, 5702 and 5703 will be tried.
8+
cluster_members=[
9+
"127.0.0.1:5701",
10+
"127.0.0.1:5702",
11+
]
12+
)
1413

1514
# Disconnect the client and shutdown
1615
client.shutdown()
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import hazelcast
22

33
# Connect
4-
config = hazelcast.ClientConfig()
5-
config.network.addresses.append("127.0.0.1:5701")
6-
client = hazelcast.HazelcastClient(config)
4+
client = hazelcast.HazelcastClient(
5+
cluster_members=[
6+
"127.0.0.1:5701"
7+
]
8+
)
79

810
# Get a map that is stored on the server side. We can access it from the client
9-
greetings_map = client.get_map("greetings-map")
11+
greetings_map = client.get_map("greetings-map").blocking()
1012

1113
# Map is empty on the first run. It will be non-empty if Hazelcast has data on this map
12-
print("Map: {}, Size: {}".format(greetings_map.name, greetings_map.size().result()))
14+
print("Size before:", greetings_map.size())
1315

1416
# Write data to map. If there is a data with the same key already, it will be overwritten
1517
greetings_map.put("English", "hello world")
@@ -19,7 +21,7 @@
1921
greetings_map.put("French", "bonjour monde")
2022

2123
# 5 data is added to the map. There should be at least 5 data on the server side
22-
print("Map: {}, Size: {}".format(greetings_map.name, greetings_map.size().result()))
24+
print("Size after:", greetings_map.size())
2325

2426
# Shutdown the client
2527
client.shutdown()
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import hazelcast
22

33
# Connect
4-
config = hazelcast.ClientConfig()
5-
config.network.addresses.append("127.0.0.1:5701")
6-
client = hazelcast.HazelcastClient(config)
4+
client = hazelcast.HazelcastClient(
5+
cluster_members=[
6+
"127.0.0.1:5701"
7+
]
8+
)
79

810
# We can access maps on the server from the client. Let's access the greetings map that we created already
9-
greetings_map = client.get_map("greetings-map")
11+
greetings_map = client.get_map("greetings-map").blocking()
1012

11-
# Get the keys of the map
12-
keys = greetings_map.key_set().result()
13+
# Get the entry set of the map
14+
entry_set = greetings_map.entry_set()
1315

1416
# Print key-value pairs
15-
for key in keys:
16-
print("{} -> {}".format(key, greetings_map.get(key).result()))
17+
for key, value in entry_set:
18+
print("%s -> %s" % (key, value))
1719

1820
# Shutdown the client
1921
client.shutdown()

examples/map/map_async_example.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55

66
def fill_map(hz_map, count=10):
77
entries = {"key-" + str(i): "value-" + str(i) for i in range(count)}
8-
hz_map.put_all(entries)
8+
hz_map.put_all(entries).result()
99

1010

1111
def put_callback(future):
12-
print("Map put: {}".format(future.result()))
12+
print("Map put:", future.result())
1313

1414

1515
def contains_callback(future):
16-
print("Map contains: {}".format(future.result()))
16+
print("Map contains:", future.result())
1717

1818

1919
client = hazelcast.HazelcastClient()
2020

2121
my_map = client.get_map("async-map")
2222
fill_map(my_map)
2323

24-
print("Map size: {}".format(my_map.size().result()))
24+
print("Map size: %d" % my_map.size().result())
2525

2626
my_map.put("key", "async-value").add_done_callback(put_callback)
2727

2828
key = random.random()
29-
print("Random key: {}".format(key))
29+
print("Random key:", key)
3030
my_map.contains_key(key).add_done_callback(contains_callback)
3131

3232
time.sleep(3)

examples/map/map_basic_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
my_map.put("2", "Paris")
1010
my_map.put("3", "Istanbul")
1111

12-
print("Entry with key 3: {}".format(my_map.get("3").result()))
12+
print("Entry with key 3:", my_map.get("3").result())
1313

14-
print("Map size: {}".format(my_map.size().result()))
14+
print("Map size:", my_map.size().result())
1515

1616
# Print the map
1717
print("\nIterating over the map: \n")
1818

1919
entries = my_map.entry_set().result()
2020
for key, value in entries:
21-
print("{} -> {}".format(key, value))
21+
print("%s -> %s" % (key, value))
2222

2323
client.shutdown()
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import hazelcast
22
import random
3-
import time
43

54

65
def fill_map(hz_map, count=10):
@@ -13,20 +12,20 @@ def fill_map(hz_map, count=10):
1312
my_map = client.get_map("sync-map").blocking()
1413
fill_map(my_map)
1514

16-
print("Map size: {}".format(my_map.size()))
15+
print("Map size:", my_map.size())
1716

1817
random_key = random.random()
1918
my_map.put(random_key, "value")
20-
print("Map contains {}: {}".format(random_key, my_map.contains_key(random_key)))
21-
print("Map size: {}".format(my_map.size()))
19+
print("Map contains %s: %s" % (random_key, my_map.contains_key(random_key)))
20+
print("Map size:", my_map.size())
2221

2322
my_map.remove(random_key)
24-
print("Map contains {}: {}".format(random_key, my_map.contains_key(random_key)))
25-
print("Map size: {}".format(my_map.size()))
23+
print("Map contains %s: %s" % (random_key, my_map.contains_key(random_key)))
24+
print("Map size:", my_map.size())
2625

2726
print("\nIterate over the map\n")
2827

2928
for key, value in my_map.entry_set():
30-
print("Key: {} -> Value: {}".format(key, value))
29+
print("Key: %s -> Value: %s" % (key, value))
3130

3231
client.shutdown()

0 commit comments

Comments
 (0)