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()) diff --git a/examples/asyncio/map_basic_example.py b/examples/asyncio/map_basic_example.py new file mode 100644 index 0000000000..71b4b98dbd --- /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()) diff --git a/examples/asyncio/vector_collection.py b/examples/asyncio/vector_collection.py new file mode 100644 index 0000000000..a95bdb2f89 --- /dev/null +++ b/examples/asyncio/vector_collection.py @@ -0,0 +1,104 @@ +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())