def __init__(self, *args, **kwargs): """ Initialize a new AsyncViewBase object. This is intended to be subclassed in order to implement the require methods to be invoked on error, data, and row events. Usage of this class is not as a standalone, but rather as an ``itercls`` parameter to the :meth:`~couchbase.connection.Connection.query` method of the connection object. """ View.__init__(self, *args, **kwargs)
def delete_dataset(dataset, bucket, ratio): q = Query() q.mapkey_single = dataset v = View(bucket, "views", "keys", query=q) keys = (x.value for x in v if x.key == dataset) def pack_in_groups(keys, n, ratio): group = [] for k in keys: if ratio > random.random(): #print k if len(group) >= n: #print(group[0]) yield group group = [] group.append(k) if len(group) >= 0: yield group nremoved = 0 for kg in pack_in_groups(keys, 500, ratio): try: if kg: bucket.remove_multi(kg, quiet=True) except NotFoundError as error: print(error) else: nremoved += len(kg) if nremoved and nremoved % 10000 == 0: print(nremoved) return nremoved
def query(self, design, view, use_devmode=False, **kwargs): """ Query a pre-defined MapReduce view, passing parameters. This method executes a view on the cluster. It accepts various parameters for the view and returns an iterable object (specifically, a :class:`~couchbase.views.iterator.View`). :param string design: The design document :param string view: The view function contained within the design document :param boolean use_devmode: Whether the view name should be transformed into a development-mode view. See documentation on :meth:`design_create` for more explanation. :param kwargs: Extra arguments passedd to the :class:`~couchbase.views.iterator.View` object constructor. .. seealso:: * :class:`~couchbase.views.iterator.View` which contains more extensive documentation and examples * :class:`~couchbase.views.params.Query` which contains documentation on the available query options """ design = self._mk_devmode(design, use_devmode) return View(self, design, view, **kwargs)
def delete_frame_data(bucket): view = View(bucket, 'frame_keys', 'frame_keys') counter = 0 for result in view: bucket.delete(result.value) counter += 1 print('{} documents deleted'.format(counter))
def execute_on_server(self, query_exec_string, worker_id): try: from couchbase.views.iterator import View except ImportError: print "Unable to import Couchbase Python Client. \ Please see http://www.couchbase.com/communities/python/getting-started." sys.exit(0) print "Executing View Python Query" start = time.time() query_results = View(self.clients[worker_id], self.query_conf["ddoc_name"], self.query_conf["view_name"], query=query_exec_string) for result in query_results: print("Emitted key: {0}, value: {1}".format( result.key, result.value)) end = time.time() # for result in query_results: # print("Emitted key: {0}, value: {1}".format(result.key, result.value)) query_exec_time = end - start return query_results, query_exec_time
def GET(self): if(session.login == 0): return render.base(session, render.login_required(), [], []) l = [] for result in View(cb, "dev_mods", "mods"): l.append({ "url": result.value["url"], "name": result.value["name"] }) return render.base(session, render.mod(l), [], [])
def lookup_all_option_groups(self, menuitem_id): q = Query(stale=False, inclusive_end=True, mapkey_single=menuitem_id) resultset = [] for result in View(self.bucket, 'dev_dsx', 'menu_item_options', query=q): resultset.append(result.value) return resultset
def GET(self): players = [] for result in View(cb, "dev_result", "result"): w = int(result.value["w"]) l = int(result.value["l"]) n = result.value["nick"] players.append([n, w, l, int(round(w / (w + l), 2) * 100)]) players = sorted(players, key=lambda x: x[3], reverse=True) return render.base(session, render.ladder(players), [], [])
def find_all_model_info(self, limit=10): view = View(self.db, self.DDOC, self.VIEW) results = [] count = 0 for v in view: if count > limit: break results.append((v.docid, v.key, v.value)) count += 1 return results
def get_last_doc(self): #print "--> get_last_doc" """Searches in Couchbase to find the document that was modified or deleted most recently.""" q = Query(descending=True, limit=1) view = View(self.couchbase, "mongo_connect", "by_timestamp", query=q, include_docs=True) for row in view: print row return result.doc.value
def search(self, start_ts, end_ts): #print '--> search: ', start_it, end_ts """Searches in Couchbase and finds all documents that were modified or deleted within the timestamp range. """ q = Query(inclusive_end=False) q.mapkey_range = [start_ts, end_ts + q.STRING_RANGE_END] view = View(self.couchbase, "mongo_connect", "by_timestamp", query=q, include_docs=True) for row in view: print row yield result.doc.value
def server_setup(self): try: from couchbase import Couchbase from couchbase.views.iterator import View except ImportError: print "Unable to import Couchbase Python Client. \ Please see http://www.couchbase.com/communities/python/getting-started." sys.exit(0) for i in xrange(self.num_workers): self.clients.append( Couchbase.connect( host=self.couchbase_ini["cb_server"], bucket=self.couchbase_ini["cb_bucket"], username=self.couchbase_ini["cb_bucket"], password=self.couchbase_ini["cb_bucket_password"], timeout=2.5)) if "view_def" in self.query_conf: setup_view_info = self.query_conf["view_def"] elif "view_def_file" in self.query_conf: setup_view_info = self.read_json_file( "conf/ddocs/" + self.query_conf["view_def_file"]) else: print "View Definition Not Found!!" print "Creating Design Doc" self.clients[0].design_create(self.query_conf["ddoc_name"], setup_view_info, use_devmode=False) time.sleep(2) if self.query_conf["type"] == "view_python_index_query": print "Executing View Query to Build Indexes" query_results = View(self.clients[0], self.query_conf["ddoc_name"], self.query_conf["view_name"], stale="false", connection_timeout=300000) time.sleep(300) for result in query_results: pass
def search_tag(query, page): (qcount, acount, tcount, ucount, tag_list) = common_data() tag=query[4:] q=pyes.MatchQuery('tag', tag) question_results=kunjika.es_conn.search(query=q) results=[] for r in question_results: results.append(r['tag']) print r['tag'] questions_list=[] for tag in results: #question_view = urllib2.urlopen( # kunjika.DB_URL + 'questions/_design/dev_qa/_view/get_questions_by_tag?key=' + '"' + tag + '"').read() #question_view = json.loads(question_view)['rows'] #print question_view q = Query(key=tag) for result in View(kunjika.qb, "dev_qa", "get_questions_by_tag", include_docs=True, query=q): #print result questions_list.append(result.doc.value) #for element in question_view['rows']: # questions_list.append(element['value']) for i in questions_list: i['tstamp'] = strftime("%a, %d %b %Y %H:%M", localtime(i['content']['ts'])) user = kunjika.cb.get(i['content']['op']).value i['opname'] = user['name'] pagination = Pagination(page, kunjika.QUESTIONS_PER_PAGE, len(questions_list)) if g.user is None: return render_template('search.html', title='Search results for ' + query, qpage=True, questions=questions_list[(page-1)*kunjika.QUESTIONS_PER_PAGE:], pagination=pagination, qcount=qcount, ucount=ucount, tcount=tcount, acount=acount, tag_list=tag_list, query=query) elif g.user is not None and g.user.is_authenticated(): return render_template('search.html', title='Search results for ' + query, qpage=True, questions=questions_list[(page-1)*kunjika.QUESTIONS_PER_PAGE:], pagination=pagination, qcount=qcount, ucount=ucount, tcount=tcount, acount=acount, tag_list=tag_list, query=query) else: return render_template('search.html', title='Search results for ' + query, qpage=True, questions=questions_list[(page-1)*kunjika.QUESTIONS_PER_PAGE:], pagination=pagination, qcount=qcount, ucount=ucount, tcount=tcount, acount=acount, tag_list=tag_list, query=query)
def prepare_mods(): l = [] for result in View(cb, "dev_mods", "mods"): l.append({"url": result.value["url"], "name": result.value["name"]}) out = '#ifndef MODS_H_\n' out += '#define MODS_H_\n\n' for i in l: out += '#ifdef MOD_%s\n' % i['url'].upper() out += '#define MOD_NAME "%s"\n' % i['name'] out += '#define MOD_URL "%s"\n' % i['url'] out += '#endif // MOD_%s\n\n' % i['url'] out += '#endif' f = open("../src/include/mods.h", "w") f.write(out) f.close() f = open("../../hm_gameserver/src/include/mods.h", "w") f.write(out) f.close()
def query_view(view_name, query_key, query=None): design, v = parse_view_name(view_name) query = query or Query(key=query_key, stale=get_stale()) result = View(connection(), design, v, query=query) result_keys = [x.docid for x in result] return result_keys
def listDataset(argv): from couchbase.views.iterator import View from couchbase.views.params import Query from striped.client import CouchBaseBackend from couchbase.exceptions import KeyExistsError, TemporaryFailError, TimeoutError, NotFoundError Usage = """ python listDataset.py -c <CouchBase config file> [-f|-l] <bucket name> <dataset name> """ config_file = None opts, args = getopt.getopt(argv, "c:lfn") opts = dict(opts) config_file = opts.get("-c") files_only = "-f" in opts long_print = "-l" in opts counter = "-n" in opts if len(sys.argv) < 2: print(Usage) sys.exit(1) bucket_name, dataset_name = args backend = CouchBaseBackend(bucket_name, config=config_file) bucket = backend.bucket if False: q = Query() q.mapkey_single = dataset_name v = View(bucket, "views", "RGInfos", query=q) infos = [x.value for x in v if x.key == dataset_name] infos = backend.RGInfos(dataset_name) infos = sorted(infos, key = lambda info: info["RGID"]) if long_print: print("RGID NEvents File(s)") print("------- ---------- -------") nevents = 0 files = {} rgids = set() for info in infos: fn = info["Segments"][0]["FileName"] print("%7d %10d %s" % (info["RGID"], info["NEvents"], fn)) rgids.add(info["RGID"]) files[fn] = 1 for s in info["Segments"][1:]: print("%19s %s" % (" ", s["FileName"])) files[s["FileName"]] = 1 nevents += info["NEvents"] print("------- ---------- -------") print("%7d %10d %d" % (len(infos), nevents, len(files))) maxrgid = max(rgids) if len(rgids) != maxrgid+1: print("Missing RGIDs (%d):" % (maxrgid+1 - len(rgids),)) for rgid in range(maxrgid): if not rgid in rgids: print(rgid, end=' ') print() elif files_only: files = {} # filename -> nevents for info in infos: for s in info["Segments"]: fn = s["FileName"] files[fn] = files.get(fn, 0) + s["NEvents"] for fn in sorted(files.keys()): print(fn) else: files = set() rgids = set() nevents = 0 try: counter = backend.counter("%s:@@nextRGID" % (dataset_name,), delta=0).value except NotFoundError: counter = None for info in infos: rgids.add(info["RGID"]) for s in info["Segments"]: files.add(s["FileName"]) nevents += info["NEvents"] print("Next FrameID: ", counter) print("Files: ", len(files)) print("Frames: ", len(rgids)) print("Events: ", nevents) if len(rgids): print("Max farme id: ", max(rgids)) print("Events/frame: ", int(float(nevents)/float(len(rgids))+0.5)) maxrgid = max(rgids) if len(rgids) < maxrgid + 1: print("Missing RGIDs (%d):" % (maxrgid+1 - len(rgids),)) for rgid in range(maxrgid): if not rgid in rgids: print(rgid, end=' ') print()
from collections import Counter import numpy as np CBS="10.112.151.101" SG="10.112.151.101" CBG="10.112.151.1" cb = Bucket('couchbase://10.112.151.101/beer-sample?operation_timeout=30') cb2 = Bucket('couchbase://10.112.151.101/beers?operation_timeout=30') i = 0 start = 0 end = 0 avg_track = [] headers = {'content-type': 'application/json'} for result in View(cb, "beer", "allkeys"): jsonID = result.docid payload = cb.get(jsonID).value jsonKey = ("SG::" + str(i)) start = time.time() resp = requests.post('http://{}:4984/beers/'.format(CBS), data=json.dumps(payload), headers=headers) end = time.time() avg_track.extend([(end - start) * 1000]) #time.sleep(2) i = i + 1 avg_value = sum(avg_track)/len(avg_track) #print(avg_track) print("average SG POST ... ") print (avg_value) modedata = Counter(avg_track)
import os from couchbase import Couchbase from couchbase.views.iterator import View from couchbase.n1ql import N1QLQuery couchbase_host = '10.1.193.189' couchbase_bucket = 'persona' # Define variables couchbucket = Couchbase.connect(bucket=couchbase_bucket, host=couchbase_host) print(couchbucket) print("-----byname view----") cnt = 1 cntnokey = 1 v = View(couchbucket, "byname", "byname", limit=3000) for r in v: theManager = "" if r.key != None: allADdates = [] for rk in r.value.keys(): if rk.startswith('AD'): allADdates.append(rk) # print(str(allADdates.sort())) allADdates.sort() for adDay in allADdates: if ('manager' in r.value[adDay][1]): cnt += 1 theManager = theManager + " ## " + r.value[adDay][1][ 'manager'][0].split(",")[0]
def get_view(self, design, view, key, include_docs=False): return View(self.get_client(self.connection_string), design, view, key=key, stale='ok', full_set=True, include_docs=include_docs)