使用spring数据连接到数据存储区google云

时间:2015-04-03 08:18:17

标签: java spring spring-data google-cloud-datastore

如何使用Spring Data连接到DataStore google,实际上我使用的是com.google.api.services.datastore.DatastoreV1

但是我的主管经理想要使用spring-Data和dataStore我该怎么做?

例如插入实际使用的实体:

    public void insert(Entity entity) {
    Datastore datastore = this.datastoreFactory.getInstance();
    CommitRequest request = 
    CommitRequest.newBuilder().setMode(CommitRequest.Mode.NON_TRANSACTIONAL)

   .setMutation(Mutation.newBuilder().addInsertAutoId(entity)).build();
    try {
        CommitResponse response = datastore.commit(request);
    } catch (DatastoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}




   @Override
   @SuppressWarnings("deprecation")
   public Datastore getInstance() {

    if(datastore != null)
        return datastore;
    try {
        // Setup the connection to Google Cloud Datastore and infer
        // credentials
        // from the environment.
        //the environment variables DATASTORE_SERVICE_ACCOUNT and    
        DATASTORE_PRIVATE_KEY_FILE must be set
        datastore = DatastoreFactory.get().create(

       DatastoreHelper.getOptionsfromEnv().dataset(Constant.ProjectId)
                        .build());



      } catch (GeneralSecurityException exception) {
        System.err.println("Security error connecting to the datastore: "
                + exception.getMessage());
        return null;
    } catch (IOException exception) {
        System.err.println("I/O error connecting to the datastore: "
                + exception.getMessage());
        return null;
    }
    return datastore;

  }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

要将Spring Data与特定存储一起使用,您需要从Spring Data Commons实现一堆接口。以GCP Spanner Spring Data实现为例(https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-data-spanner

相关问题