Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Save data in google cloud datastore emulator

I am trying to test google's cloud datastore locally. My test steps are:

  1. Crawl data using Scrapy
  2. Save data to the cloud datastore using local emulator

I followed all the steps in order to use the local emulator

  1. start local emulator: gcloud beta emulators datastore start
  2. set local environment variables: gcloud beta emulators datastore env-init

However, in python, when use the following command to access cloud datastore, it always save the data directly to google cloud instead of saving them to the local emulators

#Imports the Google Cloud client library
from google.cloud import datastore

# Instantiates a client
datastore_client = datastore.Client()

sample_entry = some_data

# Saves the entity
datastore_client.put(sample_entry)

It seems like you cannot specify the library to use the local datastore emulator, just like what they offer in their Node.js client

var datastore = gcloud.datastore({
        apiEndpoint: "http://localhost:8380"
});

My question is, How can I ask the google cloud datastore python library to use local emulator instead of using the cloud directly

like image 826
JSNoob Avatar asked May 18 '17 14:05

JSNoob


People also ask

Is Google Datastore deprecated?

Because Cloud Datastore API v1 is released, Cloud Datastore API v1beta3 is now deprecated.

What is Datastore emulator?

The Datastore emulator provides local emulation of the production Datastore environment. You can use the emulator to develop and test your application locally. In addition, the emulator can help you generate indexes for your production Datastore instance and delete unneeded indexes.

Is Datastore transactional?

A transaction is a set of Datastore operations on one or more entities in up to 25 entity groups. Each transaction is guaranteed to be atomic, which means that transactions are never partially applied. Either all of the operations in the transaction are applied, or none of them are applied.


1 Answers

You need to eval $(gcloud beta emulators datastore env-init).

gcloud beta emulators datastore env-init only prints the commands that set the necessary environment variables.

like image 113
brocoli Avatar answered Oct 11 '22 17:10

brocoli