use of com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub in project grpc-gcp-java by GoogleCloudPlatform.
the class CreateDocument method createDocumentCall.
public void createDocumentCall() {
System.out.println("\n:: Creating New Document ::\n");
FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
Scanner sc = new Scanner(System.in);
System.out.print("Enter Document Name: ");
String docName = sc.next();
HashMap<String, Value> fieldsMap;
MakeFieldsMap mfm = new MakeFieldsMap();
fieldsMap = mfm.makeFieldsMap();
Document newDoc = Document.newBuilder().putAllFields(fieldsMap).build();
CreateDocumentRequest createDocumentRequest = CreateDocumentRequest.newBuilder().setDocument(newDoc).setCollectionId("GrpcTestData").setParent("projects/firestoretestclient/databases/(default)/documents").setDocumentId(docName).build();
Document finishedDoc;
try {
finishedDoc = blockingStub.createDocument(createDocumentRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
DrawDocument dd = new DrawDocument();
dd.draw(finishedDoc);
Menu menu = new Menu();
menu.draw();
}
use of com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub in project grpc-gcp-java by GoogleCloudPlatform.
the class Write method writeCall.
public void writeCall() {
System.out.println(":: Starting Write Stream ::");
FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
FirestoreStub firestoreStub = new GRPCFirebaseClientFactory().createFirebaseClient().getFirestoreStub();
Scanner sc = new Scanner(System.in);
System.out.print("Enter document name: ");
String docName = sc.next();
GetDocumentRequest getDocumentRequest = GetDocumentRequest.newBuilder().setName("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + docName).build();
Document doc;
try {
doc = blockingStub.getDocument(getDocumentRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
// Retrieve initial stream token and stream id
WriteRequest writeRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").build();
StreamObserver<WriteResponse> writeResponseStreamObserver = new StreamObserver<WriteResponse>() {
@Override
public void onNext(WriteResponse writeResponse) {
Write.streamToken = writeResponse.getStreamToken();
Write.streamId = writeResponse.getStreamId();
}
@Override
public void onError(Throwable throwable) {
System.out.println(throwable.getMessage() + throwable.getCause());
}
@Override
public void onCompleted() {
}
};
StreamObserver<WriteRequest> writeRequestStreamObserver = firestoreStub.write(writeResponseStreamObserver);
writeRequestStreamObserver.onNext(writeRequest);
String fieldName = "";
String fieldValue = "";
while (!fieldName.matches("DONE")) {
System.out.print("Field Name (Enter DONE to quit): ");
fieldName = sc.next();
if (!fieldName.matches("DONE")) {
System.out.print("Field Value: ");
fieldValue = sc.next();
Value fsValue = Value.newBuilder().setStringValue(fieldValue).build();
doc = doc.toBuilder().putFields(fieldName, fsValue).build();
DocumentMask docMask = DocumentMask.newBuilder().addFieldPaths(fieldName).build();
com.google.firestore.v1beta1.Write currentWrite = com.google.firestore.v1beta1.Write.newBuilder().setUpdate(doc).setUpdateMask(docMask).build();
WriteRequest currentWriteRequest = WriteRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").setStreamToken(Write.streamToken).setStreamId(Write.streamId).addWrites(currentWrite).build();
writeRequestStreamObserver.onNext(currentWriteRequest);
}
}
writeRequestStreamObserver.onCompleted();
System.out.println("Finished streaming writes!");
Menu menu = new Menu();
menu.draw();
}
use of com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub in project grpc-gcp-java by GoogleCloudPlatform.
the class Rollback method rollbackCall.
public void rollbackCall() {
System.out.println("\n:: Rolling Back Transaction ::\n");
if (Main.transactionId == null) {
System.out.println("WARNING: No current transaction open, run BeginTransaction first...");
return;
} else {
System.out.println("Found Transaction ID '" + Main.transactionId.toString() + "'. Committing....");
}
FirestoreBlockingStub blockingStub = new GRPCFirebaseClientFactory().createFirebaseClient().getBlockingStub();
RollbackRequest rollbackRequest = RollbackRequest.newBuilder().setTransaction(Main.transactionId).setDatabase("projects/firestoretestclient/databases/(default)").build();
try {
blockingStub.rollback(rollbackRequest);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
return;
}
System.out.println("Success!");
Menu menu = new Menu();
menu.draw();
}
Aggregations