229.6.5. 기타 작업

229.6.5.1. 집계

본문에 포함된 지정된 파이프라인으로 집계를 수행합니다. 집계는 길고 복잡한 작업이 될 수 있습니다. 보살피와 함께 사용하십시오.

// route: from("direct:aggregate").to("mongodb3:myDb?database=science&collection=notableScientists&operation=aggregate");
List<Bson> aggregate = Arrays.asList(match(or(eq("scientist", "Darwin"), eq("scientist",
        group("$scientist", sum("count", 1)));
from("direct:aggregate")
    .setBody().constant(aggregate)
    .to("mongodb3:myDb?database=science&collection=notableScientists&operation=aggregate")
    .to("mock:resultAggregate");

다음 IN 메시지 헤더를 지원합니다.

헤더 키빠른 일정description (MongoDB API doc에서 추출)예상 유형

CamelMongoDbBatchSize

MongoDbConstants.BATCH_SIZE

일괄 처리별로 반환할 문서 수를 설정합니다.

int/Integer

CamelMongoDbAllowDiskUse

MongoDbConstants.ALLOW_DISK_USE

집계 파이프라인 단계를 활성화하여 임시 파일에 데이터를 작성할 수 있습니다.

부울/Boolean

기본적으로 모든 결과 목록이 반환됩니다. 이는 결과 크기에 따라 메모리에 무거울 수 있습니다. 더 안전한 대안은 outputType=MongoIterable을 설정하는 것입니다. 다음 프로세서는 메시지 본문에서 반복 가능한 결과를 확인하여 결과를 하나씩 진행할 수 있습니다. 따라서 배치 크기를 설정하고 반복 가능한 값을 반환하면 결과를 효율적으로 검색하고 처리할 수 있습니다.

위의 헤더를 설정하는 것보다 더 쉬운 엔드 포인트 옵션으로 outputType=DBCursor (Camel 2.21+)를 포함하여 서버에서 반환된 문서를 경로에 "스트림"할 수 있습니다. 이를 통해 Mongo 드라이버에서 DBCursor를 직접 처리하며 Mongo 쉘 내에서 aggregate()를 실행하는 것처럼 경로가 결과를 반복할 수 있습니다. 기본적으로 이 구성 요소에서는 드라이버의 커서에서 문서를 목록으로 로드하고 이를 경로로 반환하므로 메모리 내 개체가 많은 수가 발생할 수 있습니다. DBCursor가 일치하는 문서 수를 요청하지 않음을 기억하십시오. 자세한 내용은 MongoDB 문서 사이트를 참조하십시오.

옵션 outputType=MongoIterable 및 배치 크기가 있는 예:

// route: from("direct:aggregate").to("mongodb3:myDb?database=science&collection=notableScientists&operation=aggregate&outputType=MongoIterable");
List<Bson> aggregate = Arrays.asList(match(or(eq("scientist", "Darwin"), eq("scientist",
        group("$scientist", sum("count", 1)));
from("direct:aggregate")
    .setHeader(MongoDbConstants.BATCH_SIZE).constant(10)
    .setBody().constant(aggregate)
    .to("mongodb3:myDb?database=science&collection=notableScientists&operation=aggregate&outputType=MongoIterable")
    .split(body())
    .streaming()
    .to("mock:resultAggregate");

.split(body()) 을 호출하면 경로 아래에 항목을 하나씩 보낼 수 있지만 모든 항목이 메모리에 먼저 로드됩니다. 따라서 .streaming() 호출은 일괄 처리를 통해 메모리에 데이터를 로드해야 합니다.