Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Esempio: applicazione App Engine Go
Questo esempio è un'applicazione App Engine, scritta in Go, che
fornisce un'interfaccia web che utilizza Bigtable per monitorare il numero di
visite dal tuo Account Google. Viene eseguito localmente in un container Docker o nel cloud nell'ambiente flessibile di App Engine. Il codice di
questa applicazione si trova nel repository GitHub
GoogleCloudPlatform/golang-samples, nella directory
bigtable/usercounter.
Panoramica del esempio di codice
Quando viene avviato, il esempio di codice crea un client amministrativo per
Bigtable. Il client viene quindi utilizzato per controllare l'istanza Bigtable specificata dall'utente per una tabella denominata user-visit-counter, con una singola famiglia di colonne denominata emails. Se necessario, crea la tabella e
la famiglia di colonne:
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()
Il esempio di codice crea quindi un singolo client Bigtable che viene utilizzato
per tutte le operazioni di lettura e scrittura successive:
client,err=bigtable.NewClient(ctx,project,instance)iferr!=nil{log.Fatalf("Unable to create data operations client. %v",err)}
Infine, l'esempio di codice aggiunge un gestore HTTP alla radice del server App Engine. A ogni richiesta, il gestore chiede all'utente di accedere, se necessario. Successivamente, tiene traccia della visita dell'utente eseguendo un'operazione Increment sulla riga
per l'indirizzo email dell'utente:
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}}
Dopo aver incrementato la riga, il gestore visualizza una pagina HTML che mostra il numero totale
di visite dell'utente corrente.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 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."]]