Skip to content

Commit 5894655

Browse files
Creating codelab for Cassandra migration queries. (GoogleCloudPlatform#1622)
1 parent 212c43d commit 5894655

File tree

3 files changed

+325
-0
lines changed

3 files changed

+325
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.example</groupId>
23+
<artifactId>bigtable</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
<maven.compiler.source>1.8</maven.compiler.source>
29+
<maven.compiler.target>1.8</maven.compiler.target>
30+
</properties>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.13-beta-3</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-bigtable -->
40+
<dependency>
41+
<groupId>com.google.cloud</groupId>
42+
<artifactId>google-cloud-bigtable</artifactId>
43+
<version>1.4.0</version>
44+
</dependency>
45+
</dependencies>
46+
</project>
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
import com.google.api.gax.rpc.ServerStream;
18+
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
19+
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
20+
import com.google.cloud.bigtable.data.v2.models.BulkMutation;
21+
import com.google.cloud.bigtable.data.v2.models.Mutation;
22+
import com.google.cloud.bigtable.data.v2.models.Query;
23+
import com.google.cloud.bigtable.data.v2.models.Row;
24+
import com.google.cloud.bigtable.data.v2.models.RowCell;
25+
import com.google.cloud.bigtable.data.v2.models.RowMutation;
26+
import com.google.protobuf.ByteString;
27+
28+
public class CassandraMigrationCodelab {
29+
private BigtableDataClient dataClient;
30+
private String tableId;
31+
private final String COLUMN_FAMILY_NAME = "stats_summary";
32+
33+
public CassandraMigrationCodelab(String projectId, String instanceId, String tableId) {
34+
this.tableId = tableId;
35+
BigtableDataSettings settings =
36+
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
37+
38+
try {
39+
dataClient = BigtableDataClient.create(settings);
40+
} catch (Exception e) {
41+
System.out.println("Error during data client connection: \n" + e.toString());
42+
}
43+
}
44+
45+
public void run() {
46+
write();
47+
writeBatch();
48+
update();
49+
update2();
50+
get();
51+
scan();
52+
delete();
53+
deleteMultiple();
54+
}
55+
56+
public void write() {
57+
try {
58+
System.currentTimeMillis();
59+
long timestamp = (long) 1556712000 * 1000; // Timestamp of June 1, 2019 12:00
60+
61+
String rowKey = "phone#4c410523#20190501";
62+
ByteString one = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 1});
63+
64+
RowMutation rowMutation =
65+
RowMutation.create(tableId, rowKey)
66+
.setCell(
67+
COLUMN_FAMILY_NAME,
68+
ByteString.copyFrom("connected_cell".getBytes()),
69+
timestamp,
70+
one)
71+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "PQ2A.190405.003");
72+
73+
dataClient.mutateRow(rowMutation);
74+
} catch (Exception e) {
75+
System.out.println("Error during Write: \n" + e.toString());
76+
}
77+
}
78+
79+
public void writeBatch() {
80+
try {
81+
long timestamp = (long) 1556712000 * 1000; // Timestamp of June 1, 2019 12:00
82+
83+
BulkMutation bulkMutation =
84+
BulkMutation.create(tableId)
85+
.add(
86+
"tablet#a0b81f74#20190501",
87+
Mutation.create()
88+
.setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "chromeos")
89+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc1"))
90+
.add(
91+
"tablet#a0b81f74#20190502",
92+
Mutation.create()
93+
.setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "chromeos")
94+
.setCell(COLUMN_FAMILY_NAME, "os_build", timestamp, "12155.0.0-rc6"));
95+
96+
dataClient.bulkMutateRows(bulkMutation);
97+
} catch (Exception e) {
98+
System.out.println("Error during WriteBatch: \n" + e.toString());
99+
}
100+
}
101+
102+
public void update() {
103+
try {
104+
long timestamp = (long) 1556713800 * 1000; // Timestamp of June 1, 2019 12:30
105+
106+
String rowKey = "phone#4c410523#20190501";
107+
108+
RowMutation rowMutation =
109+
RowMutation.create(tableId, rowKey)
110+
.setCell(COLUMN_FAMILY_NAME, "os_name", timestamp, "android");
111+
112+
dataClient.mutateRow(rowMutation);
113+
} catch (Exception e) {
114+
System.out.println("Error during update: \n" + e.toString());
115+
}
116+
}
117+
118+
public void update2() {
119+
120+
try {
121+
long timestamp = (long) 1556713800 * 1000; // Timestamp of June 1, 2019 12:30
122+
123+
String rowKey = "phone#4c410523#20190501";
124+
125+
ByteString zero = ByteString.copyFrom(new byte[] {0, 0, 0, 0, 0, 0, 0, 0});
126+
127+
RowMutation rowMutation =
128+
RowMutation.create(tableId, rowKey)
129+
.setCell(
130+
COLUMN_FAMILY_NAME,
131+
ByteString.copyFrom("connected_cell".getBytes()),
132+
timestamp,
133+
zero);
134+
135+
dataClient.mutateRow(rowMutation);
136+
} catch (Exception e) {
137+
System.out.println("Error during update2: \n" + e.toString());
138+
}
139+
}
140+
141+
public void get() {
142+
try {
143+
String rowKey = "phone#4c410523#20190501";
144+
145+
Row row = dataClient.readRow(tableId, rowKey);
146+
for (RowCell cell : row.getCells()) {
147+
148+
System.out.printf(
149+
"Family: %s Qualifier: %s Value: %s Timestamp: %s%n",
150+
cell.getFamily(),
151+
cell.getQualifier().toStringUtf8(),
152+
cell.getValue().toStringUtf8(),
153+
cell.getTimestamp());
154+
}
155+
} catch (Exception e) {
156+
System.out.println("Error during lookup: \n" + e.toString());
157+
}
158+
}
159+
160+
public void scan() {
161+
try {
162+
Query query = Query.create(tableId).range("tablet#a0b81f74#201905", "tablet#a0b81f74#201906");
163+
ServerStream<Row> rowStream = dataClient.readRows(query);
164+
for (Row row : rowStream) {
165+
System.out.println("Row Key: " + row.getKey().toStringUtf8());
166+
for (RowCell cell : row.getCells()) {
167+
168+
System.out.printf(
169+
"Family: %s Qualifier: %s Value: %s Timestamp: %s%n",
170+
cell.getFamily(),
171+
cell.getQualifier().toStringUtf8(),
172+
cell.getValue().toStringUtf8(),
173+
cell.getTimestamp());
174+
}
175+
}
176+
} catch (Exception e) {
177+
System.out.println("Error during scan: \n" + e.toString());
178+
}
179+
}
180+
181+
public void delete() {
182+
try {
183+
String rowKey = "phone#4c410523#20190501";
184+
185+
RowMutation mutation = RowMutation.create(tableId, rowKey).deleteRow();
186+
187+
dataClient.mutateRow(mutation);
188+
} catch (Exception e) {
189+
System.out.println("Error during Delete: \n" + e.toString());
190+
}
191+
}
192+
193+
public void deleteMultiple() {
194+
try {
195+
Query query = Query.create(tableId).prefix("tablet#a0b81f7");
196+
ServerStream<Row> rowStream = dataClient.readRows(query);
197+
BulkMutation bulkMutation = BulkMutation.create(tableId);
198+
for (Row row : rowStream) {
199+
bulkMutation.add(row.getKey(), Mutation.create().deleteRow());
200+
}
201+
202+
dataClient.bulkMutateRows(bulkMutation);
203+
} catch (Exception e) {
204+
System.out.println("Error during DeleteMultiple: \n" + e.toString());
205+
}
206+
}
207+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.junit.Assert.assertNotNull;
19+
20+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
21+
import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
22+
import com.google.cloud.bigtable.admin.v2.models.CreateTableRequest;
23+
import java.io.ByteArrayOutputStream;
24+
import java.io.PrintStream;
25+
import java.util.UUID;
26+
import org.hamcrest.CoreMatchers;
27+
import org.junit.Test;
28+
29+
public class CassandraMigrationCodelabTest {
30+
private static final String INSTANCE_ENV = "BIGTABLE_TESTING_INSTANCE";
31+
private final String COLUMN_FAMILY_NAME = "stats_summary";
32+
private static final String TABLE_PREFIX = "cass-";
33+
34+
private static final String TABLE_ID =
35+
TABLE_PREFIX + UUID.randomUUID().toString().substring(0, 20);
36+
private static BigtableTableAdminClient adminClient;
37+
private static String projectId;
38+
private static String instanceId;
39+
private CassandraMigrationCodelab cassandraMigrationCodelab;
40+
41+
private static String requireEnv(String varName) {
42+
assertNotNull(
43+
System.getenv(varName),
44+
"Environment variable '%s' is required to perform these tests.".format(varName));
45+
return System.getenv(varName);
46+
}
47+
48+
@Test
49+
public void testRunDoesNotFail() throws Exception {
50+
projectId = requireEnv("GOOGLE_CLOUD_PROJECT");
51+
instanceId = requireEnv(INSTANCE_ENV);
52+
BigtableTableAdminSettings adminSettings =
53+
BigtableTableAdminSettings.newBuilder()
54+
.setProjectId(projectId)
55+
.setInstanceId(instanceId)
56+
.build();
57+
adminClient = BigtableTableAdminClient.create(adminSettings);
58+
59+
cassandraMigrationCodelab = new CassandraMigrationCodelab(projectId, instanceId, TABLE_ID);
60+
adminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(COLUMN_FAMILY_NAME));
61+
62+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
63+
System.setOut(new PrintStream(bout));
64+
65+
cassandraMigrationCodelab.run();
66+
67+
String output = bout.toString();
68+
assertThat(output, CoreMatchers.not(CoreMatchers.containsString("Error during")));
69+
70+
adminClient.deleteTable(TABLE_ID);
71+
}
72+
}

0 commit comments

Comments
 (0)