From deef7a2ac58b54b58a8510d3d45c42671f0ae2dd Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 14 Jan 2026 12:52:10 +0300 Subject: [PATCH 1/4] Asyncio compact serialization example --- .../asyncio/compact_serialization_example.py | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 examples/asyncio/compact_serialization_example.py diff --git a/examples/asyncio/compact_serialization_example.py b/examples/asyncio/compact_serialization_example.py new file mode 100644 index 0000000000..24a732ba9a --- /dev/null +++ b/examples/asyncio/compact_serialization_example.py @@ -0,0 +1,88 @@ +import asyncio + +from hazelcast.asyncio import HazelcastClient +from hazelcast.serialization.api import ( + CompactSerializer, + CompactWriter, + CompactReader, +) + + +class Address: + def __init__(self, city: str, street: str): + self.city = city + self.street = street + + def __repr__(self): + return f"Address(city='{self.city}', street='{self.street}')" + + +class Employee: + def __init__(self, name: str, age: int, address: Address): + self.name = name + self.age = age + self.address = address + + def __repr__(self): + return f"Employee(name='{self.name}', age={self.age}, address={self.address})" + + +class AddressSerializer(CompactSerializer[Address]): + def read(self, reader: CompactReader): + city = reader.read_string("city") + street = reader.read_string("street") + return Address(city, street) + + def write(self, writer: CompactWriter, obj: Address): + writer.write_string("city", obj.city) + writer.write_string("street", obj.street) + + def get_type_name(self): + return "Address" + + def get_class(self): + return Address + + +class EmployeeSerializer(CompactSerializer[Employee]): + def read(self, reader: CompactReader): + name = reader.read_string("name") + age = reader.read_int32("age") + address = reader.read_compact("address") + return Employee(name, age, address) + + def write(self, writer: CompactWriter, obj: Employee): + writer.write_string("name", obj.name) + writer.write_int32("age", obj.age) + writer.write_compact("address", obj.address) + + def get_type_name(self): + return "Employee" + + def get_class(self): + return Employee + + +async def amain(): + client = await HazelcastClient.create_and_start( + compact_serializers=[AddressSerializer(), EmployeeSerializer()] + ) + employees = await client.get_map("employees") + await employees.set( + 0, + Employee( + name="John Doe", + age=42, + address=Address( + city="Cambridge", + street="3487 Cedar Lane", + ), + ), + ) + employee = await employees.get(0) + print(employee) + await client.shutdown() + + +if __name__ == "__main__": + asyncio.run(amain()) From b7eeeb9d7c834d2d676ed57375ef096b062ee55e Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 14 Jan 2026 12:56:41 +0300 Subject: [PATCH 2/4] Asyncio map example --- examples/asyncio/map_basic_example.py | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 examples/asyncio/map_basic_example.py diff --git a/examples/asyncio/map_basic_example.py b/examples/asyncio/map_basic_example.py new file mode 100644 index 0000000000..fefbfe5b49 --- /dev/null +++ b/examples/asyncio/map_basic_example.py @@ -0,0 +1,32 @@ +import asyncio + +from hazelcast.asyncio import HazelcastClient + + +async def amain(): + client = await HazelcastClient.create_and_start() + my_map = await client.get_map("my-map") + + # Fill the map + await my_map.put("1", "Tokyo") + await my_map.put("2", "Paris") + await my_map.put("3", "Istanbul") + + entry = await my_map.get("3") + print("Entry with key 3:", entry) + + map_size = await my_map.size() + print("Map size:", map_size) + + # Print the map + print("\nIterating over the map: \n") + + entries = await my_map.entry_set() + for key, value in entries: + print("%s -> %s" % (key, value)) + + await client.shutdown() + + +if __name__ == '__main__': + asyncio.run(amain()) From f0f51793c6d25ce0ed34c7ff3d7129a1d69c2ac1 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 14 Jan 2026 13:08:32 +0300 Subject: [PATCH 3/4] Added asyncio vector collection example --- examples/asyncio/vector_collection.py | 105 ++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 examples/asyncio/vector_collection.py diff --git a/examples/asyncio/vector_collection.py b/examples/asyncio/vector_collection.py new file mode 100644 index 0000000000..cce43da9cb --- /dev/null +++ b/examples/asyncio/vector_collection.py @@ -0,0 +1,105 @@ +import asyncio +import logging + +from hazelcast.vector import Type, Vector, Metric, IndexConfig + +logging.basicConfig(level=logging.DEBUG) + +from hazelcast.asyncio import HazelcastClient +from hazelcast.config import Config +from hazelcast.proxy.vector_collection import Document + + +async def amain(): + # Connect to the cluster + cfg = Config() + cfg.cluster_members = ["localhost:5701"] + client = await HazelcastClient.create_and_start(cfg) + + vc_name = "my-vector-collection" + + # Create the VectorCollection + indexes = [ + IndexConfig(name="default-vector", metric=Metric.COSINE, dimension=2), + ] + await client.create_vector_collection_config(vc_name, indexes=indexes) + + # Use the VectorCollection + vc = await client.get_vector_collection(vc_name) + doc1 = Document( + "value1", + [ + Vector("default-vector", Type.DENSE, [0.1, 0.5]), + ], + ) + + # Add the Document + key = "key-1" + await vc.set(key, doc1) + + # Add another Document + doc2 = Document( + "value2", + [ + Vector("default-vector", Type.DENSE, [0.5, 0.7]), + ], + ) + + # Add the Document + key = "key-2" + await vc.set(key, doc2) + + # Optimize collection + await vc.optimize() + + # Search for a vector + results = await vc.search_near_vector( + Vector("default-vector", Type.DENSE, [0.2, 0.3]), + limit=2, + include_value=True, + include_vectors=True, + ) + for i, result in enumerate(results): + print( + f"{i+1}.", + "Key:", + result.key, + "Value:", + result.value, + "Score:", + result.score, + "Vector:", + result.vectors, + ) + + print("size:", await vc.size()) + + # Delete all entries + await vc.clear() + print("cleared collection") + print("size:", await vc.size()) + + # Search for a vector + results = await vc.search_near_vector( + Vector("default-vector", Type.DENSE, [0.2, 0.3]), + limit=2, + include_value=True, + include_vectors=True, + ) + for i, result in enumerate(results): + print( + f"{i+1}.", + "Key:", + result.key, + "Value:", + result.value, + "Score:", + result.score, + "Vector:", + result.vectors, + ) + + +if __name__ == "__main__": + asyncio.run(amain()) + From f487fbb7744908d402b9afc031cac6d7bcab56d3 Mon Sep 17 00:00:00 2001 From: Yuce Tekol Date: Wed, 14 Jan 2026 13:19:43 +0300 Subject: [PATCH 4/4] Black --- examples/asyncio/map_basic_example.py | 2 +- examples/asyncio/vector_collection.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/asyncio/map_basic_example.py b/examples/asyncio/map_basic_example.py index fefbfe5b49..71b4b98dbd 100644 --- a/examples/asyncio/map_basic_example.py +++ b/examples/asyncio/map_basic_example.py @@ -28,5 +28,5 @@ async def amain(): await client.shutdown() -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(amain()) diff --git a/examples/asyncio/vector_collection.py b/examples/asyncio/vector_collection.py index cce43da9cb..a95bdb2f89 100644 --- a/examples/asyncio/vector_collection.py +++ b/examples/asyncio/vector_collection.py @@ -102,4 +102,3 @@ async def amain(): if __name__ == "__main__": asyncio.run(amain()) -