|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.bigquery; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static junit.framework.TestCase.assertNotNull; |
| 21 | + |
| 22 | +import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; |
| 23 | +import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest; |
| 24 | +import com.google.cloud.bigtable.data.v2.BigtableDataClient; |
| 25 | +import com.google.cloud.bigtable.data.v2.models.BulkMutation; |
| 26 | +import com.google.cloud.bigtable.data.v2.models.Mutation; |
| 27 | +import com.google.protobuf.ByteString; |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.PrintStream; |
| 31 | +import java.util.UUID; |
| 32 | +import java.util.logging.Level; |
| 33 | +import java.util.logging.Logger; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.BeforeClass; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +public class QueryExternalBigtableTempIT { |
| 40 | + |
| 41 | + private final Logger log = Logger.getLogger(this.getClass().getName()); |
| 42 | + private static final String ID = UUID.randomUUID().toString().substring(0, 8); |
| 43 | + private static final String TABLE_ID = "bigquery-samples-test" + ID; |
| 44 | + private static final String COLUMN_FAMILY_NAME = "stats_summary"; |
| 45 | + private static final long TIMESTAMP = System.currentTimeMillis() * 1000; |
| 46 | + private static final String CONNECTED_CELL = "connected_cell"; |
| 47 | + private static final String CONNECTED_WIFI = "connected_wifi"; |
| 48 | + private static final String OS_BUILD = "os_build"; |
| 49 | + private ByteArrayOutputStream bout; |
| 50 | + private PrintStream out; |
| 51 | + private PrintStream originalPrintStream; |
| 52 | + |
| 53 | + private static final String INSTANCE = requireEnvVar("BIGTABLE_TESTING_INSTANCE"); |
| 54 | + private static final String PROJECT = requireEnvVar("SAMPLES_TESTING_PROJECT"); |
| 55 | + |
| 56 | + private static String requireEnvVar(String varName) { |
| 57 | + String value = System.getenv(varName); |
| 58 | + assertNotNull( |
| 59 | + "Environment variable " + varName + " is required to perform these tests.", |
| 60 | + System.getenv(varName)); |
| 61 | + return value; |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void checkRequirements() { |
| 66 | + requireEnvVar("GOOGLE_CLOUD_PROJECT"); |
| 67 | + requireEnvVar("BIGTABLE_TESTING_INSTANCE"); |
| 68 | + } |
| 69 | + |
| 70 | + @Before |
| 71 | + public void setUp() throws IOException { |
| 72 | + bout = new ByteArrayOutputStream(); |
| 73 | + out = new PrintStream(bout); |
| 74 | + originalPrintStream = System.out; |
| 75 | + System.setOut(out); |
| 76 | + |
| 77 | + // create a temporary bigtable table. |
| 78 | + try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) { |
| 79 | + CreateTableRequest createTableRequest = |
| 80 | + CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME); |
| 81 | + client.createTable(createTableRequest); |
| 82 | + } |
| 83 | + // inserting temporary rows. |
| 84 | + try (BigtableDataClient client = BigtableDataClient.create(PROJECT, INSTANCE)) { |
| 85 | + BulkMutation bulkMutation = |
| 86 | + BulkMutation.create(TABLE_ID) |
| 87 | + .add( |
| 88 | + "phone#4c410523#20190501", |
| 89 | + Mutation.create() |
| 90 | + .setCell( |
| 91 | + COLUMN_FAMILY_NAME, |
| 92 | + ByteString.copyFrom(CONNECTED_CELL.getBytes()), |
| 93 | + TIMESTAMP, |
| 94 | + 1) |
| 95 | + .setCell( |
| 96 | + COLUMN_FAMILY_NAME, |
| 97 | + ByteString.copyFrom(CONNECTED_WIFI.getBytes()), |
| 98 | + TIMESTAMP, |
| 99 | + 1) |
| 100 | + .setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.003")) |
| 101 | + .add( |
| 102 | + "phone#4c410523#20190502", |
| 103 | + Mutation.create() |
| 104 | + .setCell( |
| 105 | + COLUMN_FAMILY_NAME, |
| 106 | + ByteString.copyFrom(CONNECTED_CELL.getBytes()), |
| 107 | + TIMESTAMP, |
| 108 | + 1) |
| 109 | + .setCell( |
| 110 | + COLUMN_FAMILY_NAME, |
| 111 | + ByteString.copyFrom(CONNECTED_WIFI.getBytes()), |
| 112 | + TIMESTAMP, |
| 113 | + 1) |
| 114 | + .setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190405.004")) |
| 115 | + .add( |
| 116 | + "phone#4c410523#20190505", |
| 117 | + Mutation.create() |
| 118 | + .setCell( |
| 119 | + COLUMN_FAMILY_NAME, |
| 120 | + ByteString.copyFrom(CONNECTED_CELL.getBytes()), |
| 121 | + TIMESTAMP, |
| 122 | + 0) |
| 123 | + .setCell( |
| 124 | + COLUMN_FAMILY_NAME, |
| 125 | + ByteString.copyFrom(CONNECTED_WIFI.getBytes()), |
| 126 | + TIMESTAMP, |
| 127 | + 1) |
| 128 | + .setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000")) |
| 129 | + .add( |
| 130 | + "phone#5c10102#20190501", |
| 131 | + Mutation.create() |
| 132 | + .setCell( |
| 133 | + COLUMN_FAMILY_NAME, |
| 134 | + ByteString.copyFrom(CONNECTED_CELL.getBytes()), |
| 135 | + TIMESTAMP, |
| 136 | + 1) |
| 137 | + .setCell( |
| 138 | + COLUMN_FAMILY_NAME, |
| 139 | + ByteString.copyFrom(CONNECTED_WIFI.getBytes()), |
| 140 | + TIMESTAMP, |
| 141 | + 1) |
| 142 | + .setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190401.002")) |
| 143 | + .add( |
| 144 | + "phone#5c10102#20190502", |
| 145 | + Mutation.create() |
| 146 | + .setCell( |
| 147 | + COLUMN_FAMILY_NAME, |
| 148 | + ByteString.copyFrom(CONNECTED_CELL.getBytes()), |
| 149 | + TIMESTAMP, |
| 150 | + 1) |
| 151 | + .setCell( |
| 152 | + COLUMN_FAMILY_NAME, |
| 153 | + ByteString.copyFrom(CONNECTED_WIFI.getBytes()), |
| 154 | + TIMESTAMP, |
| 155 | + 0) |
| 156 | + .setCell(COLUMN_FAMILY_NAME, OS_BUILD, TIMESTAMP, "PQ2A.190406.000")); |
| 157 | + |
| 158 | + client.bulkMutateRows(bulkMutation); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + @After |
| 163 | + public void tearDown() throws IOException { |
| 164 | + // Clean up |
| 165 | + try (BigtableTableAdminClient client = BigtableTableAdminClient.create(PROJECT, INSTANCE)) { |
| 166 | + client.deleteTable(TABLE_ID); |
| 167 | + } |
| 168 | + // restores print statements in the original method |
| 169 | + System.out.flush(); |
| 170 | + System.setOut(originalPrintStream); |
| 171 | + log.log(Level.INFO, bout.toString()); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + public void testQueryExternalBigtableTemp() { |
| 176 | + String tableName = "EXTERNAL_TEMP_TABLE_FROM_BIGTABLE_TEST_" + ID; |
| 177 | + String query = String.format("SELECT * FROM %s ", tableName); |
| 178 | + String sourceUri = |
| 179 | + String.format( |
| 180 | + "https://googleapis.com/bigtable/projects/%s/instances/%s/tables/%s", |
| 181 | + PROJECT, INSTANCE, TABLE_ID); |
| 182 | + QueryExternalBigtableTemp.queryExternalBigtableTemp(tableName, sourceUri, query); |
| 183 | + assertThat(bout.toString()) |
| 184 | + .contains("Query on external temporary table performed successfully."); |
| 185 | + } |
| 186 | +} |
0 commit comments