[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eThe Datastore emulator allows for local development and testing, simulating the production Datastore environment.\u003c/p\u003e\n"],["\u003cp\u003eThe emulator can be installed via the \u003ccode\u003egcloud components install cloud-datastore-emulator\u003c/code\u003e command and requires Java JRE 11 or greater and the Google Cloud CLI.\u003c/p\u003e\n"],["\u003cp\u003eTo start the emulator, use \u003ccode\u003egcloud emulators datastore start\u003c/code\u003e, and environment variables need to be set for applications to connect to it, either automatically or manually.\u003c/p\u003e\n"],["\u003cp\u003eThe emulator has behaviors that differ from Firestore in Datastore mode, such as simulating eventual consistency, however, some restrictions can be loosened with the \u003ccode\u003e--use-firestore-in-datastore-mode\u003c/code\u003e flag, and \u003ccode\u003egcloud emulators firestore start --database-mode=datastore-mode\u003c/code\u003e should be used to properly emulate it.\u003c/p\u003e\n"],["\u003cp\u003eAfter finishing with the emulator, environment variables must be removed for the application to reconnect to the production Datastore database.\u003c/p\u003e\n"]]],[],null,["# Running the Datastore Emulator\n\n| **Note:** This content applies to the emulator for legacy Cloud Datastore. To emulate Firestore in Datastore mode, [use `gcloud emulators firestore start --database-mode=datastore-mode`](/datastore/docs/emulator) instead.\n\nThe Datastore emulator provides local emulation of the production\nDatastore environment.\nYou can use the emulator to develop and\ntest your application locally. In addition, the emulator can help you\ngenerate indexes for your production Datastore instance and\ndelete unneeded indexes. This page guides you through installing the emulator,\nstarting the emulator, and setting environment variables to connect your\napplication to the emulator.\n| **Note:** To access the emulator from App Engine, see [Using the Local Development Server](/appengine/docs/standard/python/tools/using-local-server#using_the_local_datastore).\n\nKnown issues\n------------\n\nBy default, the Datastore emulator does not emulate\n[features introduced by Firestore in Datastore mode](/datastore/docs/firestore-or-datastore#in_datastore_mode).\nThe following default emulator behaviors do not match Datastore mode:\n\n- The emulator simulates eventual consistency, by default. Firestore in Datastore mode is strongly consistent.\n- The emulator does not allow non-ancestor queries within transactions. Firestore in Datastore mode no longer has this limitation.\n- The emulator does not support `IN`, `!=`, and `NOT-IN` queries.\n- The emulator does not support aggregation queries like `COUNT(*)`.\n\nHowever, the [--use-firestore-in-datastore-mode](/sdk/gcloud/reference/beta/emulators/datastore/start)\nflag helps loosen some of the restrictions above for Firestore in Datastore Mode.\n\n- The emulator simulates strongly consistent non-ancestor queries.\n- The emulator allows for non-ancestor queries within transactions.\n- The emulator removes the limitation of 25 entity groups in a transaction.\n\nTo emulate Firestore in Datastore mode,\n[use `gcloud emulators firestore start --database-mode=datastore-mode`](/datastore/docs/emulator)\ninstead.\n\nBefore you begin\n----------------\n\nTo use the Datastore emulator you need:\n\n- A Java JRE (version 21 or greater)\n- The [Google Cloud CLI](/sdk)\n- An application built using the [Google Cloud Client Libraries](/sdk/cloud-client-libraries)\n\nInstalling the emulator\n-----------------------\n\nThe Datastore emulator is a component of the gcloud CLI.\nUse the [`gcloud components install`](/sdk/gcloud/reference/components/install) command to install the\nDatastore emulator: \n\n gcloud components install cloud-datastore-emulator\n\nEmulator data directories\n-------------------------\n\nThe emulator simulates Datastore by creating\n`/WEB-INF/appengine-generated/local_db.bin` in a specified data directory and\nstoring data in `local_db.bin`. By default, the emulator uses the\ndata directory `~/.config/gcloud/emulators/datastore/`.\nThe `local_db.bin` file persists between sessions of the emulator. You can set\nup multiple data directories and think of each as a separate, local\nDatastore mode instance. To clear the contents of a `local_db.bin`\nfile, stop the emulator and manually delete the file.\n\nStarting the emulator\n---------------------\n\nStart the emulator by executing [`datastore start`](/sdk/gcloud/reference/beta/emulators/datastore/start) from a command prompt: \n\n gcloud beta emulators datastore start [flags]\n\nwhere `[flags]` are optional command-line arguments supplied to the\ngcloud CLI. For example:\n\n- [`--data-dir=[DATA_DIR]`](/sdk/gcloud/reference/beta/emulators/datastore/start) changes the emulator's data\n directory. The emulator creates the\n `/WEB-INF/appengine-generated/local_db.bin` file inside `[DATA_DIR]`\n or, if available, uses an existing file.\n\n- [`--no-store-on-disk`](/sdk/gcloud/reference/beta/emulators/datastore/start) configures the emulator not to persist any\n data to disk for the emulator session.\n\nSee the [`gcloud beta emulators datastore start`](/sdk/gcloud/reference/beta/emulators/datastore/start) reference for the full\nlist of optional flags.\n\nAfter you start the emulator, you should see a message similar\nto the following: \n\n ...\n [datastore] Dev App Server is now running.\n\nTo stop the emulator, type Control-C at the command prompt.\n\nSetting environment variables\n-----------------------------\n\nAfter you start the emulator, you need to set environment variables so that your\napplication connects to the emulator instead of your production\nDatastore mode database. Set these environment variables on the\nsame machine that you use to run your application.\n\nYou need to set the environment variables each time you start the emulator.\nThe environment variables depend on dynamically assigned port numbers that\ncould change when you restart the emulator.\n| **Note:** If you use the .NET client library, [use this method to read the\n| environment variables and connect to the emulator](https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Datastore.V1/#using-the-datastore-emulator).\n\n### Automatically setting the variables\n\nIf your application and the emulator run on the same machine, you can set the\nenvironment variables automatically: \n\n### Linux / macOS\n\nRun `env-init` using command substitution: \n\n $(gcloud beta emulators datastore env-init)\n\n### Windows\n\nCreate and run a batch file using output from `env-init`: \n\n gcloud beta emulators datastore env-init \u003e set_vars.cmd && set_vars.cmd\n\nYour application will now connect to the Datastore emulator.\n\n### Manually setting the variables\n\nIf your application and the emulator run on different machines, set the\nenvironment variables manually:\n\n1. Run the `env-init` command:\n\n ```bash\n gcloud beta emulators datastore env-init\n ```\n2. On the machine that runs your application, set the environment\n variables and values as directed by the output\n of the `env-init` command. For example:\n\n Linux / macOS \n\n ```bash\n export DATASTORE_DATASET=my-project-id\n export DATASTORE_EMULATOR_HOST=::1:8432\n export DATASTORE_EMULATOR_HOST_PATH=::1:8432/datastore\n export DATASTORE_HOST=http://::1:8432\n export DATASTORE_PROJECT_ID=my-project-id\n ```\n Windows \n\n ```bash\n set DATASTORE_DATASET=my-project-id\n set DATASTORE_EMULATOR_HOST=::1:8432\n set DATASTORE_EMULATOR_HOST_PATH=::1:8432/datastore\n set DATASTORE_HOST=http://::1:8432\n set DATASTORE_PROJECT_ID=my-project-id\n ```\n\nYour application will now connect to the Datastore emulator. Note\nthat the project id and port provided by the command will differ from the above\nexample.\n\nUpdating and deleting indexes\n-----------------------------\n\nBy running your application using the emulator, you can generate indexes for\nyour production Datastore mode database, as well as delete unneeded\nindexes. Learn more at [Using the gcloud CLI](/datastore/docs/tools).\n\nRemoving environment variables\n------------------------------\n\nAfter you finish using the emulator, stop the emulator (Control-C) and remove\nthe environment variables so your application will connect to your production\nDatastore mode database.\n\n### Automatically removing the variables\n\nIf your application and the emulator run on the same machine, you can remove the\nenvironment variables automatically: \n\n### Linux / macOS\n\nRun `env-unset` using command substitution: \n\n $(gcloud beta emulators datastore env-unset)\n\n### Windows\n\nCreate and run a batch file using output from `env-unset`: \n\n gcloud beta emulators datastore env-unset \u003e remove_vars.cmd && remove_vars.cmd\n\nYour application will now connect to your production Datastore mode\ndatabase.\n\n### Manually removing the variables\n\nIf your application and the emulator run on different machines,\nremove the environment variables manually:\n\n1. Run the `env-unset` command:\n\n ```bash\n gcloud beta emulators datastore env-unset\n ```\n2. On the machine that runs your application, remove the environment\n variables as directed by the output of the `env-unset` command.\n For example:\n\n Linux / macOS \n\n ```bash\n unset DATASTORE_DATASET\n unset DATASTORE_EMULATOR_HOST\n unset DATASTORE_EMULATOR_HOST_PATH\n unset DATASTORE_HOST\n unset DATASTORE_PROJECT_ID\n ```\n Windows \n\n ```bash\n set DATASTORE_DATASET=\n set DATASTORE_EMULATOR_HOST=\n set DATASTORE_EMULATOR_HOST_PATH=\n set DATASTORE_HOST=\n set DATASTORE_PROJECT_ID=\n ```\n\nYour application will now connect to your production Datastore mode\ndatabase."]]