-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bigtable Java samples #3946
Bigtable Java samples #3946
Conversation
There was a problem hiding this 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
|
||
try { | ||
System.out.println("Write some greetings to the table"); | ||
String[] greetings = { "Hello World!", "Hello Bigtable!", "Hello Node!" }; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
.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.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
System.out.println("Delete the Instance"); | ||
deleteInstance(adminClient, "hdd-instance"); | ||
|
||
System.out.println("Add Cluster"); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
try { | ||
adminClient.deleteInstance(instanceID); | ||
System.out.println("Instance deleted: " + instanceID); | ||
} catch (Exception e) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@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 |
Also, to @chingor13 comment, it would be good to add tests to make sure these snippets continue to work |
@elisheva-qlogic, there's a build failure:
|
4f20ff6
to
514b3ab
Compare
4f35dbf
to
7ad20e5
Compare
There was a problem hiding this 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
google-cloud-examples/src/main/java/com/google/cloud/examples/bigtable/HelloWorld.java
Show resolved
Hide resolved
String TABLE_ID, | ||
String ROW_KEY_PREFIX, | ||
String COLUMN_FAMILY_ID, | ||
String COLUMN_QUALIFIER) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
dataClient.mutateRow(rowMutation); | ||
System.out.println(greetings[i]); | ||
} | ||
} catch (Exception e) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
System.out.println(greetings[i]); | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Exception while writing to table: " + e.getMessage()); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
} 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
} catch (Exception e) { | ||
System.out.println("Exception while reading table: " + e.getMessage()); | ||
} | ||
return rowStream; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
} | ||
|
||
public static Table createTable( | ||
BigtableTableAdminClient adminClient, String TABLE_ID, String COLUMN_FAMILY_ID) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
public class HelloWorld { | ||
|
||
public static void main(String... args) throws Exception { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
final String GCLOUD_PROJECT = args[0]; | ||
final String INSTANCE_ID = args[1]; | ||
final String TABLE_ID = "table"; |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
private static final String ROW_KEY_PREFIX = "test-rowKey"; | ||
private static BigtableDataClient dataClient; | ||
private static BigtableTableAdminClient adminClient; | ||
|
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
deleteTable(); | ||
garbageCollect(); | ||
} catch (ApiException e) { | ||
System.err.println("Exception while running HelloWorld: " + e.getMessage()); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
// [END connecting_to_bigtable] | ||
} | ||
|
||
public void run() throws Exception { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if (!adminClient.exists(tableId)) { | ||
CreateTableRequest createTableRequest = | ||
CreateTableRequest.of(tableId).addFamily(columnFamily); | ||
System.out.println("Creating table: " + tableId); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
dataClient.mutateRow(rowMutation); | ||
System.out.println(greetings[i]); | ||
} | ||
} catch (ApiException e) { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
public void readTable() { | ||
// [START scanning_all_rows] | ||
try { | ||
System.out.println("\n================= Reading the entire table ================="); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
adminClient.deleteTable(tableId); | ||
} | ||
} | ||
} |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
System.out.println(greetings[i]); | ||
} | ||
} catch (NotFoundException e) { | ||
System.err.println("Exception while writing to table: " + e.getMessage()); |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
} | ||
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
// 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Closing this in favor of #4274 |
HelloWorld, TableAdmin, and InstanceAdmin examples