From 72702874b93765224daa3f49691424543fb52a27 Mon Sep 17 00:00:00 2001 From: BenWhitehead Date: Tue, 30 Apr 2019 17:28:39 -0400 Subject: [PATCH] Add snippet for Collection Group Query * Bump `com.google.cloud:google-cloud-firestore` to `1.2.0` which includes the new functionality * Add `hashCode` for `City.java` --- firestore/pom.xml | 2 +- .../firestore/snippets/QueryDataSnippets.java | 68 +++++++++++++++++-- .../firestore/snippets/model/City.java | 5 ++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/firestore/pom.xml b/firestore/pom.xml index e4282911c75..39b7a3bc1be 100644 --- a/firestore/pom.xml +++ b/firestore/pom.xml @@ -45,7 +45,7 @@ com.google.cloud google-cloud-firestore - 0.81.0-beta + 1.2.0 diff --git a/firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java b/firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java index 0b152f18097..fede3d73c8c 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java +++ b/firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java @@ -17,11 +17,9 @@ package com.example.firestore.snippets; import com.example.firestore.snippets.model.City; - import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; import com.google.cloud.firestore.CollectionReference; -import com.google.cloud.firestore.DocumentReference; import com.google.cloud.firestore.DocumentSnapshot; import com.google.cloud.firestore.Firestore; import com.google.cloud.firestore.Query; @@ -29,11 +27,9 @@ import com.google.cloud.firestore.QueryDocumentSnapshot; import com.google.cloud.firestore.QuerySnapshot; import com.google.cloud.firestore.WriteResult; - -import com.google.firestore.v1beta1.Document; -import com.google.protobuf.Api; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -383,4 +379,66 @@ List paginateCursor() throws InterruptedException, ExecutionException, Ti // [END fs_paginate_cursor] return Arrays.asList(firstPage, secondPage); } + + void collectionGroupQuery() throws ExecutionException, InterruptedException { + //CHECKSTYLE OFF: Indentation + //CHECKSTYLE OFF: RightCurlyAlone + // [START fs_collection_group_query_data_setup] + CollectionReference cities = db.collection("cities"); + + final List> futures = Arrays.asList( + cities.document("SF").collection("landmarks").document().set(new HashMap() {{ + put("name", "Golden Gate Bridge"); + put("type", "bridge"); + }}), + cities.document("SF").collection("landmarks").document().set(new HashMap() {{ + put("name", "Legion of Honor"); + put("type", "museum"); + }}), + cities.document("LA").collection("landmarks").document().set(new HashMap() {{ + put("name", "Griffith Park"); + put("type", "park"); + }}), + cities.document("LA").collection("landmarks").document().set(new HashMap() {{ + put("name", "The Getty"); + put("type", "museum"); + }}), + cities.document("DC").collection("landmarks").document().set(new HashMap() {{ + put("name", "Lincoln Memorial"); + put("type", "memorial"); + }}), + cities.document("DC").collection("landmarks").document().set(new HashMap() {{ + put("name", "National Air and Space Museum"); + put("type", "museum"); + }}), + cities.document("TOK").collection("landmarks").document().set(new HashMap() {{ + put("name", "Ueno Park"); + put("type", "park"); + }}), + cities.document("TOK").collection("landmarks").document().set(new HashMap() {{ + put("name", "National Museum of Nature and Science"); + put("type", "museum"); + }}), + cities.document("BJ").collection("landmarks").document().set(new HashMap() {{ + put("name", "Jingshan Park"); + put("type", "park"); + }}), + cities.document("BJ").collection("landmarks").document().set(new HashMap() {{ + put("name", "Beijing Ancient Observatory"); + put("type", "museum"); + }}) + ); + final List landmarks = ApiFutures.allAsList(futures).get(); + // [END fs_collection_group_query_data_setup] + + // [START fs_collection_group_query] + final Query museums = db.collectionGroup("landmarks").whereEqualTo("type", "museum"); + final ApiFuture querySnapshot = museums.get(); + for (DocumentSnapshot document : querySnapshot.get().getDocuments()) { + System.out.println(document.getId()); + } + // [END fs_collection_group_query] + //CHECKSTYLE ON: RightCurlyAlone + //CHECKSTYLE ON: Indentation + } } diff --git a/firestore/src/main/java/com/example/firestore/snippets/model/City.java b/firestore/src/main/java/com/example/firestore/snippets/model/City.java index 864508d3866..48a41145a16 100644 --- a/firestore/src/main/java/com/example/firestore/snippets/model/City.java +++ b/firestore/src/main/java/com/example/firestore/snippets/model/City.java @@ -161,4 +161,9 @@ public boolean equals(Object obj) { && Objects.equals(capital, city.capital) && Objects.equals(regions, city.regions); } + + @Override + public int hashCode() { + return Objects.hash(name, state, country, capital, population, regions); + } }