August 12, 2021
Here is the article.
Example: C# "Hello world" application
This code sample is a "hello world" application written in C#. The sample illustrates how to complete the following tasks:
- Connect to a Cloud Bigtable instance
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Running the sample
This code communicates with Cloud Bigtable using the C# Admin API and C# Data API libraries in the Google Cloud Client Libraries for .NET.
To run this sample program, follow the .NET Cloud Bigtable Samples instructions on GitHub. Complete the Build and Run and Quick Start steps to create resources that you can use in your Hello World application. Make sure you edit the HelloWorld.cs
file to add the names of the resources you create.
Using the Cloud Client Library with Bigtable
The sample application connects to Bigtable and demonstrates some simple operations.
Connecting to Bigtable
To get started, create two client objects that you can use to connect to Bigtable. The C# Admin API's BigtableTableAdminClient
helps you create and delete instances and tables. The C# Data API's BigtableClient
helps you read and write table data.
Creating a table
Call the admin client's CreateTable()
method to generate a Table
object that stores the "hello world" greetings. The table has a single column family that retains one version of each value.
Writing rows to a table
Use the string array s_greetings[]
, which contains three simple greetings, as a source of data to write to the table. First, write a single row to the table using MutateRow()
. Then loop through the rest of the array to build a MutateRowsRequest
object that contains an entry for each greeting. Make the request to write all the entries at once with MutateRows()
. Then loop through the returned response to check the status code for each entry to make sure it was written successfully.
Creating a filter
Before you read the data that you wrote, create a filter to limit the data that Bigtable returns. This filter tells Bigtable to return only the most recent version of each value, even if the table contains older cells that are eligible for garbage collection but have not yet been deleted.
Reading a row by its row key
Use the ReadRow()
method, passing in the filter you just created, to get one version of each value in that row.
Scanning all table rows
Call the ReadRows()
method, passing in the filter, to get all of the rows in the table. Because you passed in the filter, Bigtable returns only one version of each value.
Deleting a table
Delete the table with the DeleteTable()
method.
Putting it all together
Here is the full code sample without comments.
No comments:
Post a Comment