コード例 #1
0
from cloudant.client import CouchDB
from cloudant.design_document import DesignDocument
from cloudant.view import View

#connect couchDB
client = CouchDB("admin", "password", url='http://172.26.131.173:5984', connect=True)

session = client.session()
#print('Username: {0}'.format(session['userCtx']['name'])) 
#print('Databases: {0}'.format(client.all_dbs()))

#connect covidsafe database in the couchDB
covidsafe_db=client['tweet-covid-covidsafe']

#Create a covidsafe designdocument for views
covidsafe_ddoc = DesignDocument(covidsafe_db,'covidsafe')

#Create a mapreduce function to group the documents by sentiment and location
covidsafe_ddoc.add_view('sentiment_location','function(doc){\n emit([doc.sentiment,doc.SA4],1)}','_count')
covidsafe_ddoc.save()

#connect covid database in the couchDB
covid_db=client['tweet-covid']

#Create a designdocument for views
covid_ddoc = DesignDocument(covid_db,'covid')

#Create a mapreduce function to group the documents by sentiment and location
covid_ddoc.add_view('sentiment_location','function(doc){\n emit([doc.sentiment,doc.SA4],1)}','_count')
covid_ddoc.save()
            coordinate = feature['geometry']['coordinates'][j]
            for k in range(len(coordinate[0])):
                try:
                    point = coordinate[0][
                        k]  # point[0] : longitude, point[1] : latitude
                    cur_distance = math.sqrt(
                        (input_geo_point[0] - point[0])**2 +
                        (input_geo_point[1] - point[1])**2)
                    if cur_distance < min_distance:
                        sa4_code = cur_sa4_code
                        min_distance = cur_distance
                except TypeError:
                    pass
    return -1 if min_distance > 10 else int(sa4_code)


for tweet in data['features']:

    a = check_SA4_with_geo(tweet)
    doc = {'_id': str(tweet['properties']['id']), 'SA4': a}
    doc.update(tweet)
    db.create_document(doc)

ddoc = DesignDocument(db, 'myhospital')
ddoc.add_view('sentiment_location', 'function(doc){\n emit([doc.SA4],1)}',
              '_count')
try:
    ddoc.save()
except Exception as e:
    print('Connection unsuccessful')