2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

InfluxDBを操作する

Posted at

InfluxDBとは

オープンソースの時系列データベース

データベースの操作

基本的に、RestAPIで操作するようです。
こちらのサイトにドキュメントがあります。これに沿って使ってみます。

データベースを作る

$ curl -XPOST 'http://localhost:8086/query' --data-urlencode 'q=CREATE DATABASE "mydb"'
{"results":[{"statement_id":0}]}

データを登録する

InfluxDBのデータベースはポイント(points)という単位で保存します。

ポイントは4つの要素から構成されます。メジャメント(measurement)、タグセット(tagset)、フィールドセット(fieldset)、タイムスタンプ(timestamp)です。

用語 説明
メジャメント(measurement) タグセットあるいはフィールドセットが異なる関連するポイントを関連付けるために利用
タグ(tag) フィールドに付与するメタデータ。key=valueで表現される。この場合valueは文字列。タグは複数付与することができる。データの検索はタグでフィルタすることで高速化されるため、複数の有効なタグを持つことは重要
タグセット(tagset) タグの集まり
フィールド(field) key=valueで表される実際のデータ。値は整数・浮動小数点数・文字列・真偽値、のいずれかとなる。
フィールドセット(fieldset) フィールドの集まり
$ curl -i -XPOST "http://localhost:8086/write?db=mydb&precision=ms" --data-binary "test,tag=1 key1=1.1,key2=2.5"
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: c8b0c59e-ce78-11ea-8006-0242ac120003
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.8.1
X-Request-Id: c8b0c59e-ce78-11ea-8006-0242ac120003
Date: Sat, 25 Jul 2020 13:14:34 GMT

時間を省略すると、その登録した時刻で記録されます。

登録したデータを取得する

$ curl -G http://localhost:8086/query?db=mydb --data-urlencode "q=SELECT * FROM test"
{"results":[{"statement_id":0,"series":[{"name":"test","columns":["time","key1","key2","tag"],"values":[["2020-07-25T13:14:34.664Z",1.1,2.5,"1"]]}]}]}

検索結果を整形する

$ curl -G 'http://localhost:8086/query?pretty=true' --data-urlencode "db=mydb" --data-urlencode "q=SELECT * FROM test"
{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "test",
                    "columns": [
                        "time",
                        "key1",
                        "key2",
                        "tag"
                    ],
                    "values": [
                        [
                            "2020-07-25T13:14:34.664Z",
                            1.1,
                            2.5,
                            "1"
                        ],
                        [
                            "2020-07-25T13:15:45.632Z",
                            2.1,
                            3.5,
                            "1"
                        ]
                    ]
                }
            ]
        }
    ]
}
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?