繁体   English   中英

使用spring数据连接到数据存储谷歌云

[英]use spring data to connect to datastore google cloud

我如何使用Spring Data以连接到Google的DataStore,实际上我使用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;

  }

任何帮助将不胜感激

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

暂无
暂无

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

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