Stay organized with collections
Save and categorize content based on your preferences.
Example: Go App Engine Application
This example is an App Engine application, written in Go, that
provides a web interface that uses Bigtable to track the number of
visits from your Google account. It runs locally in a Docker container or in the
cloud in App Engine's flexible environment. The code for
this application is in the GitHub repository
GoogleCloudPlatform/golang-samples, in the directory
bigtable/usercounter.
Overview of the code sample
When the code sample is launched, it creates an administrative client for
Bigtable. It then uses the client to check the user-specified
Bigtable instance for a table named user-visit-counter, with a
single column family named emails. If necessary, it creates the table and
column family:
adminClient,err:=bigtable.NewAdminClient(ctx,project,instance)iferr!=nil{log.Fatalf("Unable to create a table admin client. %v",err)}tables,err:=adminClient.Tables(ctx)iferr!=nil{log.Fatalf("Unable to fetch table list. %v",err)}if!sliceContains(tables,tableName){iferr:=adminClient.CreateTable(ctx,tableName);err!=nil{log.Fatalf("Unable to create table: %v. %v",tableName,err)}}tblInfo,err:=adminClient.TableInfo(ctx,tableName)iferr!=nil{log.Fatalf("Unable to read info for table: %v. %v",tableName,err)}if!sliceContains(tblInfo.Families,familyName){iferr:=adminClient.CreateColumnFamily(ctx,tableName,familyName);err!=nil{log.Fatalf("Unable to create column family: %v. %v",familyName,err)}}adminClient.Close()
The code sample then creates a single Bigtable client that is used
for all subsequent reads and writes:
client,err=bigtable.NewClient(ctx,project,instance)iferr!=nil{log.Fatalf("Unable to create data operations client. %v",err)}
Finally, the code sample adds an HTTP handler to the root of the App Engine
server. On each request, the handler prompts the user to log in if necessary. It
then tracks the user's visit by performing an Increment operation on the row
for the user's email address:
tbl:=client.Open(tableName)rmw:=bigtable.NewReadModifyWrite()rmw.Increment(familyName,u.Email,1)row,err:=tbl.ApplyReadModifyWrite(ctx,u.Email,rmw)iferr!=nil{return&appError{err,"Error applying ReadModifyWrite to row: "+u.Email,http.StatusInternalServerError}}
After incrementing the row, the handler displays an HTML page showing the total
number of visits by the current user.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-03-05 UTC."],[[["This is a Go-based App Engine application that uses Bigtable to track the number of visits from each user's Google account."],["The application can be run locally in a Docker container or in the cloud within App Engine's flexible environment."],["Upon launch, the code creates a Bigtable administrative client to manage the `user-visit-counter` table and its `emails` column family, creating them if they don't exist."],["The application uses a Bigtable client for reads and writes, and it includes an HTTP handler that prompts users to log in and then increments their visit count in Bigtable."],["The app does not provide any access control for the user data and should be shutdown after testing."]]],[]]