Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
709 changes: 372 additions & 337 deletions README.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions benchmarks/simple_map_nearcache_bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time

import hazelcast
from hazelcast.config import NearCacheConfig, IN_MEMORY_FORMAT
from hazelcast.config import NearCacheConfig, InMemoryFormat
from hazelcast import six
from hazelcast.six.moves import range

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

near_cache_config = NearCacheConfig(MAP_NAME)
near_cache_config.in_memory_format = IN_MEMORY_FORMAT.OBJECT
near_cache_config.in_memory_format = InMemoryFormat.OBJECT
config.add_near_cache_config(near_cache_config)

try:
Expand Down
32 changes: 13 additions & 19 deletions examples/cloud-discovery/hazelcast_cloud_discovery_example.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import hazelcast

config = hazelcast.ClientConfig()

# Set up cluster name for authentication
config.cluster_name.name = "YOUR_CLUSTER_NAME"

# Enable Hazelcast.Cloud configuration and set the token of your cluster.
config.network.cloud.enabled = True
config.network.cloud.discovery_token = "YOUR_CLUSTER_DISCOVERY_TOKEN"

# If you have enabled encryption for your cluster, also configure TLS/SSL for the client.
# Otherwise, skip this step.
config.network.ssl.enabled = True
config.network.ssl.cafile = "/path/to/ca.pem"
config.network.ssl.certfile = "/path/to/cert.pem"
config.network.ssl.keyfile = "/path/to/key.pem"
config.network.ssl.password = "YOUR_KEY_STORE_PASSWORD"

# Start a new Hazelcast client with this configuration.
client = hazelcast.HazelcastClient(config)
client = hazelcast.HazelcastClient(
# Set up cluster name for authentication
cluster_name="YOUR_CLUSTER_NAME",
# Set the token of your cloud cluster
cloud_discovery_token="YOUR_CLUSTER_DISCOVERY_TOKEN",
# If you have enabled encryption for your cluster, also configure TLS/SSL for the client.
# Otherwise, skip options below.
ssl_enabled=True,
ssl_cafile="/path/to/ca.pem",
ssl_certfile="/path/to/cert.pem",
ssl_keyfile="/path/to/key.pem",
ssl_password="YOUR_KEY_STORE_PASSWORD"
)

my_map = client.get_map("map-on-the-cloud").blocking()
my_map.put("key", "value")
Expand Down
19 changes: 7 additions & 12 deletions examples/flake-id-generator/flake_id_generator_example.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import hazelcast

config = hazelcast.ClientConfig()
flake_id_generator_config = hazelcast.FlakeIdGeneratorConfig()

# Default value is 600000 (10 minutes)
flake_id_generator_config.prefetch_validity_in_millis = 30000

# Default value is 100
flake_id_generator_config.prefetch_count = 50

config.add_flake_id_generator_config(flake_id_generator_config)
client = hazelcast.HazelcastClient(config)
client = hazelcast.HazelcastClient(flake_id_generators={
"id-generator": {
"prefetch_count": 50,
"prefetch_validity": 30,
}
})

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

for _ in range(100):
print("Id: {}".format(generator.new_id()))
print("Id:", generator.new_id())

client.shutdown()
21 changes: 10 additions & 11 deletions examples/learning-basics/1-configure_client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import hazelcast

# Create configuration for the client
config = hazelcast.ClientConfig()
print("Cluster name: {}".format(config.cluster_name))

# Add member's host:port to the configuration.
# For each member on your Hazelcast cluster, you should add its host:port pair to the configuration.
config.network.addresses.append("127.0.0.1:5701")
config.network.addresses.append("127.0.0.1:5702")

# Create a client using the configuration above
client = hazelcast.HazelcastClient(config)
# Create a client using the configuration below
client = hazelcast.HazelcastClient(
# Add member's host:port to the configuration.
# For each member on your Hazelcast cluster, you should add its host:port pair to the configuration.
# If the port is not specified, by default 5701, 5702 and 5703 will be tried.
cluster_members=[
"127.0.0.1:5701",
"127.0.0.1:5702",
]
)

# Disconnect the client and shutdown
client.shutdown()
14 changes: 8 additions & 6 deletions examples/learning-basics/2-create_a_map.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import hazelcast

# Connect
config = hazelcast.ClientConfig()
config.network.addresses.append("127.0.0.1:5701")
client = hazelcast.HazelcastClient(config)
client = hazelcast.HazelcastClient(
cluster_members=[
"127.0.0.1:5701"
]
)

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

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

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

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

# Shutdown the client
client.shutdown()
18 changes: 10 additions & 8 deletions examples/learning-basics/3-read_from_a_map.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import hazelcast

# Connect
config = hazelcast.ClientConfig()
config.network.addresses.append("127.0.0.1:5701")
client = hazelcast.HazelcastClient(config)
client = hazelcast.HazelcastClient(
cluster_members=[
"127.0.0.1:5701"
]
)

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

# Get the keys of the map
keys = greetings_map.key_set().result()
# Get the entry set of the map
entry_set = greetings_map.entry_set()

# Print key-value pairs
for key in keys:
print("{} -> {}".format(key, greetings_map.get(key).result()))
for key, value in entry_set:
print("%s -> %s" % (key, value))

# Shutdown the client
client.shutdown()
10 changes: 5 additions & 5 deletions examples/map/map_async_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@

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


def put_callback(future):
print("Map put: {}".format(future.result()))
print("Map put:", future.result())


def contains_callback(future):
print("Map contains: {}".format(future.result()))
print("Map contains:", future.result())


client = hazelcast.HazelcastClient()

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

print("Map size: {}".format(my_map.size().result()))
print("Map size: %d" % my_map.size().result())

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

key = random.random()
print("Random key: {}".format(key))
print("Random key:", key)
my_map.contains_key(key).add_done_callback(contains_callback)

time.sleep(3)
Expand Down
6 changes: 3 additions & 3 deletions examples/map/map_basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
my_map.put("2", "Paris")
my_map.put("3", "Istanbul")

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

print("Map size: {}".format(my_map.size().result()))
print("Map size:", my_map.size().result())

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

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

client.shutdown()
13 changes: 6 additions & 7 deletions examples/map/map_blocking_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hazelcast
import random
import time


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

print("Map size: {}".format(my_map.size()))
print("Map size:", my_map.size())

random_key = random.random()
my_map.put(random_key, "value")
print("Map contains {}: {}".format(random_key, my_map.contains_key(random_key)))
print("Map size: {}".format(my_map.size()))
print("Map contains %s: %s" % (random_key, my_map.contains_key(random_key)))
print("Map size:", my_map.size())

my_map.remove(random_key)
print("Map contains {}: {}".format(random_key, my_map.contains_key(random_key)))
print("Map size: {}".format(my_map.size()))
print("Map contains %s: %s" % (random_key, my_map.contains_key(random_key)))
print("Map size:", my_map.size())

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

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

client.shutdown()
8 changes: 3 additions & 5 deletions examples/map/map_listener_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@


def entry_added(event):
print("Entry added with key: {}, value: {}".format(event.key, event.value))
print("Entry added with key: %s, value: %s" % (event.key, event.value))


def entry_removed(event):
print("Entry removed with key: {}".format(event.key))
print("Entry removed with key:", event.key)


def entry_updated(event):
print("Entry updated with key: {}, old value: {}, new value: {}".format(event.key,
event.old_value,
event.value))
print("Entry updated with key: %s, old value: %s, new value: %s" % (event.key, event.old_value, event.value))


client = hazelcast.HazelcastClient()
Expand Down
16 changes: 8 additions & 8 deletions examples/map/map_portable_query_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,28 @@ def __eq__(self, other):
return isinstance(other, Employee) and self.name == other.name and self.age == other.age


config = hazelcast.ClientConfig()

config.serialization.portable_factories[Employee.FACTORY_ID] = {Employee.CLASS_ID: Employee}

client = hazelcast.HazelcastClient(config)
client = hazelcast.HazelcastClient(portable_factories={
Employee.FACTORY_ID: {
Employee.CLASS_ID: Employee
}
})

my_map = client.get_map("employee-map")

my_map.put(0, Employee("Jack", 28))
my_map.put(1, Employee("Jane", 29))
my_map.put(2, Employee("Joe", 30))

print("Map Size: {}".format(my_map.size().result()))
print("Map Size:", my_map.size().result())

predicate = sql("age <= 29")


def values_callback(f):
result_set = f.result()
print("Query Result Size: {}".format(len(result_set)))
print("Query Result Size:", len(result_set))
for value in result_set:
print("value: {}".format(value))
print("Value:", value)


my_map.values(predicate).add_done_callback(values_callback)
Expand Down
36 changes: 21 additions & 15 deletions examples/map/map_portable_versioning_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,23 @@ def __eq__(self, other):


# Let's now configure 3 clients with 3 different versions of Employee.
config = hazelcast.ClientConfig()
config.serialization.portable_factories[Employee.FACTORY_ID] = {Employee.CLASS_ID: Employee}
client = hazelcast.HazelcastClient(config)

config2 = hazelcast.ClientConfig()
config2.serialization.portable_factories[Employee2.FACTORY_ID] = {Employee2.CLASS_ID: Employee2}
client2 = hazelcast.HazelcastClient(config2)

config3 = hazelcast.ClientConfig()
config3.serialization.portable_factories[Employee3.FACTORY_ID] = {Employee3.CLASS_ID: Employee3}
client3 = hazelcast.HazelcastClient(config3)
client = hazelcast.HazelcastClient(portable_factories={
Employee.FACTORY_ID: {
Employee.CLASS_ID: Employee
}
})

client2 = hazelcast.HazelcastClient(portable_factories={
Employee2.FACTORY_ID: {
Employee2.CLASS_ID: Employee2
}
})

client3 = hazelcast.HazelcastClient(portable_factories={
Employee3.FACTORY_ID: {
Employee3.CLASS_ID: Employee3
}
})

# Assume that a member joins a cluster with a newer version of a class.
# If you modified the class by adding a new field, the new member's put operations include that
Expand All @@ -147,7 +153,7 @@ def __eq__(self, other):
my_map.put(0, Employee("Jack", 28))
my_map2.put(1, Employee2("Jane", 29, "Josh"))

print('Map Size: {}'.format(my_map.size()))
print('Map Size: %s' % my_map.size())

# If this new member tries to get an object that was put from the older members, it
# gets null for the newly added field.
Expand All @@ -161,21 +167,21 @@ def __eq__(self, other):
my_map3 = client3.get_map("employee-map").blocking()
my_map3.put(2, Employee3("Joe", "30", "Mary"))

print('Map Size: {}'.format(my_map.size()))
print('Map Size: %s' % my_map.size())

# As clients with incompatible versions of the class try to access each other, a HazelcastSerializationError
# is raised (caused by a TypeError).
try:
# Client that has class with int type age field tries to read Employee3 object with String age field.
print(my_map.get(2))
except HazelcastSerializationError as ex:
print("Failed due to: {}".format(ex))
print("Failed due to: %s" % ex)

try:
# Client that has class with String type age field tries to read Employee object with int age field.
print(my_map3.get(0))
except HazelcastSerializationError as ex:
print("Failed due to: {}".format(ex))
print("Failed due to: %s" % ex)

client.shutdown()
client2.shutdown()
Expand Down
Loading