def test_predicate_portable_key(self):
        _map = self._fill_map()
        map_keys = _map.keys()

        predicate = sql("param_int >= 900")
        key_set = self.map.key_set(predicate)
        self.assertEqual(len(key_set), 100)
        for k in key_set:
            self.assertGreaterEqual(k.param_int, 900)
            self.assertIn(k, map_keys)
Example #2
0
    def test_predicate_portable_key(self):
        _map = self._fill_map()
        map_keys = _map.keys()

        predicate = sql("param_int >= 900")
        key_set = self.map.key_set(predicate)
        self.assertEqual(len(key_set), 100)
        for k in key_set:
            self.assertGreaterEqual(k.param_int, 900)
            self.assertIn(k, map_keys)
Example #3
0
    def getResults(self, feedName):
        query = request.get_json()
        map = self.hz.get_map('{}-{}'.format(feedName, query.get("kind")))
        predicate = sql(query.get("value"))
        values = map.values(predicate).result()

        payload = []
        for value in values:
            payload.append(value.loads())
        if payload:
            return Response(json.dumps(payload), mimetype="application/json")
        else:
            return Response("not found", status=404)
Example #4
0
    def getContainer(self):
        """
        return a port associated with a running browser container which is not the mainPort

        :return:
        """

        workerPorts = self.hz.get_map(self.container_map)
        unused: List[Container] = workerPorts.values(
            sql("active = 'no'")).result()
        if len(unused) == 0:
            return "max containers reached"
        else:
            port = unused[0].port
            try:
                browser: Browser = self.client.containers.get(
                    'worker-{port}'.format(port=port))
                workerPorts.replace(key=port,
                                    value=Container(
                                        port=port,
                                        active="yes",
                                        status=browser.status)).result()
                browser.restart()
            except APIError as e:
                if e.status_code == 404:
                    browser = self.client.containers.run(
                        self.client.images.get(browser_params['image']),
                        detach=True,
                        name='worker-{}'.format(port),
                        ports={'4444/tcp': port},
                        network=os.getenv("NETWORK", "car_default"))
                    workerPorts.replace(key=port,
                                        value=Container(
                                            port=port,
                                            active="yes",
                                            status=browser.status)).result()
                else:
                    return str(port)
            logging.info(msg='started browser named worker-{port}'.format(
                port=port))
            self.wait_for_log(browser, BrowserConstants().CONTAINER_SUCCESS)
            return str(port)
Example #5
0
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()
Example #6
0
    logging.basicConfig()
    logging.getLogger().setLevel(logging.INFO)

    config = hazelcast.ClientConfig()

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

    client = hazelcast.HazelcastClient(config)

    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()))

    predicate = sql("age <= 29")

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

    my_map.values(predicate).add_done_callback(values_callback)

    time.sleep(10)
    client.shutdown()
    print(my_map)
    #
    _map = fill_map(my_map, 1000)
    # map_keys = _map.keys()
    #
    # predicate = sql("param-int >= 990")
    # key_set = my_map.key_set(predicate)
    #
    # print("map.size=", my_map.size())
    # print("result size=", len(key_set))
    # print('Iterate over all map:')
    #
    # for key in key_set:
    #     print "key:", key

    my_map_async = client.get_map("map")
    print("Map Size:", my_map_async.size().result())

    predicate2 = sql("param-str ILIKE 'value-2%'")

    def values_callback(f):
        result_set = f.result()
        print("Query Result Size:", len(result_set))
        for value in result_set:
            print "value:", value
    my_map_async.values(predicate2).add_done_callback(values_callback)


    sleep(10)
    client.shutdown()
Example #8
0
 def test_sql(self):
     self._fill_map()
     predicate = sql("this == 'value-1'")
     self.assertItemsEqual(self.map.key_set(predicate), ["key-1"])
Example #9
0
 def test_sql(self):
     predicate = sql("this == 'value-1'")
     self.assertEqual(str(predicate),
                      "SqlPredicate(sql='this == 'value-1'')")
Example #10
0
if __name__ == '__main__':
    config = hazelcast.ClientConfig()

    # add serialization for our SecMasterBean using Portable Serialization
    config.serialization_config.portable_factories[SecMasterBean.FACTORY_ID] = \
        {SecMasterBean.CLASS_ID: SecMasterBean}

    client = hazelcast.HazelcastClient(config)

    # Get a local instance to the distributed IMap
    my_map = client.get_map("demo.exoduspoint.beans.SecMasterBean")

    # see the current size
    print("Map Size: {}".format(my_map.size().result()))

    # perform sql search
    predicate = sql("NAME like 'B%' and ID_COMMON='3299'")

    def values_callback(f):
        result_set = f.result()
        print("Query Result Size: {}".format(len(result_set)))
        for value in result_set:
            print("\tvalue: {}".format(value.NAME))
            print("\tvalue: {}".format(value.ID_COMMON))

    my_map.values(predicate).add_done_callback(values_callback)

    # async call needs some time to complete prior to exiting
    time.sleep(2)
    client.shutdown()
 def test_sql(self):
     self._fill_map()
     predicate = sql("this == 'value-1'")
     self.assertItemsEqual(self.map.key_set(predicate), ["key-1"])
 def test_sql(self):
     predicate = sql("this == 'value-1'")
     self.assertEqual(str(predicate), "SqlPredicate(sql='this == 'value-1'')")
    def test_nested_attribute_query_sql_predicate(self):
        predicate = sql("limb.name == 'leg'")
        values = self.map.values(predicate)

        self.assertEqual(1, len(values))
        self.assertEqual("body2", values[0].name)
    print(my_map)
    #
    _map = fill_map(my_map, 1000)
    # map_keys = _map.keys()
    #
    # predicate = sql("param-int >= 990")
    # key_set = my_map.key_set(predicate)
    #
    # print("map.size=", my_map.size())
    # print("result size=", len(key_set))
    # print('Iterate over all map:')
    #
    # for key in key_set:
    #     print "key:", key

    my_map_async = client.get_map("map")
    print("Map Size:", my_map_async.size().result())

    predicate2 = sql("param-str ILIKE 'value-2%'")

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

    my_map_async.values(predicate2).add_done_callback(values_callback)

    sleep(10)
    client.shutdown()
Example #15
0
                                                         self.age, self.active)


def generate_users(users):
    users.put("Rod", User("Rod", 19, True))
    users.put("Jane", User("Jane", 20, True))
    users.put("Freddy", User("Freddy", 23, True))


config = ClientConfig()
portable_factory = {User.CLASS_ID: User}
config.serialization.add_portable_factory(User.FACTORY_ID, portable_factory)
# Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
hz = hazelcast.HazelcastClient(config)
# Get a Distributed Map called "users"
users = hz.get_map("users").blocking()
# Add some users to the Distributed Map
generate_users(users)
# Create a Predicate from a String (a SQL like Where clause)
sql_query = sql("active AND age BETWEEN 18 AND 21)")
# Creating the same Predicate as above but with a builder
criteria_query = and_(is_equal_to("active", True), is_between("age", 18, 21))
# Get result collections using the two different Predicates
result1 = users.values(sql_query)
result2 = users.values(criteria_query)
# Print out the results
print(result1)
print(result2)
# Shutdown this Hazelcast Client
hz.shutdown()