简体   繁体   English

Google云数据存储模拟器初始化数据

[英]Google cloud datastore emulator init data

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

but couldn't make it work. 但无法使其发挥作用。

Should I write a standalone js/python script that fills the database programatically ? 我应该编写一个以编程方式填充数据库的独立js / python脚本吗?

Please advise 请指教

The emulator creates a "Datastore" working on your local machine that basically emulates the behavior as if it was the Google Cloud Datastore itself. 模拟器在本地计算机上创建“数据存储区”,基本上模拟行为,就像它是Google Cloud Datastore本身一样。

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 : 例如,如果你使用这个python-datastore github repo

the code inserts entities of every "user's ip" & "timestamp" when they visited your app, and then queries last 10 visits: 代码在访问您的应用时插入每个“用户的IP”和“时间戳”的实体,然后查询最近10次访问:

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))

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: 如果您停止模拟器然后再次运行它,您将看到如下内容:

Reusing existing data in [/tmp/tmp.(whatever)/emulators/datastore] 重用[/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 所以你将能够继续使用相同的数据,除非你删除它或更改模拟器的数据目录更改--data-dir标志

If you run the following command: 如果您运行以下命令:

gcloud datastore export gcloud数据存储区导出

First of all you are missing the OUTPUT_URL_PREFIX ; 首先,你缺少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 . 有一种方法可以将您的生产数据存储区导出到本地数据存储区,如下所示,您可以在@ Olivier.Roger和@stanzheng的答案中看到并编辑 You have to follow these steps: 您必须遵循以下步骤:

1.Deploy some App which is runnning using the remote_api . 1.部署一些使用remote_api运行的应用程序。 For example this repo is a straightforward way. 例如, 这个回购是一种直截了当的方式。

2.Run this command to download your datastore in production to the file data.csv: 2.运行此命令将生产中的数据存储区下载到文件data.csv:

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

3.Start the datastore emulator: 3.启动数据存储模拟器:

gcloud beta emulators datastore start

4.Run the Local Development Server with the same remote_api repo than before . 4. 使用之前相同的remote_api repo运行 本地开发服务器 When you run this you will see something like: 当你运行它时,你会看到类似的东西:

Starting API server at: http://0.0.0.0:39693 启动API服务器:http: ///0.0.0.039693

Use this last port(39693) in the following step 在以下步骤中使用此最后一个端口(39693)

5.Run the following command: 5.运行以下命令:

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

In the last step what you are actually doing is the following: You are uploading the data.csv to your App running in local. 在最后一步中,您实际执行的操作如下:您正在将data.csv上传到在本地运行的应用程序。 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. 考虑到您还在运行数据存储模拟器,您在本地运行的应用程序已连接到它,因此您将data.csv上传到本地数据存储区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM