Interaction between Golang functions and distributed databases in distributed systems

王林
Release: 2024-04-19 15:06:01
Original
676 people have browsed it

In a distributed system, Go functions can interact with distributed databases. The specific steps are as follows: Install the necessary dependencies. Use the spanner.NewClient function to connect to the database. Use the Query method to execute the query and get the iterator. Use the Do method to iterate through the query results and process the data. After the query is complete, use the Close method to close the connection.

分布式系统中 Golang 函数与分布式数据库的交互

Interaction between Go functions and distributed databases in distributed systems

In distributed systems, it is a common requirement for Go functions to interact with distributed databases. This article guides you through using Go code to connect to and query a distributed database.

Install dependencies

The first step is to install the necessary dependencies:

import (
    "context"
    "fmt"
    "log"

    "cloud.google.com/go/spanner"
)
Copy after login

Connect to the database

To connect to the database, use spanner.NewClient Function:

client, err := spanner.NewClient(context.Background(), "projects/my-project", "instances/my-instance", "databases/my-database")
if err != nil {
    log.Fatal(err)
}
Copy after login

Execute query

To execute a query, use Query Method:

stmt := spanner.Statement{SQL: `SELECT * FROM Singers`}
iter := client.Single().Query(context.Background(), stmt)
defer iter.Stop()
Copy after login

iter is an iterator that will iterate over the query results.

Processing the results

To process the results, please use the Do method:

for {
    row, err := iter.Next()
    if err == iterator.Done {
        break
    }
    if err != nil {
        log.Fatal(err)
    }
    var singerId int64
    var firstName string
    var lastName string
    if err := row.ColumnByName("SingerId", &singerId); err != nil {
        log.Fatal(err)
    }
    if err := row.ColumnByName("FirstName", &firstName); err != nil {
        log.Fatal(err)
    }
    if err := row.ColumnByName("LastName", &lastName); err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%d %s %s\n", singerId, firstName, lastName)
}
Copy after login

Close the connection

After the query is completed, please close Connection to the database:

client.Close()
Copy after login

Practical case

The following is a complete Go code example showing how to connect to the Cloud Spanner database and execute queries:

package main

import (
    "context"
    "fmt"
    "log"

    "cloud.google.com/go/spanner"
)

func main() {
    client, err := spanner.NewClient(context.Background(), "projects/my-project", "instances/my-instance", "databases/my-database")
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    stmt := spanner.Statement{SQL: `SELECT * FROM Singers`}
    iter := client.Single().Query(context.Background(), stmt)
    defer iter.Stop()

    for {
        row, err := iter.Next()
        if err == iterator.Done {
            break
        }
        if err != nil {
            log.Fatal(err)
        }
        var singerId int64
        var firstName string
        var lastName string
        if err := row.ColumnByName("SingerId", &singerId); err != nil {
            log.Fatal(err)
        }
        if err := row.ColumnByName("FirstName", &firstName); err != nil {
            log.Fatal(err)
        }
        if err := row.ColumnByName("LastName", &lastName); err != nil {
            log.Fatal(err)
        }
        fmt.Printf("%d %s %s\n", singerId, firstName, lastName)
    }
}
Copy after login

The above is the detailed content of Interaction between Golang functions and distributed databases in distributed systems. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template