본문 바로가기

Data

InfluxDB 2.* 클라이언트 샘플 테스트

제공되는 클라이언트 샘플 소스를 이용해서 조회 테스트를 해본다.

  • Go lang
package main

import (
	"context"
	"fmt"
	"github.com/influxdata/influxdb-client-go"
)

func main() {
	token := "..."
	org := "..."
	url := "http://...:9999"

	client := influxdb2.NewClient(url, token)
	queryApi := client.QueryApi(org)

	query := `from(bucket:"market")
	         |> range(start: -2d) 
	         //|> filter(fn: (r) => r._measurement == "tickers" and r.ticker == "AAPL" and r._field == "open")
	         |> filter(fn: (r) => r._measurement == "tickers" and r.ticker == "AAPL")
	       `
	result, err := queryApi.Query(context.Background(), query)
	if err == nil {
		for result.Next() {
			fmt.Printf("%v : %v %v %v\n", result.Record().Time(), result.Record().ValueByKey("ticker"), result.Record().Field(), result.Record().Value())
		}

		if result.Err() != nil {
			fmt.Printf("query parsing error: %s\n", result.Err().Error())
		}
	} else {
		panic(err)
	}

	client.Close()
}

 

  • Python
from influxdb_client import InfluxDBClient

token = '...'
org = '...'
url = ''http://...:9999'

client = InfluxDBClient(url=url, token=token, org=org)
query_api = client.query_api()


query = """from(bucket:"market") 
            |> range(start: -2d)
            |> filter(fn:(r) => r._measurement == "tickers")
            |> filter(fn: (r) => r["ticker"] == "AAPL")
        """
records = query_api.query_stream(query=query)
for record in records:
    print(record.get_time(), ":", record["ticker"], record.get_field(), record.get_value())

Flux 문법이 아직 익숙치 않긴 하지만 사용 방법은 그다지 어렵지 않다.

그런데, 1.* 에서는 field key 별로 자동으로 컬럼이 추가되는 식이었는데 2.* 에서는 _field 와 _value 로 나눠져서 저장된다. 가령 다음과 같은 Protocol 의 데이터가 있다면 

tickers,ticker=AAPL company="APPLE INC",close=175.05,high=175.06,low=174.95,open=175.05,volume=261381 1591518918684

V1.*

$>influx
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0
> use market Using database market
> select * from tickers where ticker = 'AAPL' and volume = 80184
name: tickers time close company high low open ticker volume
---- ----- ------- ---- --- ---- ------ ------
1511988900000000000 169.4493 APPLE INC 169.47 169.38 169.4 AAPL 80184

V2.* 에서는 각각의 field key 별로 새로운 Point 로 저장된다.

$ influx repl
> from(bucket:"market") |> range(start: -2d) |> filter(fn: (r) => r._measurement == "tickers" and r.ticker == "AAPL") |> limit(n: 1)

Result: _result

Table: keys: [_start, _stop, _field, _measurement, ticker]
_start:time _stop:time _field:string _measurement:string ticker:string _time:time _value:float
------------------------------ ------------------------------ ---------------------- ----------------------
2020-06-07T01:59:06.332088855Z 2020-06-09T01:59:06.332088855Z low tickers AAPL 2020-06-07T08:34:44.484000000Z 174.05

Table: keys: [_start, _stop, _field, _measurement, ticker] _start:time _stop:time _field:string _measurement:string ticker:string _time:time _value:float
------------------------------ ------------------------------ ---------------------- ----------------------
2020-06-07T01:59:06.332088855Z 2020-06-09T01:59:06.332088855Z close tickers AAPL 2020-06-07T08:34:44.484000000Z 174.05

Table: keys: [_start, _stop, _field, _measurement, ticker] _start:time _stop:time _field:string _measurement:string ticker:string _time:time _value:float
------------------------------ ------------------------------ ---------------------- ----------------------
2020-06-07T01:59:06.332088855Z 2020-06-09T01:59:06.332088855Z volume tickers AAPL 2020-06-07T08:34:44.484000000Z 34908
... 

달라진 이유가 있을텐데 RDB 에 익숙한 내겐 아직 낯설기만.

마침 눈에 이 기사가 들어왔다.

How was Flux received by the community?
It seems like InfluxData were pretty nervous to start with, but they got a lot of positive and constructive feedback leading to some pretty fast quality of life updates such as code suggestions in VS Code, but also some discoveries that their tech was being used in even more places than they originally realized. They’ve known from the start that InfluxData isn’t an island, but now they’ve started building bridges to islands they didn’t know about until specific use cases were brought to their attention by the community.

- https://jaxenter.com/influxdata-interview-flux-166581.html

덧) Go, Python 클라이언트 실행해보면 Go 가 훨씬 빠른 느낌.

'Data' 카테고리의 다른 글

[Python]pandas 의 to_sql 이용한 Bulk insert  (0) 2020.07.07
InfluxDB vs. TimescaleDB  (0) 2020.06.17
cassandra DB GUI 클라이언트로 DBeaver community 이용하기  (0) 2020.06.11
[InfluxDB] Why Flux?  (0) 2020.06.09
InfluxDB 2.* 설치  (0) 2020.06.04