# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Let's start by importing the demo module and initializing our client. from gcloud import datastore from gcloud.datastore import demo client = datastore.Client(project=demo.PROJECT) # Let's create a new entity of type "Thing" and name it 'Toy': key = client.key('Thing') toy = datastore.Entity(key) toy.update({'name': 'Toy'}) # Now let's save it to our datastore: client.put(toy) # If we look it up by its key, we should find it... print(client.get(toy.key)) # And we should be able to delete it... client.delete(toy.key)
def get_client(): if not hasattr(DatastoreClient, "_client"): DatastoreClient._client = datastore.Client() return DatastoreClient._client
import json from datetime import datetime, timedelta # Third-party app imports import requests import certifi from requests.packages.urllib3.exceptions import InsecureRequestWarning from elasticsearch import Elasticsearch, helpers from gcloud import datastore # Elasticsearch ELASTICSEARCH_USER = os.environ['NEWSAI_ELASTICSEARCH_USER'] ELASTICSEARCH_PASSWORD = os.environ['NEWSAI_ELASTICSEARCH_PASSWORD'] # Setup datastore connection for Google Cloud client = datastore.Client('newsai-1166') # Removing requests warning urllib3.disable_warnings() requests.packages.urllib3.disable_warnings(InsecureRequestWarning) # Elasticsearch setup es = Elasticsearch( ['https://search1.newsai.org'], http_auth=(ELASTICSEARCH_USER, ELASTICSEARCH_PASSWORD), port=443, use_ssl=True, verify_certs=True, ca_certs=certifi.where(), )
def remove_all_entities(client=None): if client is None: # Get a client that uses the test dataset. client = datastore.Client(dataset_id=TESTS_DATASET) for kind in ALL_KINDS: remove_kind(kind, client)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from gcloud import datastore, pubsub import psq import tasks PROJECT_ID = 'your-project-id' pubsub_client = pubsub.Client(project=PROJECT_ID) datastore_client = datastore.Client(project=PROJECT_ID) q = psq.Queue( pubsub_client, storage=psq.DatastoreStorage(datastore_client)) def main(): q.enqueue(tasks.slow_task) q.enqueue(tasks.print_task, "Hello, World") r = q.enqueue(tasks.adder, 1, 5) print(r.result(timeout=10)) if __name__ == '__main__': main()
def setUp(self): if 'prod' in config.PROJECT_ID: raise RuntimeError('Cowardly refusing to delete prod datastore') self.datastore_client = datastore.Client(config.PROJECT_ID) test_common._clear_data(self.datastore_client)
from gcloud import datastore dataset = datastore.Client(project='thedatalabproject') entity = datastore.Entity(key=dataset.key('Greeting')) entity['message'] = 'Hello, world!' dataset.put(entity)
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Script to populate datastore with system test data.""" from six.moves import zip from gcloud import datastore from gcloud.datastore import client client._DATASET_ENV_VAR_NAME = 'GCLOUD_TESTS_DATASET_ID' CLIENT = datastore.Client() ANCESTOR = ('Book', 'GoT') RICKARD = ANCESTOR + ('Character', 'Rickard') EDDARD = RICKARD + ('Character', 'Eddard') KEY_PATHS = [ RICKARD, EDDARD, ANCESTOR + ('Character', 'Catelyn'), EDDARD + ('Character', 'Arya'), EDDARD + ('Character', 'Sansa'), EDDARD + ('Character', 'Robb'), EDDARD + ('Character', 'Bran'), EDDARD + ('Character', 'Jon Snow'), ] CHARACTERS = [
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Let's start by importing the demo module and initializing our client. from gcloud import datastore from gcloud.datastore import demo client = datastore.Client(dataset_id=demo.DATASET_ID) # Let's create a new entity of type "Thing" and name it 'Toy': key = client.key('Thing') toy = datastore.Entity(key) toy.update({'name': 'Toy'}) # Now let's save it to our datastore: client.put(toy) # If we look it up by its key, we should find it... print(client.get(toy.key)) # And we should be able to delete it... client.delete(toy.key)
from gcloud import datastore import os # the location of the JSON file on your local machine os.environ[ "GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/anandjain/Documents/GitHub/anand-bq-test-2-04cfb06c492d.json" # project ID from the Developers Console projectID = "anand-bq-test-2" os.environ["GCLOUD_TESTS_PROJECT_ID"] = projectID os.environ["GCLOUD_TESTS_DATASET_ID"] = projectID client = datastore.Client(project=projectID) query = client.query(kind='Job').fetch() for r in query: print(r)
'https://www.googleapis.com/auth/compute', 'https://www.googleapis.com/auth/datastore', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/devstorage.read_write' ] # This try except is needed to allow CI testing to pass - it does not properly set the # variables because there are not appropriate service credentials try: service_credentials = ServiceAccountCredentials.from_json_keyfile_dict( settings.GOOGLE_CREDENTIALS, scopes=scopes) http_compute = service_credentials.authorize(Http()) # Create clients for gcloud compatible Google Cloud Services gce_service = build('compute', API_VERSION, http=http_compute) datastore_client = datastore.Client(credentials=service_credentials, project=PROJECT_ID) storage_client = storage.Client(credentials=service_credentials, project=PROJECT_ID) default_bucket = storage_client.get_bucket(DEFAULT_BUCKET_NAME) default_job_bucket = storage_client.get_bucket(DEFAULT_JOB_BUCKET_NAME) except ValueError: import mock service_credentials = object() http_compute = object() gce_service = object() datastore_client = object() storage_client = object() default_bucket = mock.Mock() mockblob = mock.MagicMock() mockblob.name = 'foo-0.16.0.whl' default_bucket.list_blobs = mock.MagicMock(return_value=[mockblob],
from gcloud import datastore from google.appengine.api import app_identity __client = datastore.Client(app_identity.get_application_id()) def add_location(user_id, user_location): with __client.transaction(): key = __client.key("Location", user_id) location = __client.get(key) if location: location["latitude"] = user_location["latitude"] location["longitude"] = user_location["longitude"] else: location = datastore.Entity(key) location.update({ "user_id": user_id, "latitude": user_location["latitude"], "longitude": user_location["longitude"], }) __client.put(location) return location.key def get_location(user_id): query = __client.query(kind='Location') query.add_filter('user_id', '=', user_id)
def remove_all_entities(client=None): if client is None: # Get a client that uses the test dataset. client = datastore.Client(project=os.getenv(TESTS_PROJECT)) for kind in ALL_KINDS: remove_kind(kind, client)
def get_client(): return datastore.Client( dataset_id=current_app.config['DATASTORE_DATASET_ID'])
def setUp(self): super(DatastoreTasksTest, self).setUp() self.client = datastore.Client(self.project_id)
def get_client(): return datastore.Client(current_app.config['PROJECT_ID'])
from gcloud import datastore import datetime client = datastore.Client() def add_user(name, passwort, email): key = client.key('User') user = datastore.Entity(key) user.update({ 'created': datetime.datetime.utcnow(), 'name': name, 'password': passwort, 'email': email }) client.put(user) return user.key def get_user(name, password): query = client.query(kind='User') query.add_filter('name', '=', name) query.add_filter('password', '=', password) query = list(query.fetch()) return query def add_new_post(subject, content): key = client.key('Blog') blog = datastore.Entity(key) blog.update({
def GetEntry(cls, entry_link): link_hash = hashlib.sha1(entry_link).hexdigest() gclient = datastore.Client() entry_key = gclient.key(config.ENTRY_ENTITY, link_hash) return gclient.get(entry_key)
import logging import multiprocessing import time from gcloud import datastore from gcloud_requests.connection import datastore_connection from threading import Thread logging.basicConfig(level=logging.DEBUG) client = datastore.Client(http=datastore_connection.http) ns = "gcloud-requests-{}".format(time.time()) forms = client.query(kind="Form", namespace=ns).fetch() for form in forms: client.delete(form.key) for i in range(100): e = datastore.Entity(key=client.key("Form", namespace=ns)) e.update(x=i) client.put(e) def r(): forms = client.query(kind="Form", namespace=ns).fetch() for form in forms: logging.info(str(form)) client.put(form)
def create_client(self, project_id): return datastore.Client(project_id)
from datetime import date, datetime from flask import Flask, render_template, request, redirect, url_for, flash from gcloud import datastore from flask_bootstrap import Bootstrap # from wyncopy.models import query from models import query # プロジェクトID project_id = "web-yacht-note-208313" # DataStoreに接続するためのオブジェクトを作成 client = datastore.Client(project_id) # アプリケーションを作成 app = Flask(__name__) bootstrap = Bootstrap(app) @app.route('/') def top(): """ TOPページを表示したときの挙動 """ # 練習ノートの一覧を取得 query = client.query(kind='Outline') outline_list = list(query.fetch()) # 本日の日付を取得 today = date.today()