Category: InfluxDB

  • Flux 작성하기

    InfluxDB v2.0 부터 Flux 라는 형식으로 데이터를 추출하도록 해야 한다고 한다.

    ICU03번으로 입력된 SpO2 1일치 자료는 다음과 같이 입력하면 깔끔하게 출력된다. map 부분을 저렇게 해주지 않으면 예쁘지 않은 label로 보인다.

    from(bucket: "measurements")
    |> range(start:-1d)
    |> filter(fn: (r) => r._measurement == "vital" and r.bed  == "ICU03" and r._field == "spo2")
    |>  map(fn: (r) => ({ _value:r._value, _time:r._time, _field:"SpO2" }))
  • API로 결과 입력하기

    InfluxDB의 경우 ns 단위로 시간 결과를 입력할 수 있다고 한다. 나의 경우에는 ms도 과할 정도고 s 단위 정도면 충분하다.

    기본적으로 다음과 같은 식으로 데이터를 준비해야 한다.

    빈 칸(white space)는 두 번 들어갈 수 있다. 첫 번째는 tag와 field 사이이고 두 번째는 field와 timestamp 사이이다.

    꼭 필요한 값은 measurement 와 field 이다. timestamp 의 경우 입력하지 않으면 host 시간을 기준으로 입력된다.

    curl --request POST \
    "http://localhost:8086/api/v2/write?org=rcc&bucket=measurements&precision=s" \
      --header "Authorization: Token MY_TOKEN_HERE" \
      --header "Content-Type: text/plain; charset=utf-8" \
      --header "Accept: application/json" \
      --data-binary '
        vital,bed=ICU01 spo2=99
        '

    curl 로 입력하려면 위와 같은 식으로 준비하면 된다.

    Python의 Requests로 입력하려면 다음과 같이 하면 된다. 준비 과정에서 따옴표가 지나치게 많이 들어가기 때문에 최대한 dictionary 타입으로 준비하는 것이 좋다. 위에서 언급했지만 precision은 second로 하도록 한다.

    headers = {
        "Authorization": "Token My_TOKEN_HERE",  
        "Content-Type": "text/plain; charset=utf-8",
        "Accept": "application/json"
    }
    
    requests.post(
    "http://rcc.work:8086/api/v2/write?org=rcc&bucket=measurements&precision=s",
    headers=headers,
    data=data_influx_3
    )
  • InfluxDB 설치

    현재 최신 버젼은 v2.7이다.

    기존에 설치되어 있는 컨테이너를 삭제하고 설치한다.

    sudo docker rm -f influxdb
    sudo docker run -it \
      --network rcc \
      -p 8086:8086 \
      --restart always \
      --name influxdb \
      debian

    컨테이너 기본을 설정한다.

    echo exit 0 > /usr/sbin/policy-rc.d
    echo 'Acquire::http::Proxy "http://rcc.work:3142";' > /etc/apt/apt.conf
    rm -rf /etc/localtime; ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime 
    
    apt -y update && apt -y upgrade && apt -y install wget nano curl procps

    설치 편의를 위하여 미리 다운받은 패키지를 다운받고 설치한다.

    cd ~
    wget http://rcc.work:3143/influxdb2-2.7.0-amd64.deb
    dpkg -i influxdb2-2.7.0-amd64.deb
    

    다음과 같은 명령어를 사용하여 서비스를 실행한다.

    # service influxdb start
    Starting influxdb...
    influxdb process was started [ OK ]
    

    다음에는 웹으로 접속해서 organization과 bucket 이름, 보관 기한 등을 설정한다. 결과 입력용 token을 별도로 발급하는 것이 권한 관리에 좋다.