Go Client

Prev Next

Available in VPC

It describes how to use Go Client provided by Data Query.

Preparations

Go 1.22 or later must be installed in the environment where you will be using Go Client.

How to use

This guide describes how to use Data Query Go Client.

1. Install package

Install the dataquery-go-client package available on pkg.go.dev.

go get github.com/NaverCloudPlatform/dataquery-go-client

2. Go Client integration examples

Authentication method

The NCP IAM authentication methods are supported.
You must enter your AccessKey and SecretKey in the DSN strings.

Submit queries and view results

Write the code to submit the query and view the results.

Example:

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/NaverCloudPlatform/dataquery-go-client/trino"
)

func main() {

    accessKey := "_your_access_key_"
    secretKey := "_your_secret_key_"
    endpoint  := "_dataquery_endpoint_"

    dsn := fmt.Sprintf("https://%s:%s@%s?catalog=data_catalog&schema=default", accessKey, secretKey, endpoint)
    db, err := sql.Open("dataquery", dsn)

    if err != nil {
        log.Fatalf("Failed to connect to DataQuery: %v", err)
    }
    defer db.Close()

    if err := db.Ping(); err != nil {
        log.Fatalf("Ping failed: %v", err)
    }

    query := "SELECT * FROM public_data.korea_national_railway.subway_seoul_capital_area LIMIT 10"
    rows, err := db.Query(query)
    if err != nil {
        log.Fatalf("Query failed: %v", err)
    }
    defer rows.Close()

    columns, err := rows.Columns()
    if err != nil {
        log.Fatalf("Failed to get columns: %v", err)
    }
    for rows.Next() {
        values := make([]interface{}, len(columns))
        valuePtrs := make([]interface{}, len(columns))
        for i := range values {
            valuePtrs[i] = &values[i]
        }
        if err := rows.Scan(valuePtrs...); err != nil {
            log.Fatalf("Failed to scan row: %v", err)
        }
        for i := range columns {
            val := values[i]
            if b, ok := val.([]byte); ok {
                fmt.Print(string(b))
            } else {
                fmt.Print(val)
            }
            if i < len(columns)-1 {
                fmt.Print(" ")
            }
        }
        fmt.Println()
    }
}
// Result examples
Pangyo (Pangyo Techno Valley), Gyeonggang Line operated by Korail 127.111135 37.394798
Imae, Gyeonggang Line operated by Korail 127.128272 37.395846
Samdong, Gyeonggang Line operated by Korail 127.203279 37.408654
Gyeonggi Gwangju, Gyeonggang Line operated by Korail 127.253201 37.398974
Chowol, Gyeonggang Line operated by Korail 127.299996 37.373389
Gonjiam, Gyeonggang Line operated by Korail 127.346193 37.350518
Sindundoyechon, Gyeonggang Line operated by Korail 127.405251 37.315499
Icheon, Gyeonggang Line operated by Korail 127.442135 37.264222
Bubal, Gyeonggang Line operated by Korail 127.490303 37.260411
Sejongdaewangneung, Gyeonggang Line operated by Korail 127.570764 37.293636

Available options

  • Enter it as the argument value of DSN strings.
  • Example:
    https://_accessKey_:_secretKey_@_endpoint_?catalog=data_catalog&schema=default&reuse_query_result=true&reuse_query_max_age=10
Option name Data type Input values Required Note
accessKey String User accessKey Y
secretKey String User secretKey Y
endpoint String Data Query Endpoint Y For more information, see Data Query Service Endpoint by Region
reuse_query_result Boolean true or false N * default : false
reuse_query_max_age Integer 1 ~ 10,080 N * default : 60
* Used only if the value of reuse-query-result is true. (Unit: time)

Data Query Service Endpoint by Region
Currently, only the KR region is available, with more Regions to be provided later.

Region Endpoint
KR kr.dataquery.naverncp.com
Note

dataquery-go-client is provided based on trino-go-client. For more information beyond the options above, see Trino documentation.