데이터베이스 만들기
먼저 데이터베이스를 만들어야 한다.
influx를 실행한다. 그리고 weather란 데이터베이스를 만들자.
$ influx
Connected to http://localhost:8086 version 1.8.9
InfluxDB shell version: 1.8.9
> create database weather
Python 코드 예
만들고 나면 아래처럼 코드를 적고 실행하자.
influxdb 파이썬 팩키지 설치가 필요하다.
관계형 데이터베이스와 influxDB의 용어 차이를 다시 보면 아래와 같다.
관계형 데이터베이스 RDB | influxDB v1.8 | influxDB v2.0 |
database | database | bucket |
table | measurement | measurement |
indexed column | tag | tag |
unindexed column | field | field |
json_body = [
{
"measurement": "temperature",
"tags": {
"country": "Korea",
"location": "Seoul"
},
"time": datetime.datetime.now(),
"fields": {
"Celsius": -7.0
}
}
]
위 json_body는 결국 measurement 테이블을 temperature라고 만들고, 인덱싱이 필요한 tag에 country국가와 location위치를 추가한 것이다. 필드에는 섭씨celsius로 섭씨 온도를 적었다.
실행 결과 아래처럼 입력된 것을 확인할 수 있다.
ResultSet({'('temperature', None)': [{'time': '2022-01-12T21:29:51.348695Z', 'Celsius': -7.0, 'country': 'Korea', 'location': 'Seoul'}]})
chronograf로 확인해 보면 아래처럼 데이터를 확인할 수 있다.
이때 주의할 점은 tag명은 홑따옴표로 'Korea'로 표시해야 한다는 점이다. 겹따옴표를 쓰면 결과가 나오지 않는다.
SELECT "Celsius" FROM "weather"."autogen"."temperature" WHERE "country"='Korea' AND "location"='Seoul'
기상청 API를 활용해서 온도를 주기적으로 입력한다면 그래프로 볼 수 있다.
관련 글
시계열 데이터베이스Time Series Database, influxDB 소개와 설치 방법
시계열 데이터베이스Time Series Database, influxDB v1.8 튜토리얼, 데이터 수집과 조회 예제
시계열 데이터베이스Time Series Database, influxDB v1.8 튜토리얼, python으로 데이터 입력과 조회하기