use of com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub in project grpc-gcp-java by GoogleCloudPlatform.
the class BatchGetDocuments method batchGetDocumentsCall.
public void batchGetDocumentsCall() {
List<String> docList = new ArrayList<String>();
System.out.println("\n :: Batch Retrieve Documents :: \n");
Scanner sc = new Scanner(System.in);
String input = "initial";
FirestoreGrpc.FirestoreStub firestoreStub = new GRPCFirebaseClientFactory().createFirebaseClient().getFirestoreStub();
DrawDocument dd = new DrawDocument();
while (!input.matches("DONE")) {
System.out.print("Enter Document Id (Enter DONE when finished): ");
input = sc.next();
if (!input.matches("DONE")) {
docList.add("projects/firestoretestclient/databases/(default)/documents/GrpcTestData/" + input);
}
}
BatchGetDocumentsRequest batchGetDocsRequest = BatchGetDocumentsRequest.newBuilder().setDatabase("projects/firestoretestclient/databases/(default)").addAllDocuments(docList).build();
final CountDownLatch finishLatch = new CountDownLatch(1);
StreamObserver respStream = new StreamObserver() {
@Override
public void onNext(Object resp) {
BatchGetDocumentsResponse response = (BatchGetDocumentsResponse) resp;
Document doc = response.getFound();
dd.draw(doc);
}
@Override
public void onError(Throwable throwable) {
System.out.println("Error During Call: " + throwable.getMessage());
finishLatch.countDown();
}
@Override
public void onCompleted() {
Menu menu = new Menu();
menu.draw();
finishLatch.countDown();
}
};
try {
firestoreStub.batchGetDocuments(batchGetDocsRequest, respStream);
finishLatch.await(1, TimeUnit.MINUTES);
} catch (Exception e) {
System.out.println("Error during call: " + e.getMessage() + e.getCause());
}
}
use of com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub 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();
}
Aggregations