如何使用一个命令在ClickHouse中使用Golang插入CSV文件

huangapple go评论82阅读模式
英文:

How to insert csv file using one command in clickhouse using golang

问题

这是一个关于使用go库https://github.com/ClickHouse/clickhouse-go插入csv文件的问题。你想知道是否有一种方法可以在一个命令中完成插入操作,而不需要读取csv文件并迭代内容。如果有这样的方法,你能给我提供一个示例吗?

如果没有这样的方法,你想知道如何将这个系统命令转换为使用os/exec库编写的golang代码。

cat /home/srijan/employee.csv | clickhouse-client --query="INSERT INTO test1 FORMAT CSV"

英文:

Is there a way to insert csv file using this go library https://github.com/ClickHouse/clickhouse-go in one command (without reading csv and iterating through the content.). If there is a way can you provide me with the example.
if not how can we convert this system command and write it in golang using os/exec library.
cat /home/srijan/employee.csv | clickhouse-client --query="INSERT INTO test1 FORMAT CSV"

答案1

得分: 0

这是一个使用Go语言编写的示例代码,它使用HTTP API与ClickHouse数据库进行通信。你可以使用这个代码来将数据插入到ClickHouse中。

package main

import (
        "compress/gzip"
        "fmt"
        "io"
        "io/ioutil"
        "net/http"
        "net/url"
        "os"
)

func compress(data io.Reader) io.Reader {
        pr, pw := io.Pipe()
        gw, err := gzip.NewWriterLevel(pw, int(3))
        if err != nil {
                panic(err)
        }

        go func() {
                _, _ = io.Copy(gw, data)
                gw.Close()
                pw.Close()
        }()

        return pr
}

func main() {
        p, err := url.Parse("http://localhost:8123/")
        if err != nil {
                panic(err)
        }
        q := p.Query()

        q.Set("query", "INSERT INTO test1 FORMAT CSV")
        p.RawQuery = q.Encode()
        queryUrl := p.String()

        var req *http.Request


        req, err = http.NewRequest("POST", queryUrl, compress(os.Stdin))
        req.Header.Add("Content-Encoding", "gzip")

        if err != nil {
                panic(err)
        }

        client := &http.Client{
                Transport: &http.Transport{DisableKeepAlives: true},
        }
        resp, err := client.Do(req)
        if err != nil {
                panic(err)
        }
        defer resp.Body.Close()

        body, _ := ioutil.ReadAll(resp.Body)

        if resp.StatusCode != 200 {
                panic(fmt.Errorf("clickhouse response status %d: %s", resp.StatusCode, string(body)))
        }
}

这段代码使用了gzip压缩算法对数据进行压缩,并通过HTTP POST请求将压缩后的数据发送到ClickHouse数据库。在发送请求时,需要设置请求头的Content-Encoding为gzip,以告知服务器数据已经被压缩。

请注意,这段代码中的URL是"http://localhost:8123/",你需要将其替换为你实际使用的ClickHouse数据库的URL。另外,这段代码假设你的数据是从标准输入(os.Stdin)中读取的,你可以根据实际情况修改代码以适应你的数据来源。

英文:

It's impossible with that go library. You can use http api https://clickhouse.com/docs/en/interfaces/http/ and any http go client

for example

package main

import (
        "compress/gzip"
        "fmt"
        "io"
        "io/ioutil"
        "net/http"
        "net/url"
        "os"
)

func compress(data io.Reader) io.Reader {
        pr, pw := io.Pipe()
        gw, err := gzip.NewWriterLevel(pw, int(3))
        if err != nil {
                panic(err)
        }

        go func() {
                _, _ = io.Copy(gw, data)
                gw.Close()
                pw.Close()
        }()

        return pr
}

func main() {
        p, err := url.Parse("http://localhost:8123/")
        if err != nil {
                panic(err)
        }
        q := p.Query()

        q.Set("query", "INSERT INTO test1 FORMAT CSV")
        p.RawQuery = q.Encode()
        queryUrl := p.String()

        var req *http.Request


        req, err = http.NewRequest("POST", queryUrl, compress(os.Stdin))
        req.Header.Add("Content-Encoding", "gzip")

        if err != nil {
                panic(err)
        }

        client := &http.Client{
                Transport: &http.Transport{DisableKeepAlives: true},
        }
        resp, err := client.Do(req)
        if err != nil {
                panic(err)
        }
        defer resp.Body.Close()

        body, _ := ioutil.ReadAll(resp.Body)

        if resp.StatusCode != 200 {
                panic(fmt.Errorf("clickhouse response status %d: %s", resp.StatusCode, string(body)))
        }
}

huangapple
  • 本文由 发表于 2021年12月20日 19:34:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/70421191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定