def test_identity(self): attributes = self.map.project(identity()) self.assertCountEqual( [ HazelcastJsonValue('{"attr1": 4, "attr2": 5, "attr3": 6}'), HazelcastJsonValue('{"attr1": 1, "attr2": 2, "attr3": 3}'), ], [attribute.value for attribute in attributes], )
def test_querying_over_keys_with_hazelcast_json_value(self): json_value = HazelcastJsonValue({"a": 1}) json_value2 = HazelcastJsonValue({"a": 3}) self.map.put(json_value, 1) self.map.put(json_value2, 2) results = self.map.key_set(is_greater_than("__key.a", 2)) self.assertEqual(1, len(results)) self.assertEqual(json_value2.to_string(), results[0].to_string())
def test_querying_over_values_with_hazelcast_json_value(self): json_value = HazelcastJsonValue({"a": 1}) json_value2 = HazelcastJsonValue({"a": 3}) self.map.put(1, json_value) self.map.put(2, json_value2) results = self.map.values(is_greater_than("a", 2)) self.assertEqual(1, len(results)) self.assertEqual(json_value2.to_string(), results[0].to_string())
def test_querying_nested_attr_over_keys_with_hazelcast_json_value(self): json_value = HazelcastJsonValue({"a": 1, "b": {"c": "d"}}) json_value2 = HazelcastJsonValue({"a": 2, "b": {"c": "e"}}) self.map.put(json_value, 1) self.map.put(json_value2, 2) results = self.map.key_set(is_equal_to("__key.b.c", "d")) self.assertEqual(1, len(results)) self.assertEqual(json_value.to_string(), results[0].to_string())
def test_querying_nested_attr_over_values_with_hazelcast_json_value(self): json_value = HazelcastJsonValue({"a": 1, "b": {"c": "d"}}) json_value2 = HazelcastJsonValue({"a": 2, "b": {"c": "e"}}) self.map.put(1, json_value) self.map.put(2, json_value2) results = self.map.values(equal("b.c", "d")) self.assertEqual(1, len(results)) self.assertEqual(json_value.to_string(), results[0].to_string())
def test_hazelcast_json_value_construction_with_non_json_serializable_object(self): class A: def __init__(self): self.b = "c" with self.assertRaises(TypeError): HazelcastJsonValue(A())
def insertResult(self, name, result, key): """ request the next set of results :return: """ map = self.client.get_map(name) map.put(key=key, value=HazelcastJsonValue(json.dumps(result))) logging.debug('inserted object result {}'.format(key))
def test_storing_hazelcast_json_value_with_invalid_str(self): json_value = HazelcastJsonValue('{"a') self.map.put(0, json_value) self.assertEqual(json_value.to_string(), self.map.get(0).to_string())
def test_storing_hazelcast_json_value_as_value(self): json_value = HazelcastJsonValue(self.json_str) self.map.put(0, json_value) self.assertEqual(json_value.to_string(), self.map.get(0).to_string())
def test_hazelcast_json_value(self): self.validate(HazelcastJsonValue("{\"abc\": \"abc\", \"five\": 5}"))
def test_hazelcast_json_value(self): self.validate(HazelcastJsonValue('{"abc": "abc", "five": 5}'))
return 1 def get_class_id(self): return 1 client = hazelcast.HazelcastClient() students = client.get_map("students").blocking() # Populate the map with some random data. for i in range(42): students.set( i, HazelcastJsonValue({ "student_id": i, "age": random.randrange(8, 24), }), ) # Use the paging predicate with true predicate # to get all students with the page size of 10. # It also uses the custom comparator we have # written and sorts the values in ascending # order of age. paging_predicate = predicate.paging( predicate=predicate.true(), page_size=10, comparator=AgeComparator(), ) print(students.values(paging_predicate))
def test_hjv_from_server(self): self.assertTrue( self.set_on_server( 'new com.hazelcast.core.HazelcastJsonValue("{\\"a\\": 3}")')) self.assertEqual(HazelcastJsonValue({"a": 3}), self.map.get("key"))
import hazelcast from hazelcast.core import HazelcastJsonValue from hazelcast.serialization.predicate import and_, is_greater_than, sql client = hazelcast.HazelcastClient() employees_map = client.get_map("employees").blocking() alice = "{\"name\": \"Alice\", \"age\": 35}" andy = "{\"name\": \"Andy\", \"age\": 22}" bob = {"name": "Bob", "age": 37} # HazelcastJsonValue can be constructed from JSON strings employees_map.put(0, HazelcastJsonValue(alice)) employees_map.put(1, HazelcastJsonValue(andy)) # or from JSON serializable objects employees_map.put(2, HazelcastJsonValue(bob)) # Employees whose name starts with 'A' and age is greater than 30 predicate = and_(sql("name like A%"), is_greater_than("age", 30)) values = employees_map.values(predicate) for value in values: print(value.to_string()) # As JSON string print(value.loads()) # As Python object client.shutdown()
def decode(msg): msg.next_frame() value = StringCodec.decode(msg) CodecUtil.fast_forward_to_end_frame(msg) return HazelcastJsonValue(value)
import hazelcast from hazelcast.core import HazelcastJsonValue client = hazelcast.HazelcastClient() employees = client.get_map("employees").blocking() # Populate some data employees.put(0, HazelcastJsonValue('{"name": "Alice", "age": 32}')) employees.put(1, HazelcastJsonValue('{"name": "John", "age": 42}')) employees.put(2, HazelcastJsonValue('{"name": "Jake", "age": 18}')) # Create mapping for the employees map. This needs to be done only once per map. client.sql.execute(""" CREATE OR REPLACE MAPPING employees TYPE IMap OPTIONS ( 'keyFormat' = 'int', 'valueFormat' = 'json' ) """).result() # Select the names of employees older than 25 result = client.sql.execute(""" SELECT JSON_VALUE(this, '$.name') AS name FROM employees WHERE JSON_VALUE(this, '$.age' RETURNING INT) > 25 """).result() for row in result: print(f"Name: {row['name']}")
def read(self, inp): return HazelcastJsonValue(inp.read_utf())
def test_hazelcast_json_value_construction_with_none(self): with self.assertRaises(AssertionError): HazelcastJsonValue(None)
def test_hazelcast_json_value_construction_with_string(self): json_value = HazelcastJsonValue(self.json_str) self.assertEqual(self.json_str, json_value.to_string())
def test_hjv(self): value = HazelcastJsonValue({"a": 3}) self.map.set("key", value) self.assertEqual(value, self.map.get("key")) response = HazelcastJsonValue(self.get_from_server()) self.assertEqual(value, response)
def test_hazelcast_json_value_construction_with_json_serializable_object( self): json_value = HazelcastJsonValue(self.json_obj) self.assertEqual(json.dumps(self.json_obj), json_value.to_string())
def setUp(self): self.map = self.client.get_map(random_string()).blocking() self.map.put( 1, HazelcastJsonValue('{"attr1": 1, "attr2": 2, "attr3": 3}')) self.map.put( 2, HazelcastJsonValue('{"attr1": 4, "attr2": 5, "attr3": 6}'))
def test_hazelcast_json_value_loads(self): json_value = HazelcastJsonValue(self.json_str) self.assertEqual(self.json_obj, json_value.loads())
def test_identity_with_predicate(self): attributes = self.map.project(identity(), greater_or_equal("attr2", 3)) self.assertCountEqual( [HazelcastJsonValue('{"attr1": 4, "attr2": 5, "attr3": 6}')], [attribute.value for attribute in attributes], )
def test_storing_hazelcast_json_value_as_key(self): json_value = HazelcastJsonValue(self.json_str) self.map.put(json_value, 0) self.assertEqual(0, self.map.get(json_value))
def read(self, inp): return HazelcastJsonValue(inp.read_string())
import hazelcast from hazelcast.core import HazelcastJsonValue from hazelcast.predicate import less_or_equal from hazelcast.projection import single_attribute, multi_attribute client = hazelcast.HazelcastClient() people = client.get_map("people").blocking() people.put_all({ 1: HazelcastJsonValue({ "name": "Philip", "age": 46 }), 2: HazelcastJsonValue({ "name": "Elizabeth", "age": 44 }), 3: HazelcastJsonValue({ "name": "Henry", "age": 13 }), 4: HazelcastJsonValue({ "name": "Paige", "age": 15 }), }) names = people.project(single_attribute("name")) print("Names of the people are %s." % names)