이 예는 Go로 작성된 App Engine 애플리케이션으로, Bigtable을 사용하여 Google 계정의 방문 수를 추적하는 웹 인터페이스를 제공합니다. 이 예는 Docker 컨테이너에서 로컬로 실행되거나 App Engine의 가변형 환경에 있는 클라우드에서 실행됩니다. 이 애플리케이션의 코드는 GitHub 저장소 GoogleCloudPlatform/golang-samples의 bigtable/usercounter 디렉터리에 있습니다.
코드 샘플 개요
이 코드 샘플은 먼저 Bigtable용 관리 클라이언트를 만듭니다. 그런 다음 클라이언트를 사용하여 사용자가 지정한 Bigtable 인스턴스에서 이름이 emails인 단일 column family가 있는 user-visit-counter 테이블을 확인합니다. 필요한 경우 테이블 및 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()
그런 다음 이후의 모든 읽기 및 쓰기에 사용되는 단일 Bigtable 클라이언트를 만듭니다.
client,err=bigtable.NewClient(ctx,project,instance)iferr!=nil{log.Fatalf("Unable to create data operations client. %v",err)}
마지막으로 App Engine 서버의 루트에 HTTP 핸들러를 추가합니다. 핸들러는 요청이 있을 때마다 사용자에게 로그인하라는 메시지를 필요에 따라 표시합니다. 그런 다음 사용자의 이메일 주소 행에서 Increment 작업을 수행하여 사용자의 방문을 추적합니다.
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}}
행을 증분하면 핸들러는 현재 사용자의 총 방문 수를 보여주는 HTML 페이지를 표시합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eThis is a Go-based App Engine application that uses Bigtable to track the number of visits from each user's Google account.\u003c/p\u003e\n"],["\u003cp\u003eThe application can be run locally in a Docker container or in the cloud within App Engine's flexible environment.\u003c/p\u003e\n"],["\u003cp\u003eUpon launch, the code creates a Bigtable administrative client to manage the \u003ccode\u003euser-visit-counter\u003c/code\u003e table and its \u003ccode\u003eemails\u003c/code\u003e column family, creating them if they don't exist.\u003c/p\u003e\n"],["\u003cp\u003eThe 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.\u003c/p\u003e\n"],["\u003cp\u003eThe app does not provide any access control for the user data and should be shutdown after testing.\u003c/p\u003e\n"]]],[],null,["Example: Go App Engine Application\n\nThis example is an [App Engine](/appengine) application, written in Go, that\nprovides a web interface that uses Bigtable to track the number of\nvisits from your Google account. It runs locally in a Docker container or in the\ncloud in App Engine's [flexible environment](/appengine/docs/flexible). The code for\nthis application is in the GitHub repository\n[GoogleCloudPlatform/golang-samples](https://github.com/GoogleCloudPlatform/golang-samples), in the directory\n`bigtable/usercounter`.\n| **Warning:** This application does not provide any access control for your data. Shut down the application when you are done testing.\n\nOverview of the code sample\n\nWhen the code sample is launched, it creates an administrative client for\nBigtable. It then uses the client to check the user-specified\nBigtable instance for a table named `user-visit-counter`, with a\nsingle column family named `emails`. If necessary, it creates the table and\ncolumn family: \n\n adminClient, err := bigtable.NewAdminClient(ctx, project, instance)\n if err != nil {\n \tlog.Fatalf(\"Unable to create a table admin client. %v\", err)\n }\n tables, err := adminClient.Tables(ctx)\n if err != nil {\n \tlog.Fatalf(\"Unable to fetch table list. %v\", err)\n }\n if !sliceContains(tables, tableName) {\n \tif err := adminClient.CreateTable(ctx, tableName); err != nil {\n \t\tlog.Fatalf(\"Unable to create table: %v. %v\", tableName, err)\n \t}\n }\n tblInfo, err := adminClient.TableInfo(ctx, tableName)\n if err != nil {\n \tlog.Fatalf(\"Unable to read info for table: %v. %v\", tableName, err)\n }\n if !sliceContains(tblInfo.Families, familyName) {\n \tif err := adminClient.CreateColumnFamily(ctx, tableName, familyName); err != nil {\n \t\tlog.Fatalf(\"Unable to create column family: %v. %v\", familyName, err)\n \t}\n }\n adminClient.Close()\n\nThe code sample then creates a single Bigtable client that is used\nfor all subsequent reads and writes: \n\n client, err = bigtable.NewClient(ctx, project, instance)\n if err != nil {\n \tlog.Fatalf(\"Unable to create data operations client. %v\", err)\n }\n\nFinally, the code sample adds an HTTP handler to the root of the App Engine\nserver. On each request, the handler prompts the user to log in if necessary. It\nthen tracks the user's visit by performing an `Increment` operation on the row\nfor the user's email address: \n\n tbl := client.Open(tableName)\n rmw := bigtable.NewReadModifyWrite()\n rmw.Increment(familyName, u.Email, 1)\n row, err := tbl.ApplyReadModifyWrite(ctx, u.Email, rmw)\n if err != nil {\n \treturn &appError{err, \"Error applying ReadModifyWrite to row: \" + u.Email, http.StatusInternalServerError}\n }\n\nAfter incrementing the row, the handler displays an HTML page showing the total\nnumber of visits by the current user."]]