![](/img/trans.png)
[英]Connect to Google DataStore through Spring data and outside Gae
[英]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.