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.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
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.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
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.