Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置数据存储,以便通过非应用程序引擎Java应用程序访问本地计算机_Java_Google Cloud Datastore_Google Cloud Platform - Fatal编程技术网

如何设置数据存储,以便通过非应用程序引擎Java应用程序访问本地计算机

如何设置数据存储,以便通过非应用程序引擎Java应用程序访问本地计算机,java,google-cloud-datastore,google-cloud-platform,Java,Google Cloud Datastore,Google Cloud Platform,我在java应用程序(非应用程序引擎)中从本地机器访问google云数据存储时遇到问题 当我试图用下面的代码访问数据存储时,我得到一个异常,说“数据存储数据集未在选项中设置”,这让一些人看到,因为它未在GoogleCredential中设置 System.setProperty("DATASTORE_DATASET", "xxxx-722"); String emailAddress = "xxxxxxx@developer.gserviceaccount.com"; Jso

我在java应用程序(非应用程序引擎)中从本地机器访问google云数据存储时遇到问题

当我试图用下面的代码访问数据存储时,我得到一个异常,说“数据存储数据集未在选项中设置”,这让一些人看到,因为它未在GoogleCredential中设置

System.setProperty("DATASTORE_DATASET", "xxxx-722");
     String emailAddress = "xxxxxxx@developer.gserviceaccount.com";
     JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

     GoogleCredential credential = new GoogleCredential.Builder()
         .setTransport(httpTransport)
         .setJsonFactory(JSON_FACTORY)
         .setServiceAccountId(emailAddress)
         .setServiceAccountPrivateKeyFromP12File(new File("D:\\Key\\xxxxx.p12"))
         .setServiceAccountScopes(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL))         
         .build();
如果我按照下面的代码做,我会得到一个例外,我没有使用任何凭据

System.setProperty("DATASTORE_SERVICE_ACCOUNT", "xxxxx@developer.gserviceaccount.com");
     System.setProperty("DATASTORE_PRIVATE_KEY_FILE", "D:\\Key\\xxxxxx.p12");
     System.setProperty("DATASTORE_DATASET", "xxxxxx-722");
     Datastore datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv().dataset("xxxxx-722").build()); //(credential).build());

因此,我的问题是如何正确设置数据存储对象,以便通过非应用程序引擎Java应用程序在本地计算机上访问它?非常感谢所有帮助。

请注意,
DatastoreHelper
查看的是环境变量,而不是系统属性

创建
数据存储
对象的最简单方法是设置三个环境变量:

export DATASTORE_DATASET=<dataset>
export DATASTORE_SERVICE_ACCOUNT=<service-account>
export DATASTORE_PRIVATE_KEY_FILE=<path-to-private-key-file>
如果需要以编程方式设置或覆盖某些选项,可以执行以下操作:

Datastore datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv()
    .dataset("<dataset>")
    .build())
Datastore Datastore=DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv())
.dataset(“”)
.build())
Datastore datastore = DatastoreFactory.get().create(DatastoreHelper.getOptionsfromEnv()
    .dataset("<dataset>")
    .build())