vendredi 3 avril 2015

use spring data to connect to datastore google cloud

How can i use Spring Data in order to connect to DataStore google, actually i use com.google.api.services.datastore.DatastoreV1


But my lead Manager want use spring-Data with that how can i do that?


for example to insert an Entity:



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;

}


any help will be appreciated


Aucun commentaire:

Enregistrer un commentaire