Search in sources :

Example 1 with FirestoreStub

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());
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Scanner(java.util.Scanner) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Document(com.google.firestore.v1beta1.Document) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument) BatchGetDocumentsRequest(com.google.firestore.v1beta1.BatchGetDocumentsRequest) BatchGetDocumentsResponse(com.google.firestore.v1beta1.BatchGetDocumentsResponse) Menu(org.roguewave.grpc.util.gfx.Menu) FirestoreGrpc(com.google.firestore.v1beta1.FirestoreGrpc) DrawDocument(org.roguewave.grpc.util.gfx.DrawDocument)

Example 2 with FirestoreStub

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();
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GRPCFirebaseClientFactory(org.roguewave.grpc.util.GRPCFirebaseClientFactory) Scanner(java.util.Scanner) FirestoreBlockingStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub) FirestoreStub(com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub) ByteString(com.google.protobuf.ByteString) com.google.firestore.v1beta1(com.google.firestore.v1beta1) Menu(org.roguewave.grpc.util.gfx.Menu)

Aggregations

StreamObserver (io.grpc.stub.StreamObserver)2 Scanner (java.util.Scanner)2 GRPCFirebaseClientFactory (org.roguewave.grpc.util.GRPCFirebaseClientFactory)2 Menu (org.roguewave.grpc.util.gfx.Menu)2 com.google.firestore.v1beta1 (com.google.firestore.v1beta1)1 BatchGetDocumentsRequest (com.google.firestore.v1beta1.BatchGetDocumentsRequest)1 BatchGetDocumentsResponse (com.google.firestore.v1beta1.BatchGetDocumentsResponse)1 Document (com.google.firestore.v1beta1.Document)1 FirestoreGrpc (com.google.firestore.v1beta1.FirestoreGrpc)1 FirestoreBlockingStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreBlockingStub)1 FirestoreStub (com.google.firestore.v1beta1.FirestoreGrpc.FirestoreStub)1 ByteString (com.google.protobuf.ByteString)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DrawDocument (org.roguewave.grpc.util.gfx.DrawDocument)1