def aggregateUsersByTweetCountByRegion(self): policy = {} udf_type = 0 # 0 for LUA lua_file_name = 'udf/aggregationByRegion.lua' try: min = int(raw_input("Enter Min Tweet Count: ")) max = int(raw_input("Enter Max Tweet Count: ")) print("\nAggregating users with ", min, "-", max, " tweets by region:\n") self.client.udf_put(lua_file_name, udf_type, policy) time.sleep(5) tweetQuery = self.client.query("test", "users") # callback for each record read def tweetQueryAggCallback(record): print("\nTotal Users in North: ", record['n'], "\n") print("\nTotal Users in North: ", record['s'], "\n") print("\nTotal Users in North: ", record['e'], "\n") print("\nTotal Users in North: ", record['w'], "\n") # invoke the operations, and for each record invoke the callback tweetQuery.where(p.between('tweetcount', min, max)) tweetQuery.apply("aggregationByRegion", "sum") tweetQuery.foreach(tweetQueryAggCallback) except Exception as e: print("error: {0}".format(e), file=sys.stderr)
def test_aggregate_with_arguments_to_lua_function_having_float_value(self): pytest.xfail("Missing function in lua") """ Invoke aggregate() with unicode arguments to lua function having a float value """ if TestAggregate.skip_old_server is True: pytest.skip( "Server does not support aggregate on float \ type as lua argument") query = self.client.query('test', 'demo') query.where(p.between('test_age', 0, 5)) query.apply( 'stream_example', 'double_group_count', [u"name", u"addr", 2.5]) rec = [] def callback(value): rec.append(value) query.foreach(callback) assert rec == [ {u'name4': 3.5, u'name2': 3.5, u'name3': 3.5, u'name0': 3.5, u'name1': 3.5} ]
def test_3(self): """ Using multiple keys """ from aerospike import predicates as p global count for i in range(2): key = ('test', 'unittest', i) rec = { 'name': 'name%s' % (str(i)), 'addr': 'name%s' % (str(i)), 'age': i, 'no': i } self.client.put(key, rec) self.client.index_integer_create('test', 'unittest', 'age', 'age_index', {}) query = self.client.query('test', 'unittest') query.select("name", "age") count = 0 query.where(p.between('age', 1, 3)) query.foreach(count_records_false) self.assertEqual(count, 1, "foreach failed") for i in range(2): key = ('test', 'unittest', i) self.client.remove(key) self.client.index_remove('test', 'age_index', {})
def aggregateUsersByTweetCountByRegion(self): policy = {} udf_type = 0 # 0 for LUA lua_file_name = 'udf/aggregationByRegion.lua' try: min = int(raw_input("Enter Min Tweet Count: ")) max = int(raw_input("Enter Max Tweet Count: ")) print("\nAggregating users with " , min , "-" , max , " tweets by region:\n") self.client.udf_put(policy, lua_file_name, udf_type) time.sleep(5) argsForUDF = map(self.__prepForUDF,"region") tweetQuery = self.client.query("test", "users") # callback for each record read def tweetQueryAggCallback(record): print("\nTotal Users in North: ", record['n'],"\n") print("\nTotal Users in North: ", record['s'],"\n") print("\nTotal Users in North: ", record['e'],"\n") print("\nTotal Users in North: ", record['w'],"\n") # invoke the operations, and for each record invoke the callback tweetQuery.where(p.between('tweetcount',min,max)) #tweetQuery.apply("aggregationByRegion", "sum",*argsForUDF) tweetQuery.apply("aggregationByRegion", "sum") tweetQuery.foreach(tweetQueryAggCallback) except Exception as e : print("error: {0}".format(e), file=sys.stderr)
def test_3(self): """ Using multiple keys """ from aerospike import predicates as p from time import sleep global count for i in xrange(2): key = ("test", "unittest", i) rec = {"name": "name%s" % (str(i)), "addr": "name%s" % (str(i)), "age": i, "no": i} self.client.put(key, rec) self.client.index_integer_create("test", "unittest", "age", "age_index", {}) query = self.client.query("test", "unittest") query.select("name", "age") count = 0 query.where(p.between("age", 1, 3)) query.foreach(count_records_false) self.assertEqual(count, 1, "foreach failed") for i in xrange(2): key = ("test", "unittest", i) self.client.remove(key) self.client.index_remove("test", "age_index", {})
def test_aggregate_with_arguments_to_lua_function_having_float_value(self): """ Invoke aggregate() with unicode arguments to lua function having a float value """ if TestAggregate.skip_old_server == True: pytest.skip( "Server does not support aggregate on float type as lua argument" ) query = self.client.query('test', 'demo') query.where(p.between('test_age', 0, 5)) query.apply('stream_example', 'double_group_count', [u"name", u"addr", 2.5]) rec = [] def callback(value): rec.append(value) query.foreach(callback) assert rec == [{ u'name4': 3.5, u'name2': 3.5, u'name3': 3.5, u'name0': 3.5, u'name1': 3.5 }]
def queryUsersByTweetCount(self): print("\n********** Query Users By Tweet Count Range **********\n") try: # Create a Secondary Index on tweetcount # Exercise Q4 #self.client.index_integer_create("test", "users", "tweetcount", "tweetcount_index", None) #time.sleep(5) #print("\nInteger Secondary Index Created ") # Create Query and Set min--max range Filter on tweetcount # Exercise Q4 min = int(input("Enter Min Tweet Count: ")) max = int(input("Enter Max Tweet Count: ")) print("\nList of users with " , min , "-" , max , " tweets:\n") tweetQuery = self.client.query("test", "users") # Select bin(s) you would like to retrieve tweetQuery.select('username', 'tweetcount') tweetQuery.where(p.between('tweetcount',min,max)) # Define the Call back to print Tweets for given Username # Exercise Q4 def tweetQueryCountCallback(input_tuple): (key, meta, record)=input_tuple print(record["username"] , " has " , record["tweetcount"] , " tweets\n") # Execute query and for each record invoke the callback # Exercise Q4 tweetQuery.foreach(tweetQueryCountCallback) except Exception as e : print("error: {0}".format(e), file=sys.stderr)
def get(self): query = client.query('test', 'sessions') _from = random.randint(0, 100) to = 100 query.where(p.between('expires', _from, to)) results = [] def each_session(t): session = t[2] nick = session['nick'] t = client.get(('test', 'users', nick)) user = t[2] if user is not None: results.append(user['email']) query.foreach(each_session) self.write( json.dumps({ 'data': results, 'version': 1, 'date': str(datetime.now()), })) self.finish()
def query(self, as_host, ns_name, set_name, key_name, days, s3_bucket, s3_key): now = int(time.time()) min_ts = now - (60 * 60 * 24 * days) max_ts = 2147483647 # 2038-01-19 12:14:07 fn = '/tmp/{0}.txt'.format(str(uuid.uuid4())) cl = aerospike.client({'hosts': [(as_host, 3000)]}).connect() q = cl.query(ns_name, set_name) q.select(key_name) q.where(predicates.between("ts", min_ts, max_ts)) def write_id((key, meta, bins)): f.write(bins[key_name] + "\n") # logger.info('aql: SELECT {0} FROM {1}.{2} WHERE ts > {3}'.format(key_name, ns_name, set_name, min_ts)) # logger.info('Start Query ...') with open(fn, "w") as f: q.foreach(write_id, {"timeout": 0}) # logger.info('Finish Query ...') cl.close() digdag.env.store({'file': fn}) s3_mv(fn, 's3://{0}/{1}'.format(s3_bucket, s3_key))
def test_query_apply_with_ns_set_none(self): """ Invoke query_apply() with ns and set as None """ with pytest.raises(TypeError) as typeError: query_id = self.client.query_apply(None, None, p.between("age", 1, 5), "bin_lua", "mytransform", ['age', 2]) assert "query_apply() argument 1 must be string, not None" in typeError.value
def test_query_apply_with_ns_set_none(self): """ Invoke query_apply() with ns and set as None """ with pytest.raises(TypeError) as typeError: self.client.query_apply(None, None, p.between("age", 1, 5), "bin_lua", "mytransform", ['age', 2]) assert "query_apply() argument 1 must be str" in str(typeError.value)
def query_with_limit(filter, lim): res = [] def add_record(record): res.append(record) query = client.query(NAMESPACE, 'iris') query.where(p.between(filter[0], filter[1], filter[2])) query.foreach(limit(lim, res)) return res
def test_query_apply_with_extra_argument(self): """ Invoke query_apply() with extra argument """ policy = {'timeout': 1000} with pytest.raises(TypeError) as typeError: query_id = self.client.query_apply("test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform_incorrect", ['age', 2], policy, "") assert "query_apply() takes at most 7 arguments (8 given)" in typeError.value
def filter_and_group_count(filter, groupby): res = [] def add_record(record): res.append(record) query = client.query(NAMESPACE, 'iris') query.where(p.between(filter[0], filter[1], filter[2])) query.apply('filter_and_group_count', 'count', [groupby]) query.foreach(add_record) return res
def test_query_apply_with_argument_is_none(self): """ Invoke query_apply() with arguments as None """ try: self.client.query_apply("test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform", None) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == "Arguments should be a list"
def test_query_apply_with_argument_is_none(self): """ Invoke query_apply() with arguments as None """ try: query_id = self.client.query_apply("test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform", None) except ParamError as exception: assert exception.code == -2 assert exception.msg == "Arguments should be a list"
def test_query_apply_with_module_function_none(self): """ Invoke query_apply() with None module function """ try: query_id = self.client.query_apply("test", "demo", p.between("age", 1, 5), None, None, ['age', 2]) except ParamError as exception: assert exception.code == -2L assert exception.msg == "Module name should be string"
def test_query_apply_with_extra_argument(self): """ Invoke query_apply() with extra argument """ policy = {'timeout': 1000} with pytest.raises(TypeError) as typeError: self.client.query_apply( "test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform_incorrect", ['age', 2], policy, "") assert "query_apply() takes at most 7 arguments (8 given)" in str( typeError.value)
def test_query_apply_with_incorrect_module_type(self): """ Invoke query_apply() with incorrect module type """ query_id = self.client.query_apply("test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform", ['age', 2]) try: time.sleep(0.1) response = self.client.job_info(query_id, "aggregate") except ParamError as exception: assert exception.code == -2 assert exception.msg == "Module can have only two values: aerospike.JOB_SCAN or aerospike.JOB_QUERY"
def test_query_with_correct_parameters_between(self): """ Invoke query() with correct arguments and using predicate between """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 4)) records = [] def callback((key,metadata,record)): records.append(record) query.foreach(callback) assert len(records) == 4
def test_query_apply_with_correct_parameters_without_connection(self): """ Invoke query_apply() with correct parameters without connection """ config = {'hosts': [('127.0.0.1', 3000)]} client1 = aerospike.client(config) try: client1.query_apply("test", "demo", p.between( "age", 1, 5), "bin_lua", "mytransform", ['age', 2]) except e.ClusterError as exception: assert exception.code == 11 assert exception.msg == 'No connection to aerospike cluster'
def test_aggregate_with_where_bool_value(self): """ Invoke aggregate() with where is bool value """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', True, True)) query.apply('stream_example', 'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records[0] == 1
def test_aggregate_with_arguments_to_lua_function(self): """ Invoke aggregate() with unicode arguments to lua function. """ query = self.client.query('test', 'demo') query.where(p.between('test_age', 0, 5)) query.apply('stream_example', 'group_count', [u"name", u"addr"]) rec = [] def callback(value): rec.append(value) query.foreach(callback) assert rec == [{u'name4': 1, u'name2': 1, u'name3': 1, u'name0': 1, u'name1': 1}]
def test_query_apply_with_correct_parameters_without_connection(self): """ Invoke query_apply() with correct parameters without connection """ config = {'hosts': [('127.0.0.1', 3000)]} client1 = aerospike.client(config) try: query_id = client1.query_apply("test", "demo", p.between("age", 1, 5), "bin_lua", "mytransform", ['age', 2]) except ClusterError as exception: assert exception.code == 11L assert exception.msg == 'No connection to aerospike cluster'
def aggregateUsersByTweetCountByRegion(self): policy = {} udf_type = 0 # 0 for LUA lua_file_name = 'udf/aggregationByRegion.lua' try: min = int(raw_input("Enter Min Tweet Count: ")) max = int(raw_input("Enter Max Tweet Count: ")) print("\nAggregating users with " , min , "-" , max , " tweets by region:\n") # Register UDF # Exercise 2 # Note: Registration via udf_put() will register udfs both on server # side and local client side in local user_path specified in connection # configuration. AQL registers udfs with server only. If using AQL, # for stream udfs, copy them manually in local client node lua user_path. # NOTE: UDF registration has been included here for convenience # and to demonstrate the syntax. # Create a separate script to register udfs only when modified. self.client.udf_put(lua_file_name, udf_type, policy) time.sleep(5) tweetQuery = self.client.query("test", "users") # Set min--max range Filter on tweetcount # Exercise 2 tweetQuery.where(p.between('tweetcount',min,max)) # Execute aggregate query passing in , .lua filename of the UDF and lua function name # Exercise 2 tweetQuery.apply("aggregationByRegion", "sum") # Output result to the console in format \"Total Users in <region>: <#>\" # Exercise 2 # callback for each record read def tweetQueryAggCallback(record): print("\nTotal Users in North: ", record['n'],"\n") print("\nTotal Users in South: ", record['s'],"\n") print("\nTotal Users in East: ", record['e'],"\n") print("\nTotal Users in West: ", record['w'],"\n") # invoke the operations, and for each record invoke the callback tweetQuery.foreach(tweetQueryAggCallback) except Exception as e : print("error: {0}".format(e), file=sys.stderr)
def test_aggregate_with_multiple_results_call_on_same_query_object(self): """ Invoke aggregate() with multiple foreach on same query object. """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count') records = query.results() assert records[0] == 4 records = [] records = query.results() assert records[0] == 4
def test_aggregate_with_empty_module_function(self): """ Invoke aggregate() with empty module and function """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('', '') result = None def user_callback(value): result = value query.foreach(user_callback) assert result == None
def test_aggregate_with_unicode_module_and_function_name(self): """ Invoke aggregate() with unicode module and function names """ query = self.client.query('test', 'demo') query.select(u'name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply(u'stream_example', u'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records[0] == 4
def test_query_with_correct_parameters_between(self): """ Invoke query() with correct arguments and using predicate between """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 4)) records = [] def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 4
def test_aggregate_with_extra_parameter_in_lua(self): """ Invoke aggregate() with extra parameter in lua """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count_extra') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records[0] == 4
def test_neg_aggregate_no_sec_index(self): """ Invoke aggregate() with no secondary index """ try: query = self.as_connection.query('test', 'demo') query.select('name', 'no') query.where(p.between('no', 1, 5)) query.apply('stream_example', 'count') def user_callback(value): _ = value query.foreach(user_callback) except e.IndexNotFound as exception: assert exception.code == 201
def test_pos_aggregate_with_correct_parameters(self): """ Invoke aggregate() with correct arguments """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records[0] == 4
def test_query_with_correct_parameters_between(self): """ Invoke query() with correct arguments and using predicate between """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 4)) records = [] def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 4
def test_query_with_callback_returning_false(self): """ Invoke query() with callback function returns false """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) records = [] def callback((key,metadata,record)): if len(records) == 2: return False records.append(key) result = query.foreach(callback) assert len(records) == 2
def test_aggregate_no_sec_index(self): """ Invoke aggregate() with no secondary index """ try: query = self.client.query('test', 'demo') query.select('name', 'no') query.where(p.between('no', 1, 5)) query.apply('stream_example', 'count') def user_callback(value): _ = value query.foreach(user_callback) except e.IndexNotFound as exception: assert exception.code == 201 assert exception.msg == 'AEROSPIKE_ERR_INDEX_NOT_FOUND'
def test_aggregate_with_policy(self): """ Invoke aggregate() with policy """ policy = {'timeout': 1000} query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback, policy) assert records[0] == 4
def test_aggregate_with_less_parameter_in_lua(self): """ Invoke aggregate() with less parameter in lua """ with pytest.raises(Exception) as exception: query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count_less') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert exception.value[0] == -1L
def test_aggregate_no_sec_index(self): """ Invoke aggregate() with no secondary index """ with pytest.raises(Exception) as exception: query = self.client.query('test', 'demo') query.select('name', 'no') query.where(p.between('no', 1, 5)) query.apply('stream_example', 'count'); result = None def user_callback(value): result = value query.foreach(user_callback) assert exception.value[0] == 201L assert exception.value[1] == 'AEROSPIKE_ERR_INDEX_NOT_FOUND'
def test_aggregate_with_incorrect_function(self): """ Invoke aggregate() with incorrect function """ with pytest.raises(Exception) as exception: query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'countno') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert exception.value[0] == -1L assert exception.value[1] == 'UDF: Execution Error 2 : function not found'
def test_query_with_callback_returning_false(self): """ Invoke query() with callback function returns false """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) records = [] def callback((key, metadata, record)): if len(records) == 2: return False records.append(key) result = query.foreach(callback) assert len(records) == 2
def test_aggregate_with_incorrect_module(self): """ Invoke aggregate() with incorrect module """ with pytest.raises(Exception) as exception: query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('streamwrong', 'count') result = None def user_callback(value): result = value query.foreach(user_callback) assert exception.value[0] == -1L assert exception.value[1] == 'UDF: Execution Error 1'
def test_query_with_callback_returning_false(self): """ Invoke query() with callback function returns false """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) records = [] def callback(input_tuple): key, _, _ = input_tuple if len(records) == 2: return False records.append(key) query.foreach(callback) assert len(records) == 2
def queryUsersByTweetCount(self): print("\n********** Query Users By Tweet Count Range **********\n") try: self.client.index_integer_create(None, "test", "users", "tweetcount", "tweetcount_index") time.sleep(5) print("\nINFO: Integer Secondary Index Created ") min = int(raw_input("Enter Min Tweet Count: ")) max = int(raw_input("Enter Max Tweet Count: ")) print("\nList of users with " , min , "-" , max , " tweets:\n") tweetQuery = self.client.query("test", "users") #tweetQuery.select('username') # callback for each record read def tweetQueryCountCallback((key, meta, record)): print(record["username"] , " has " , record["tweetcount"] , " tweets\n") # invoke the operations, and for each record invoke the callback tweetQuery.where(p.between('tweetcount',min,max)) tweetQuery.foreach(tweetQueryCountCallback) except Exception as e : print("error: {0}".format(e), file=sys.stderr)
def test_query_with_callback_returning_false(self): """ Invoke query() with callback function returns false This will stop iteration """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) records = [] def callback(input_tuple): key, _, _ = input_tuple if len(records) == 2: return False records.append(key) query.foreach(callback) assert len(records) == 2
def test_aggregate_with_extra_parameter(self): """ Invoke aggregate() with extra parameter """ policy = {'timeout': 1000} with pytest.raises(TypeError) as typeError: query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.between('test_age', 1, 5)) query.apply('stream_example', 'count') def user_callback(value): _ = value query.foreach(user_callback, policy, "") assert "foreach() takes at most 2 arguments (3 given)" in str( typeError.value)