-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Writing quickstart + test for Bigtable (#1431)
* Setup mvn package and copy in existing samples and tests * Fix style issues and ran tests * Writing quickstart + test for Bigtable * Updating pom with current versions * Adding default values for the system propertiesˆ * surefire instead of failsafe * setup table for testing using cbt tool, retriggering test * Removing unnecessary code from quickstart test that was causing failures * cleaning up quickstart * Changing test variables to use GOOGLE_CLOUD_PROJECT and the instance environment variable
- Loading branch information
1 parent
01edb36
commit 1a491c8
Showing
7 changed files
with
171 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
bigtable/src/main/java/com/m/examples/bigtable/Quickstart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.m.examples.bigtable; | ||
|
||
// START [bigtable_quickstart_veneer] | ||
|
||
import com.google.api.gax.rpc.NotFoundException; | ||
import com.google.cloud.bigtable.data.v2.BigtableDataClient; | ||
import com.google.cloud.bigtable.data.v2.BigtableDataSettings; | ||
import com.google.cloud.bigtable.data.v2.models.Row; | ||
import com.google.cloud.bigtable.data.v2.models.RowCell; | ||
|
||
public class Quickstart { | ||
|
||
public static void quickstart(String projectId, String instanceId, String tableId) { | ||
// String projectId = "my-project-id"; | ||
// String instanceId = "my-instance-id"; | ||
// String tableId = "my-table-id"; | ||
|
||
BigtableDataSettings settings = | ||
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build(); | ||
|
||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) { | ||
try { | ||
System.out.println("\nReading a single row by row key"); | ||
Row row = dataClient.readRow(tableId, "r1"); | ||
System.out.println("Row: " + row.getKey().toStringUtf8()); | ||
for (RowCell cell : row.getCells()) { | ||
System.out.printf( | ||
"Family: %s Qualifier: %s Value: %s%n", | ||
cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8()); | ||
} | ||
} catch (NotFoundException e) { | ||
System.err.println("Failed to read from a non-existent table: " + e.getMessage()); | ||
} | ||
|
||
} catch (Exception e) { | ||
System.out.println("Error during functionName: \n" + e.toString()); | ||
} | ||
} | ||
} | ||
// END [bigtable_quickstart_veneer] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
bigtable/src/test/java/com/m/examples/bigtable/QuickstartTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.m.examples.bigtable; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; | ||
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings; | ||
import com.google.cloud.bigtable.data.v2.BigtableDataClient; | ||
import com.google.cloud.bigtable.data.v2.BigtableDataSettings; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.After; | ||
import org.junit.AfterClass; | ||
import org.junit.AssumptionViolatedException; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Integration tests for {@link Quickstart} | ||
*/ | ||
public class QuickstartTest { | ||
|
||
private static final String INSTANCE_PROPERTY_NAME = "BIGTABLE_TESTING_INSTANCE"; | ||
private static final String TABLE_ID = "quickstart-table"; | ||
private static String projectId; | ||
private static String instanceId; | ||
private ByteArrayOutputStream bout; | ||
|
||
private static String requireEnv(String varName) { | ||
assertNotNull( | ||
System.getenv(varName), | ||
"System property '%s' is required to perform these tests.".format(varName)); | ||
return System.getenv(varName); | ||
} | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
projectId = requireEnv("GOOGLE_CLOUD_PROJECT"); | ||
instanceId = requireEnv(INSTANCE_PROPERTY_NAME); | ||
} | ||
|
||
@Before | ||
public void setupStream() { | ||
bout = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(bout)); | ||
} | ||
|
||
@Test | ||
public void testQuickstart() { | ||
try { | ||
Quickstart.quickstart(projectId, instanceId, TABLE_ID); | ||
} catch (Exception e) { | ||
System.out.println("Failed to run quickstart."); | ||
System.out.println(e); | ||
} | ||
|
||
String output = bout.toString(); | ||
assertThat(output, CoreMatchers.containsString("Reading a single row by row key")); | ||
assertThat(output, CoreMatchers.containsString("Row: r1")); | ||
assertThat( | ||
output, CoreMatchers.containsString("Family: cf1 Qualifier: c1 Value: quickstart")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters