Docs Menu

์ปค์„œ์—์„œ ๋ฐ์ดํ„ฐ ์•ก์„ธ์Šค

์ด ๊ฐ€์ด๋“œ ์—์„œ๋Š” Java Reactive Streams ์šด์ „์ž ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปค์„œ ์—์„œ ๋ฐ์ดํ„ฐ์— ์•ก์„ธ์Šค ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ปค์„œ๋Š” ์ฝ๊ธฐ ์ž‘์—…์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๋ฐฐ์น˜๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ž…๋‹ˆ๋‹ค. ์ปค์„œ๋Š” ์ฃผ์–ด์ง„ ์‹œ๊ฐ„์— ๋ฌธ์„œ์˜ ํ•˜์œ„ ์ง‘ํ•ฉ๋งŒ ๋ณด์œ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปค์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์†Œ๋น„์™€ ๋„คํŠธ์›Œํฌ ๋Œ€์—ญํญ ์‚ฌ์šฉ๋Ÿ‰์„ ๋ชจ๋‘ ์ค„์ž…๋‹ˆ๋‹ค.

In the Java Reactive Streams driver, some streams are backed by cursors. The size of batches used in these underlying cursors depends on the demand requested on the Subscription for the Publisher. You can set the batch size of data contained by each underlying cursor by using the FindPublisher.batchSize() method.

์ด ๊ฐ€์ด๋“œ ์˜ ์˜ˆ์ œ์—์„œ๋Š” Atlas ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ ์˜ sample_restaurants.restaurants ์ปฌ๋ ‰์…˜ ์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ฌด๋ฃŒ MongoDB Atlas cluster ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ ์„ธํŠธ๋ฅผ ๋กœ๋“œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šต ๋ณด๋ ค๋ฉด ์‹œ์ž‘ํ•˜๊ธฐ ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ค‘์š”

ํ”„๋กœ์ ํŠธ ๋ฆฌ์•กํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์ด ๊ฐ€์ด๋“œ Project Reactor ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Java Reactive Streams ์šด์ „์ž ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ˜ํ™˜๋œ Publisher ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ Reactor ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ํ•™์Šต Reactor ๋ฌธ์„œ์—์„œ ์‹œ์ž‘ํ•˜๊ธฐ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ด ๊ฐ€์ด๋“œ ์—์„œ Project Reactor ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ํ•™์Šต MongoDB ์— ๋ฐ์ดํ„ฐ ์“ฐ๊ธฐ ๊ฐ€์ด๋“œ ์ฐธ์กฐํ•˜์„ธ์š”.

์ปค์„œ ์˜ ๋‚ด์šฉ์„ ๋ฐ˜๋ณตํ•˜๋ ค๋ฉด ๋‹ค์Œ ์˜ˆ์‹œ ์™€ ๊ฐ™์ด Flux.from() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

FindPublisher<Document> findPublisher = collection.find();
Flux.from(findPublisher)
.doOnNext(x -> System.out.println(x.getString("name")))
.blockLast();

๊ฒฝ๊ณ 

์ฟผ๋ฆฌ์—์„œ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์„œ์˜ ์ˆ˜์™€ ํฌ๊ธฐ๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ถฉ๋Œํ•ฉ๋‹ˆ๋‹ค. ํฐ ๊ฒฐ๊ณผ ์„ธํŠธ๊ฐ€ ์˜ˆ์ƒ๋˜๋Š” ๊ฒฝ์šฐ ์ปค์„œ์— ๋ฐ˜๋ณต์ ์œผ๋กœ ์•ก์„ธ์Šคํ•ฉ๋‹ˆ๋‹ค.

์ปค์„œ ์—์„œ ๋ชจ๋“  ๋ฌธ์„œ๋ฅผ ์กฐํšŒ ํ•˜๋ ค๋ฉด ๋‹ค์Œ ์˜ˆ์‹œ ์™€ ๊ฐ™์ด ์ปค์„œ ๋ฅผ List ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

FindPublisher<Document> findPublisher = collection.find(Filters.eq("name", "Dunkin' Donuts"));
List<Document> resultsList = Flux.from(findPublisher).collectList().block();

๊ณ ์ • ์‚ฌ์ด์ฆˆ ๊ณ ์ • ์‚ฌ์ด์ฆˆ ์ปฌ๋ ‰์…˜ ์„ ์ฟผ๋ฆฌํ•  ๋•Œ๋Š” ํด๋ผ์ด์–ธํŠธ ๊ฐ€ ์ปค์„œ ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•œ ํ›„์—๋„ ๊ณ„์† ์—ด๋ ค ์žˆ๋Š” ํ…Œ์ผ ์ปค์„œ( tailable cursor )๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ณ ์ • ์‚ฌ์ด์ฆˆ ๊ณ ์ • ์‚ฌ์ด์ฆˆ ์ปฌ๋ ‰์…˜ ์— ํ…Œ์ผ ์ปค์„œ( tailable cursor )๋ฅผ ๋งŒ๋“ค๋ ค๋ฉด FindPublisher ๊ฐ์ฒด ์˜ cursorType() ๋ฉ”์„œ๋“œ์— CursorType.TailableAwait ๊ฐ’์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ ์˜ˆ์‹œ ์—์„œ๋Š” ์ปฌ๋ ‰์…˜ ์— ํ…Œ์ผ ์ปค์„œ( tailable cursor )๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

FindPublisher<Document> findPublisher = collection.find().cursorType(CursorType.TailableAwait);
Flux.from(findPublisher)
.doOnNext(System.out::println)
.blockLast();

ํ…Œ์ผ ์ปค์„œ(tailable cursor) ๋ฐ ์‚ฌ์šฉ๋ฒ•์— ํ•™์Šต ๋ณด๋ ค๋ฉด MongoDB Server ๋งค๋‰ด์–ผ์˜ ํ…Œ์ผ ์ปค์„œ(tailable cursor) ๊ฐ€์ด๋“œ ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

์ด ๊ฐ€์ด๋“œ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ ๋˜๋Š” ์œ ํ˜•์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ๋‹ค์Œ API ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.