Google Cloud Datastore模拟器初始数据 [英] Google cloud datastore emulator init data

查看:118
本文介绍了Google Cloud Datastore模拟器初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Google Cloud Datastore模拟器用于本地项目.我已经使用:

I would like to use the google cloud datastore emulator for a local project. I have already installed and ran the emulator with :

gcloud beta emulators datastore start

我的应用程序连接到它,但是问题是我不知道如何用实体填充它,因为没有用户界面,并且我的应用程序需要一些管理员用户.

My app connects to it, but the problem is that I do not know how to fill it with entities since there is no user interface and my application requires some admin users to be present.

我还尝试使用以下命令导出生产数据库(数据存储):

I also tried to export the production database (datastore) with the following command:

 gcloud datastore export

但无法使其正常工作.

我应该编写一个以编程方式填充数据库的独立js/python脚本吗?

Should I write a standalone js/python script that fills the database programatically ?

请告知

推荐答案

模拟器会在您的本地计算机上创建一个数据存储区",基本上可以模拟该行为,就好像它是Google Cloud Datastore本身一样.

The emulator creates a "Datastore" working on your local machine that basically emulates the behavior as if it was the Google Cloud Datastore itself.

因此,如果您已经在运行模拟器,并且您的应用已连接到该模拟器,则只需使用连接到数据存储区的任何脚本,您就可以执行任何读/写操作.因此,例如,如果您使用此 python -datastore github存储库:

So, if you are already running the emulator and your app connects to it, simply using any script that connects to Datastore you will be able to perform any read/write operation. So for example, if you use this python-datastore github repo:

该代码插入每个用户的ip"的实体&他们访问您的应用时的时间戳记",然后查询最近10次访问:

the code inserts entities of every "user's ip" & "timestamp" when they visited your app, and then queries last 10 visits:

entity = datastore.Entity(key=ds.key('visit'))
entity.update({
    'user_ip': user_ip,
    'timestamp': datetime.datetime.utcnow()
})

ds.put(entity)

query = ds.query(kind='visit', order=('-timestamp',))

results = [
    'Time: {timestamp} Addr: {user_ip}'.format(**x)
    for x in query.fetch(limit=10)]

output = 'Last 10 visits:\n{}'.format('\n'.join(results))

因此,如果您正在使用仿真器运行App,则所有这些实体都将插入到本地并从那里查询.如果停止模拟器,然后再次运行它,将会看到类似以下内容的

So if you are running your App using the emulator, these all entities will be inserted in local and queried from there. If you stop the emulator and then run it again you will see something like:

重复使用[/tmp/tmp.(无论如何)/模拟器/数据存储区]中的现有数据

Reusing existing data in [/tmp/tmp.(whatever)/emulators/datastore]

,因此您将能够继续使用相同的数据,除非您将其擦除或更改模拟器的数据目录

so you will be able to keep using the same data unless you erase it or change the emulator's data directory changing the --data-dir flag

如果运行以下命令:

gcloud数据存储导出

gcloud datastore export

首先,您缺少OUTPUT_URL_PREFIX ;您的数据存储将导出到的位置.其次,该命令尚不具有与本地数据存储区一起使用的功能:您可以看到以下公共问题跟踪器已被请求的位置.

First of all you are missing the OUTPUT_URL_PREFIX; where your datastore will be exported. And second, this command doesn't have the functionality to work with local datastore yet:You can see the following public issue tracker where it has been already requested.

有一种方法可以将生产数据存储导出到本地存储,如您在

There is a way of exporting your Production datastore to your local one as you can see in the answer and edit by @Olivier.Roger and @stanzheng in the following thread. You have to follow these steps:

1.使用 remote_api .例如,此存储库是一种简单的方法.

1.Deploy some App which is runnning using the remote_api. For example this repo is a straightforward way.

2.运行以下命令将生产中的数据存储下载到文件data.csv:

2.Run this command to download your datastore in production to the file data.csv:

appcfg.py download_data -A YOUR_APP_NAME --url=http://YOUR_APP_NAME.appspot.com/_ah/remote_api/ --filename=data.csv

3.启动数据存储模拟器:

3.Start the datastore emulator:

gcloud beta emulators datastore start

4.运行本地开发服务器与以前相同的 remote_api回购.当您运行此程序时,您会看到类似以下内容的

4.Run the Local Development Server with the same remote_api repo than before. When you run this you will see something like:

在以下位置启动API服务器: http://0.0.0.0:39693

在后续步骤中使用最后一个端口(39693)

Use this last port(39693) in the following step

5.运行以下命令:

appcfg.py --url=http://localhost:39693/_ah/remote_api/ --filename=data.csv upload_data

在最后一步中,您实际要做的是:将data.csv上载到在本地运行的App中.考虑到您也在运行数据存储区模拟器,因此在本地运行的应用程序已连接到该应用程序,因此您要将data.csv上传到本地数据存储区.

In the last step what you are actually doing is the following: You are uploading the data.csv to your App running in local. Taking into account that you are also running the datastore emulator, your app running in local is connected to it, so you're uploading the data.csv to your local datastore.

这篇关于Google Cloud Datastore模拟器初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆