influxdata / influxdb1-client

The old clientv2 for InfluxDB 1.x
MIT License
190 stars 112 forks source link

Is there any way to use this lib to create a InfluxDB database #46

Open mong0520 opened 4 years ago

mong0520 commented 4 years ago

I'd like to use this lib to create a new InfluxDB database like the SQL command create database xxx. Is it possible to do that?

jknutson commented 3 years ago

There is a NewQuery function in the client package: https://github.com/influxdata/influxdb1-client/blob/master/v2/client.go#L471-L473

You could probably just change the example in the readme to execute the query that you wish to run:

func ExampleClient_query() {
    c, err := client.NewHTTPClient(client.HTTPConfig{
        Addr: "http://localhost:8086",
    })
    if err != nil {
        fmt.Println("Error creating InfluxDB Client: ", err.Error())
    }
    defer c.Close()

    q := client.NewQuery("create database xxx", "", "")
    if response, err := c.Query(q); err == nil && response.Error() == nil {
        fmt.Println(response.Results)
    }
}

I've not tested this, but it might work.

Hope this helps!