C# hello world
This code sample is a "hello world" application written in C#. The sample illustrates how to complete the following tasks:
- Set up authentication
- Connect to a Bigtable instance
- Create a new table.
- Write data to the table.
- Read the data back.
- Delete the table.
Set up authentication
To use the .NET samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
For more information, see Set up authentication for a local development environment.
Running the sample
This code communicates with 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 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 Libraries 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 APIs BigtableTableAdminClient
helps you create and delete instances and tables. The C#
Data APIs BigtableClient
helps you read and write table
data.
Creating a table
Call the CreateTable()
method in the BigtableTableAdminClient
class 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.