Skip to content

Conversation

@elisheva-qlogic
Copy link
Contributor

HelloWorld, TableAdmin, and InstanceAdmin examples

@elisheva-qlogic elisheva-qlogic requested a review from a team as a code owner November 15, 2018 15:28
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 15, 2018
Copy link

@igorbernstein2 igorbernstein2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good start, I added some comments, but didn't do a full review. Can you help me understand how samples work? how/who uses the comment tags? What are snippets? Why are other products differentiate between examples and snippets?

import com.google.cloud.bigtable.data.v2.models.RowMutation;
import java.sql.Timestamp;

// [START HelloWorld]

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

String COLUMN_FAMILY_ID = "cf1";
String COLUMN_QUALIFIER = "greeting";
String INSTANCE_ID = System.getenv("INSTANCE_ID");
String GCLOUD_PROJECT_ID = System.getenv("GCLOUD_PROJECT_ID");

This comment was marked as spam.

String COLUMN_FAMILY_ID = "cf1";
String COLUMN_QUALIFIER = "greeting";
String INSTANCE_ID = System.getenv("INSTANCE_ID");
String GCLOUD_PROJECT_ID = System.getenv("GCLOUD_PROJECT_ID");

This comment was marked as spam.


try {
System.out.println("Write some greetings to the table");
String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" };

This comment was marked as spam.

String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" };
for (int i = 0; i < greetings.length; i++) {
RowMutation rowMutation = RowMutation.create(TABLE_ID, rowKeyPrefix + i);
long timestamp = new Timestamp(System.currentTimeMillis()).getTime();

This comment was marked as spam.

.println("PRODUCTION type instance : " + instance.getId() + " created successfully");

} catch (Exception e) {
System.out.println("Error creating prod-instance: " + e.getMessage());

This comment was marked as spam.

deleteCluster(adminClient, "ssd-instance", "ssd-cluster");

// end operations with deleting the pro-instance created in `runInstanceOperations`
deleteInstance(adminClient, "ssd-instance");

This comment was marked as spam.

BigtableInstanceAdminClient.create(instanceAdminSettings);

System.out.println("Create an instance (type: PRODUCTION) and run basic instance-operations");
runInstanceOperations(adminClient, "ssd-instance", "ssd-cluster");

This comment was marked as spam.

System.out.println("Delete the Instance");
deleteInstance(adminClient, "hdd-instance");

System.out.println("Add Cluster");

This comment was marked as spam.

try {
adminClient.deleteInstance(instanceID);
System.out.println("Instance deleted: " + instanceID);
} catch (Exception e) {

This comment was marked as spam.

@sduskis sduskis changed the title Java samples Bigtable Java samples Nov 15, 2018
@sduskis
Copy link
Contributor

sduskis commented Nov 15, 2018

@igorbernstein2, the snippets are used in https://cloud.google.com/bigtable/docs/. Our documentation has a tool to pull in code based on the tags

@igorbernstein2
Copy link

Also, to @chingor13 comment, it would be good to add tests to make sure these snippets continue to work

@sduskis
Copy link
Contributor

sduskis commented Dec 4, 2018

@elisheva-qlogic, there's a build failure:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:compile (default-compile) on project google-cloud-examples: Compilation failure
[ERROR] /tmpfs/src/github/google-cloud-java/google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/TableAdmin.java:[56,60] incompatible types
[ERROR]   required: java.util.List<com.google.bigtable.admin.v2.TableName>
[ERROR]   found:    java.util.List<java.lang.String>

Copy link

@igorbernstein2 igorbernstein2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this sample should be restructured a bit

String TABLE_ID,
String ROW_KEY_PREFIX,
String COLUMN_FAMILY_ID,
String COLUMN_QUALIFIER) {

This comment was marked as spam.

dataClient.mutateRow(rowMutation);
System.out.println(greetings[i]);
}
} catch (Exception e) {

This comment was marked as spam.

System.out.println(greetings[i]);
}
} catch (Exception e) {
System.out.println("Exception while writing to table: " + e.getMessage());

This comment was marked as spam.

} catch (Exception e) {
System.out.println("Exception while writing to table: " + e.getMessage());
}
return mutation;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

} catch (Exception e) {
System.out.println("Exception while reading table: " + e.getMessage());
}
return rowStream;

This comment was marked as spam.

}

public static Table createTable(
BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) {

This comment was marked as spam.


public class HelloWorld {

public static void main(String... args) throws Exception {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


final String GCLOUD_PROJECT = args[0];
final String INSTANCE_ID = args[1];
final String TABLE_ID = "table";

This comment was marked as spam.

private static final String ROW_KEY_PREFIX = "test-rowKey";
private static BigtableDataClient dataClient;
private static BigtableTableAdminClient adminClient;

This comment was marked as spam.

@elisheva-qlogic
Copy link
Contributor Author

The main change is that HelloWorld sample is restructured. If this looks good, I will work on restructuring TableAdmin and InstanceAdmin samples like this as well.

private static String instanceId;
private static String tableId;
private static BigtableDataClient dataClient;
private static BigtableTableAdminClient adminClient;

This comment was marked as spam.

deleteTable();
garbageCollect();
} catch (ApiException e) {
System.err.println("Exception while running HelloWorld: " + e.getMessage());

This comment was marked as spam.

// [END connecting_to_bigtable]
}

public void run() throws Exception {

This comment was marked as spam.

This comment was marked as spam.

if (!adminClient.exists(tableId)) {
CreateTableRequest createTableRequest =
CreateTableRequest.of(tableId).addFamily(columnFamily);
System.out.println("Creating table: " + tableId);

This comment was marked as spam.

private static final String tablePrefix = "table";
private static final String columnFamily = "cf1";
private static final String columnQualifier = "greeting";
private static final String rowKeyPrefix = "rowKey";

This comment was marked as spam.

dataClient.mutateRow(rowMutation);
System.out.println(greetings[i]);
}
} catch (ApiException e) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

public void readTable() {
// [START scanning_all_rows]
try {
System.out.println("\n================= Reading the entire table =================");

This comment was marked as spam.

adminClient.deleteTable(tableId);
}
}
}

This comment was marked as spam.

System.out.println(greetings[i]);
}
} catch (NotFoundException e) {
System.err.println("Exception while writing to table: " + e.getMessage());

This comment was marked as spam.

}
tableId = generateTableId();
helloWorld = new HelloWorld(instanceName.getProject(), instanceName.getInstance(), tableId);
if (!adminClient.exists(tableId)) {

This comment was marked as spam.

This comment was marked as spam.


// Read a single row
OutputStream outputStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStream));

This comment was marked as spam.

This comment was marked as spam.

@sduskis
Copy link
Contributor

sduskis commented Jan 2, 2019

Closing this in favor of #4274

@sduskis sduskis closed this Jan 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants