개요
이 가이드 에서는 스칼라 운전자 사용하여 Time Series 데이터 저장 하고 상호 작용 방법을 학습 수 있습니다.
시계열 데이터는 다음 구성 요소로 구성됩니다.
측정 수량
측정 타임스탬프
측정값을 설명하는 메타데이터
다음 표에서는 Time Series 데이터 를 저장 수 있는 샘플 상황을 설명합니다.
상황 | 측정 수량 | Metadata |
|---|---|---|
산업별 월별 매출 기록 | USD 기준 수익 | 회사, 국가 |
날씨 변화 추적 | 강수량 수준 | 위치, 센서 유형 |
기록적인 주택 가격 변동 | 월 임대료 | 위치, 통화 |
Time Series 컬렉션 만들기
중요
Time Series 컬렉션의 서버 버전
Time Series 컬렉션을 만들고 상호 작용하려면 MongoDB Server 5.0 이상을 실행하는 배포에 연결되어 있어야 합니다.
time series 컬렉션 을 만들어 Time Series 데이터 를 저장 수 있습니다. time series 컬렉션 을 만들려면 createCollection() 메서드에 다음 매개변수를 전달합니다.
생성할 새 컬렉션의 이름
메서드를 사용하여 TimeSeriesOptions가 설정하다CreateCollectionOptions 객체
timeSeriesOptions()
예시
이 예시 timeField 옵션을 "timestamp" 필드 로 설정하다 하고 metaField 옵션을 "location" 필드 로 설정하다 fall_weather 데이터베이스 에 october2024 time series 컬렉션 생성합니다.
val database = mongoClient.getDatabase("fall_weather") val tsOptions = TimeSeriesOptions("timestamp").metaField("location") val collectionOptions = CreateCollectionOptions().timeSeriesOptions(tsOptions) val createObservable = database.createCollection("october2024", collectionOptions) Await.result(createObservable.toFuture(), Duration(10, TimeUnit.SECONDS))
time series 컬렉션이 성공적으로 생성되었는지 확인하려면 데이터베이스에서 listCollections() 메서드를 실행하고 결과를 출력합니다.
val listObservable = database.listCollections() val list = Await.result(listObservable.toFuture(), Duration(10, TimeUnit.SECONDS)) val jsonSettings = JsonWriterSettings.builder().indent(true).build() list.foreach(collection => { println(collection.toJson(jsonSettings)) })
{ "name": "october2024", "type": "timeseries", "options": { "timeseries": { "timeField": "temperature", "granularity": "seconds", "bucketMaxSpanSeconds": 3600 } }, "info": { "readOnly": false } } ...
Time Series 데이터 저장
insertOne() 또는 insertMany() 메서드를 사용하고 삽입된 각 문서 에 측정값, 타임스탬프 및 메타데이터 를 지정하여 time series 컬렉션 에 데이터를 삽입할 수 있습니다.
팁
컬렉션 에 문서를 삽입하는 방법에 학습 보려면 문서 삽입 가이드 를 참조하세요.
예시
이 예시 에서는 Time Series 컬렉션 만들기 예시 에서 만든 october2024 Time Series time series 컬렉션 에 뉴욕시 온도 데이터를 삽입합니다. 각 문서 에는 다음 필드가 포함되어 있습니다.
temperature, 온도 측정값을 화씨 단위로 저장합니다.location위치 메타데이터 를 저장합니다.timestamp, 측정 컬렉션 의 시간을 저장합니다.
val collection = database.getCollection("october2024") val temperatures = Seq( Document("timestamp" -> BsonDateTime(1727755200000L), "temperature" -> 54, "location" -> "New York City"), Document("timestamp" -> BsonDateTime(1727841600000L), "temperature" -> 55, "location" -> "New York City"), ) val insertObservable = collection.insertMany(temperatures) Await.result(insertObservable.toFuture(), Duration(10, TimeUnit.SECONDS))
Time Series 데이터 쿼리
다른 컬렉션에서 읽기 또는 집계 작업을 수행할 때 사용하는 것과 동일한 구문 및 규칙을 사용하여 time series 컬렉션 에 저장된 데이터를 쿼리 할 수 있습니다. 이러한 작업에 학습 보려면 추가 정보 섹션을 참조하세요.
추가 정보
이 가이드 에 언급된 개념에 학습 보려면 다음 MongoDB Server 매뉴얼 항목을 참조하세요.
읽기 작업 수행에 대해 자세히 학습 데이터 읽기를 참조하세요.
집계 작업 수행에 학습 보려면 애그리 게이션을 통한 데이터 변환 가이드 를 참조하세요.
API 문서
이 가이드 에 언급된 메서드에 학습 보려면 다음 API 설명서를 참조하세요.