def test_sync_many(self): '''see issue 218''' func = 'function(doc) { emit(doc._id, doc._rev); }' first_view = design.ViewDefinition('design_doc', 'view_one', func) second_view = design.ViewDefinition('design_doc_two', 'view_one', func) third_view = design.ViewDefinition('design_doc', 'view_two', func) _, db = self.temp_db() results = design.ViewDefinition.sync_many( db, (first_view, second_view, third_view)) self.assertEqual(len(results), 2, 'There should only be two design documents')
def create_views(db): """ 2 views for getting info about sentiment """ # view 1: return sum and counts of sentiment for each hour map = '''function (doc) { var date = new Date(Date.parse(doc.created_at)) var newDate = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + date.getHours() emit(newDate, doc.sentiment); }''' reduce = '''function(keys, values, rereduce) { if (rereduce) { return { 'sum': values.reduce(function(a, b) { return a + b.sum }, 0), 'count': values.reduce(function(a, b) { return a + b.count }, 0), 'positive': values.reduce(function(a, b) { return a + b.positive }, 0), 'negative': values.reduce(function(a, b) { return a + b.negative }, 0) } } else { var pos = 0; var neg = 0; values.forEach(function (value) { if (value > 0) { pos += 1; } else if (value < 0) { neg += 1; } }); return { 'sum': sum(values), 'count': values.length, 'positive': pos, 'negative': neg } } }''' view = design.ViewDefinition( "sentiment", # design document "hour_sentiment", # view name map, # map reduce) view.sync(db) # view 2: return overall sum and counts of sentiment map2 = '''function(doc) { emit(doc._id, doc.sentiment); }''' reduce2 = reduce # same as view 1: 'stats' view2 = design.ViewDefinition("sentiment", "sentiment", map2, reduce2) view2.sync(db)
def view_gluttony_collected_data(db): map_fnc = """function (doc) { emit(doc.code, doc.gluttony); }""" reduce_fnc = """function (keys, values, rereduce) { var analysis = { count: 0, polarity: 0 }; if (rereduce){ for(var i=0; i < values.length; i++) { analysis.count += values[i].count; analysis.polarity += values[i].polarity; } analysis.polarity = analysis.polarity / analysis.count; return analysis; } analysis.count = values.length; analysis.polarity = sum(values); return analysis; }""" # analysis.count = values.length 意思是有多少条记录 # analysis.polarity = sum(values) 意思是把这些记录的值加起来 view = design.ViewDefinition('sentiment_analysis', 'sentiment_analysis', map_fnc, reduce_fun=reduce_fnc) view.sync(db)
def create_views_month(db): view = design.ViewDefinition('time_senti', 'mapreduce', map_function_month, reduce_function) view.sync(db) row1s = db3.view('time_senti/mapreduce', descending=True, group=True) for row1 in row1s: print(row1)
def _read_view(self, doc_name, view_name): """ Read a view corresponding to the given doc_name and view_name Views must be stored under "views/" in a format like views/{design doc}/{view name}/map.js + reduce.js Reduce is optional :param doc_name: The design document the view will be written to :param view_name: The name of the view :returns: The constructed couchdb view definition :rtype: couchdb.design.ViewDefinition """ map = None reduce = None path = os.path.join(self.view_path, doc_name, view_name) map_path = os.path.join(path, 'map.js') reduce_path = os.path.join(path, 'reduce.js') with open(map_path, 'rb') as f: map = f.read() if os.path.isfile(reduce_path): with open(reduce_path, 'rb') as f: reduce = f.read() else: reduce = None return design.ViewDefinition(doc_name, view_name, map, reduce)
def view_unprocessed_raw(db): map_fnc = """function(doc) { if (!doc.processed) { emit(doc._id, null); } }""" view = design.ViewDefinition('raw_tweets', 'unprocessed', map_fnc) view.sync(db)
def test_retrieve_view_defn(self): '''see issue 183''' view_def = design.ViewDefinition('foo', 'bar', 'baz') result = view_def.sync(self.db) self.assertTrue(isinstance(result, list)) self.assertEqual(result[0][0], True) self.assertEqual(result[0][1], '_design/foo') doc = self.db[result[0][1]] self.assertEqual(result[0][2], doc['_rev'])
def view_unprocessed_raw(db): map_fnc = '''function(doc) { if (!doc.username) { emit(doc._id, null); } }''' view = design.ViewDefinition('original_tweets', 'username_not_used', map_fnc) view.sync(db)
def view_uncollected_data(dataset): map_fnc = """function(doc) { if (!doc.collected) { emit(doc._id, null); } }""" view = design.ViewDefinition('processed_data', 'uncollected', map_fnc) view.sync(dataset)
def test_options(self): options = {'collation': 'raw'} view = design.ViewDefinition('foo', 'foo', 'function(doc) {emit(doc._id, doc._rev)}', options=options) _, db = self.temp_db() view.sync(db) design_doc = db.get('_design/foo') self.assertTrue(design_doc['views']['foo']['options'] == options)
def create_countTweets_view(db): view = design.ViewDefinition( 'counting', 'countdata', """ function (doc) { emit([doc.location,doc.time.slice(doc.time.length-4)], 1); } """, '''\ _count''') # if not view.get_doc(db): view.sync(db)
def create_message_view(db): from couchdb import design view = design.ViewDefinition('kombu', 'messages', """ function (doc) { if (doc.queue && doc.payload) emit(doc.queue, doc); } """) if not view.get_doc(db): view.sync(db)
def create_unemployment_view(db): view = design.ViewDefinition( 'views', 'data', """ function (doc) { if (doc.city) { emit(doc.city, {city :doc.city,year:doc.year,rate:doc.avg_unemp_rate}); } } """) if not view.get_doc(db): view.sync(db)
def create_income_view(db): view = design.ViewDefinition( 'views', 'data', """ function (doc) { if (doc.city) { emit(doc.city, {city :doc.city,year:doc.year,mean:doc.mean_income_yr}); } } """) if not view.get_doc(db): view.sync(db)
def view_unprocessed_raw(db): """Create a view of all unprocessed tweets in raw tweets db.""" map_fnc = """function(doc) { if (!doc.processed) { emit(doc._id, null); } }""" view = design.ViewDefinition( 'raw_tweets', 'unprocessed', map_fnc ) view.sync(db)
def create_realtimedata_view(db): view = design.ViewDefinition( 'data', 'realtime', """ function (doc) { emit(doc.location, {compound:doc.sentiment.compound,negative:doc.sentiment.neg, neutral:doc.sentiment.neu,positive:doc.sentiment.pos },true); } """, '''\ _sum''') # if not view.get_doc(db): view.sync(db)
def create_sentiment_view(db): view = design.ViewDefinition( 'data', 'alldata', """ function (doc) { emit([doc.location,doc.time.slice(doc.time.length-4)], {compound:doc.sentiment.compound,negative:doc.sentiment.neg, neutral:doc.sentiment.neu,positive:doc.sentiment.pos },true); } """, '''\ _sum''') # if not view.get_doc(db): view.sync(db)
def create_default_views(self): """ Create default views to get all docs with all fields. :return: """ # view 1: views to get all docs with all fields view1_map = "function (doc) { emit(doc._id, doc) }" view1 = design.ViewDefinition(design="example", name="get_all", map_fun=view1_map) view1.sync(self.couch_db_connector) # view 2: views to get count of all docs view2_map = "function (doc) { emit(doc._id, 1) }" view2_reduce = "function(keys, values) { return sum(values) }" view2 = design.ViewDefinition(design="example", name="get_all_count", map_fun=view2_map, reduce_fun=view2_reduce) view2.sync(self.couch_db_connector)
def createview(db_name, design_name, view_name, mapfunc, reducefuc): if db_name in couchserver: db = couchserver[db_name] print('db exist') else: db = couchserver.create(db_name) print('db NOT exist') view = design.ViewDefinition( '_design/'+design_name, #design document view_name, #view name mapfunc, #map reduce_fun = reducefuc) print (view.sync(db))
def create_views_all(db): map_tweets = map_function_all reduce_senti = reduce_function view = design.ViewDefinition('user_senti', 'mapreduce', map_tweets, reduce_senti) view.sync(db)
def create_view(db, design_name, view_name, map_function, reduce_function): view = design.ViewDefinition(design_name, view_name, map_function, reduce_function) view.sync(db)
def create_views_year(db): view = design.ViewDefinition('time_senti', 'mapreduce', map_function_year, reduce_function) view.sync(db)
def create_views(db_name, design_name, view_name, map_fun, reduce_fun): view = design.ViewDefinition(design_name, view_name, map_fun, reduce_fun=reduce_fun) view.sync(db_name)