LoginSignup
0
0

More than 1 year has passed since last update.

Influxdb-client-kotlinを使ってみた

Posted at

InfluxDBを使ってみたかったので、AndroidからKotlin SDKでwrite/readを試してみた。

準備 Clientライブラリの追加

ライブラリを追加する

app/build.gradle
    implementation "com.influxdb:influxdb-client-kotlin:3.1.0"

ライブラリバージョンは以下で確認できる。
2021/08/12時点では 3.1.0 が最新
https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin

実装

Androidで実装しているので、
View → Repository → ApiClientWrapper の順になるよう実装していく。
(ViewModelは 面倒 サンプルなのでスキップした)

View

MainActivity.kt
        // 本来はDIすべき
        val repository = ApiRepository(InfluxDBClientKotlinWrapper())

        lifecycleScope.launch {
            withContext(Dispatchers.IO) {
                // サンプル用書き込み値
                val writeValue = Random.nextDouble(62.0, 65.0)

                // InfluxDBに書き込み
                repository.add(writeValue)
                // InfuxDBから読み取り
                repository.fetch().collect { record ->
                    Log.d("FLUXRECORD", record.value.toString())
                }
            }
        }

Repository

ApiRepository.kt
class ApiRepository(private val influxDBClientWrapper: InfluxDBClientKotlinWrapper) {

    // これ必要?
    private val bucket = "k_ikemura's Bucket"

    /**
     * データ取得
     */
    fun fetch(): Flow<FluxRecord> {
        val query = """
from(bucket: "$bucket") 
|> range(start: 0) 
            """
        val results = influxDBClientWrapper.query(query)
        return results.consumeAsFlow()
    }

    /**
     * データ登録
     */
    suspend fun add(writeValue: Double) {
        influxDBClientWrapper.add(writeValue)
    }

ApiClientWrapper

InfluxDBClientKotlinWrapper.kt
class InfluxDBClientKotlinWrapper {
    private val token = "dummy_token" // influxdb-cloudや自分の環境で用意する
    private val org = "dummy_org"
    private val bucket = "sample Bucket"
    private val url = "dummy_url"

    // 公式ライブラリのClientクラス TODO:DIで渡したい
    private val client: InfluxDBClientKotlin = InfluxDBClientKotlinFactory.create(
        url = url,
        token = token.toCharArray(),
        org = org,
        bucket = bucket
    )

    // 読み込み実行
    fun query(query: String): Channel<FluxRecord> = client.getQueryKotlinApi().query(query)

    // 使用が終わったらクローズする必要あり?(公式サンプルではクローズしてた)
    fun cloese() {
        client.close()
    }

    // 書き込み実行
    suspend fun add(value: Double) {
        val writeApi = client.getWriteKotlinApi()
        // 書き込み用データ、公式サンプルのコピペなので正式な値に修正すべき
        val temperature = Temperature("south", value, Instant.now())
        writeApi.writeMeasurement(temperature, WritePrecision.NS)
    }

上記を実行して、InfluxDB-Cloudで書き込み値の確認できたので、正常に動いてるっぽい。

参考にした情報

ライブラリのGitHub
https://github.com/influxdata/influxdb-client-java/tree/master/client-kotlin

公式ライブラリのドキュメント
https://docs.influxdata.com/influxdb/cloud/query-data/get-started/

ライブラリバージョン
https://mvnrepository.com/artifact/com.influxdb/influxdb-client-kotlin

0
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
0
0