8000 Moving Bigtable samples into this repo (#1429) · llozano100/java-docs-samples@a7e39e7 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7e39e7

Browse files
Moving Bigtable samples into this repo (GoogleCloudPlatform#1429)
* Setup mvn package and copy in existing samples and tests * Fix style issues and ran tests * Updating pom with current versions * Adding default values for the system propertiesˆ * surefire instead of failsafe
1 parent 4fc43b7 commit a7e39e7

File tree

7 files changed

+1433
-0
lines changed

7 files changed

+1433
-0
lines changed

bigtable/pom.xml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google Inc.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
22+
<groupId>com.m.examples.bigtable</groupId>
23+
<artifactId>bigtable</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
26+
<!--
27+
The parent pom defines common style checks and testing strategies for our samples.
28+
Removing or replacing it should not affect the execution of the samples in anyway.
29+
-->
30+
<parent>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<artifactId>shared-configuration</artifactId>
33+
<version>1.0.11</version>
34+
</parent>
35+
36+
<name>bigtable</name>
37+
38+
<properties>
39+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
40+
<maven.compiler.source>1.8</maven.compiler.source>
41+
<maven.compiler.target>1.8</maven.compiler.target>
42+
</properties>
43+
44+
<dependencies>
45+
<dependency>
46+
<groupId>junit</groupId>
47+
<artifactId>junit</artifactId>
48+
<version>4.12</version>
49+
<scope>test</scope>
50+
</dependency>
51+
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
52+
<dependency>
53+
<groupId>com.google.cloud</groupId>
54+
<artifactId>google-cloud-bigtable</artifactId>
55+
<version>0.92.0-alpha</version>
56+
</dependency>
57+
</dependencies>
58+
59+
<build>
60+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
61+
<plugins>
62+
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
63+
<plugin>
64+
<artifactId>maven-clean-plugin</artifactId>
65+
<version>3.1.0</version>
66+
</plugin>
67+
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
68+
<plugin>
69+
<artifactId>maven-resources-plugin</artifactId>
70+
<version>3.0.2</version>
71+
</plugin>
72+
<plugin>
73+
<artifactId>maven-compiler-plugin</artifactId>
74+
<version>3.8.0</version>
75+
</plugin>
76+
<plugin>
77+
<artifactId>maven-surefire-plugin</artifactId>
78+
<version>2.22.1</version>
79+
</plugin>
80+
<plugin>
81+
<artifactId>maven-jar-plugin</artifactId>
82+
<version>3.0.2</version>
83+
</plugin>
84+
<plugin>
85+
<artifactId>maven-install-plugin</artifactId>
86+
<version>2.5.2</version>
87+
</plugin>
88+
<plugin>
89+
<artifactId>maven-deploy-plugin</artifactId>
90+
<version>2.8.2</version>
91+
</plugin>
92+
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
93+
<plugin>
94+
<artifactId>maven-site-plugin</artifactId>
95+
<version>3.7.1</version>
96+
</plugin>
97+
<plugin>
98+
<artifactId>maven-project-info-reports-plugin</artifactId>
99+
<version>3.0.0</version>
100+
</plugin>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-surefire-plugin</artifactId>
104+
<version>3.0.0-M3</version>
105+
<configuration>
106+
<systemPropertyVariables>
107+
<bigtable.project>java-docs-samples-testing</bigtable.project>
108+
<bigtable.instance>instance</bigtable.instance>
109+
</systemPropertyVariables>
110+
</configuration>
111+
</plugin>
112+
</plugins>
113+
</pluginManagement>
114+
</build>
115+
</project>
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright 2019 Google Inc.
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.m.examples.bigtable;
18+
19+
// [START bigtable_hw_imports_veneer]
20+
import com.google.api.gax.rpc.NotFoundException;
21+
import com.google.api.gax.rpc.ServerStream;
22+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
23+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
24+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
25+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
26+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
27+
import com.google.cloud.bigtable.data.v2.models.Query;
28+
import com.google.cloud.bigtable.data.v2.models.Row;
29+
import com.google.cloud.bigtable.data.v2.models.RowCell;
30+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
31+
import java.io.IOException;
32+
33+
// [END bigtable_hw_imports_veneer]
34+
35+
/**
36+
* An example of using Google Cloud Bigtable.
37+
*
38+
* <p>This example is a very simple "hello world" application, that illustrates how to create a new
39+
* table, write to the table, read the data back, and delete the table.
40+
*
41+
* <ul>
42+
* <li>create table
43+
* <li>read single row
44+
* <li>read table
45+
* <li>delete table
46+
* </ul>
47+
*/
48+
public class HelloWorld {
49+
50+
private static final String COLUMN_FAMILY = "cf1";
51+
private static final String COLUMN_QUALIFIER = "greeting";
52+
private static final String ROW_KEY_PREFIX = "rowKey";
53+
private final String tableId;
54+
private final BigtableDataClient dataClient;
55+
private final BigtableTableAdminClient adminClient;
56+
57+
public static void main(String[] args) throws Exception {
58+
59+
if (args.length != 2) {
60+
System.out.println("Missing required project id or instance id");
61+
return;
62+
}
63+
String projectId = args[0];
64+
String instanceId = args[1];
65+
66+
HelloWorld helloWorld = new HelloWorld(projectId, instanceId, "test-table");
67+
helloWorld.run();
68+
}
69+
70+
public HelloWorld(String projectId, String instanceId, String tableId) throws IOException {
71+
this.tableId = tableId;
72+
73+
// [START bigtable_hw_connect_veneer]
74+
// Creates the settings to configure a bigtable data client.
75+
BigtableDataSettings settings =
76+
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
77+
78+
// Creates a bigtable data client.
79+
dataClient = BigtableDataClient.create(settings);
80+
81+
// Creates the settings to configure a bigtable table admin client.
82+
BigtableTableAdminSettings adminSettings =
83+
BigtableTableAdminSettings.newBuilder()
84+
.setProjectId(projectId)
85+
.setInstanceId(instanceId)
86+
.build();
87+
88+
// Creates a bigtable table admin client.
89+
adminClient = BigtableTableAdminClient.create(adminSettings);
90+
// [END bigtable_hw_connect_veneer]
91+
}
92+
93+
public void run() throws Exception {
94+
createTable();
95+
writeToTable();
96+
readSingleRow();
97+
readTable();
98+
deleteTable();
99+
dataClient.close();
100+
adminClient.close();
101+
}
102+
103+
/** Demonstrates how to create a table. */
104+
public void createTable() {
105+
// [START bigtable_hw_create_table_veneer]
106+
// Checks if table exists, creates table if does not exist.
107+
if (!adminClient.exists(tableId)) {
108+
System.out.printl B41A n("Creating table: " + tableId);
109+
CreateTableRequest createTableRequest =
110+
CreateTableRequest.of(tableId).addFamily(COLUMN_FAMILY);
111+
adminClient.createTable(createTableRequest);
112+
System.out.printf("Table %s created successfully%n", tableId);
113+
}
114+
// [END bigtable_hw_create_table_veneer]
115+
}
116+
117+
/** Demonstrates how to write some rows to a table. */
118+
public void writeToTable() {
119+
// [START bigtable_hw_write_rows_veneer]
120+
try {
121+
System.out.println("\nWriting some greetings to the table");
122+
String[] greetings = {"Hello World!", "Hello Bigtable!", "Hello Java!"};
123+
for (int i = 0; i < greetings.length; i++) {
124+
RowMutation rowMutation =
125+
RowMutation.create(tableId, ROW_KEY_PREFIX + i)
126+
.setCell(COLUMN_FAMILY, COLUMN_QUALIFIER, greetings[i]);
127+
dataClient.mutateRow(rowMutation);
128+
System.out.println(greetings[i]);
129+
}
130+
} catch (NotFoundException e) {
131+
System.err.println("Failed to write to non-existent table: " + e.getMessage());
132+
}
133+
// [END bigtable_hw_write_rows_veneer]
134+
}
135+
136+
/** Demonstrates how to read a single row from a table. */
137+
public void readSingleRow() {
138+
// [START bigtable_hw_get_by_key_veneer]
139+
try {
140+
System.out.println("\nReading a single row by row key");
141+
Row row = dataClient.readRow(tableId, ROW_KEY_PREFIX + 0);
142+
System.out.println("Row: " + row.getKey().toStringUtf8());
143+
for (RowCell cell : row.getCells()) {
144+
System.out.printf(
145+
"Family: %s Qualifier: %s Value: %s%n",
146+
cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
147+
}
148+
} catch (NotFoundException e) {
149+
System.err.println("Failed to read from a non-existent table: " + e.getMessage());
150+
}
151+
// [END bigtable_hw_get_by_key_veneer]
152+
}
153+
154+
/** Demonstrates how to read an entire table. */
155+
public void readTable() {
156+
// [START bigtable_hw_scan_all_veneer]
157+
try {
158+
System.out.println("\nReading the entire table");
159+
Query query = Query.create(tableId);
160+
ServerStream<Row> rowStream = dataClient.readRows(query);
161+
for (Row r : rowStream) {
162+
System.out.println("Row Key: " + r.getKey().toStringUtf8());
163+
for (RowCell cell : r.getCells()) {
164+
System.out.printf(
165+
"Family: %s Qualifier: %s Value: %s%n",
166+
cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
167+
}
168+
}
169+
} catch (NotFoundException e) {
170+
System.err.println("Failed to read a non-existent table: " + e.getMessage());
171+
}
172+
// [END bigtable_hw_scan_all_veneer]
173+
}
174+
175+
/** Demonstrates how to delete a table. */
176+
public void deleteTable() {
177+
// [START bigtable_hw_delete_table_veneer]
178+
System.out.println("\nDeleting table: " + tableId);
179+
try {
180+
adminClient.deleteTable(tableId);
181+
System.out.printf("Table %s deleted successfully%n", tableId);
182+
} catch (NotFoundException e) {
183+
System.err.println("Failed to delete a non-existent table: " + e.getMessage());
184+
}
185+
// [END bigtable_hw_delete_table_veneer]
186+
}
187+
}

0 commit comments

Comments
 (0)
0