|
| 1 | +package com.example.query; |
| 2 | + |
| 3 | +import io.questdb.client.QueryException; |
| 4 | +import io.questdb.client.QuestDB; |
| 5 | +import io.questdb.client.cutlass.qwp.client.QwpColumnBatch; |
| 6 | +import io.questdb.client.cutlass.qwp.client.QwpColumnBatchHandler; |
| 7 | + |
| 8 | +/** |
| 9 | + * Sharing one {@link QuestDB} handle across many query threads. |
| 10 | + * <p> |
| 11 | + * The query side of the pool is thread-safe the same way ingest is (see |
| 12 | + * {@link com.example.sender.WsConcurrentIngestExample}): create one |
| 13 | + * {@code QuestDB}, hand the same instance to every thread, and let each call |
| 14 | + * {@link QuestDB#query()}. Every call returns that thread's own cached |
| 15 | + * {@link io.questdb.client.Query} instance -- no external synchronization, no |
| 16 | + * per-query allocation, one in-flight query per thread. Each {@code submit()} |
| 17 | + * acquires a worker from the query pool, so {@code queryPoolSize} caps how |
| 18 | + * many queries run in parallel; extra submits block on the acquire timeout |
| 19 | + * until a worker frees up. |
| 20 | + * <p> |
| 21 | + * For several in-flight queries from a <em>single</em> thread, see |
| 22 | + * {@link MultiInFlightQueryExample} ({@link QuestDB#newQuery()}). |
| 23 | + */ |
| 24 | +public class ConcurrentQueryExample { |
| 25 | + |
| 26 | + private static final String[] SYMBOLS = {"ETH-USD", "BTC-USD", "SOL-USD", "ADA-USD"}; |
| 27 | + |
| 28 | + public static void main(String[] args) throws InterruptedException { |
| 29 | + try (QuestDB db = QuestDB.builder() |
| 30 | + .fromConfig("ws::addr=localhost:9000;") |
| 31 | + .queryPoolSize(SYMBOLS.length) |
| 32 | + .build()) { |
| 33 | + |
| 34 | + Thread[] readers = new Thread[SYMBOLS.length]; |
| 35 | + for (int i = 0; i < SYMBOLS.length; i++) { |
| 36 | + final String symbol = SYMBOLS[i]; |
| 37 | + readers[i] = new Thread(() -> countTrades(db, symbol), "reader-" + symbol); |
| 38 | + readers[i].start(); |
| 39 | + } |
| 40 | + for (Thread reader : readers) { |
| 41 | + reader.join(); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void countTrades(QuestDB db, final String symbol) { |
| 47 | + try { |
| 48 | + db.query() |
| 49 | + .sql("SELECT count() FROM trades WHERE symbol = $1") |
| 50 | + .binds(binds -> binds.setVarchar(0, symbol)) |
| 51 | + .handler(new QwpColumnBatchHandler() { |
| 52 | + @Override |
| 53 | + public void onBatch(QwpColumnBatch batch) { |
| 54 | + batch.forEachRow(row -> System.out.println(symbol + " count = " + row.getLongValue(0))); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onEnd(long totalRows) { |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void onError(byte status, String message) { |
| 63 | + System.err.printf("%s error: 0x%02X %s%n", symbol, status & 0xFF, message); |
| 64 | + } |
| 65 | + }) |
| 66 | + .submit() |
| 67 | + .await(); |
| 68 | + } catch (QueryException e) { |
| 69 | + System.err.printf("query failed: status=0x%02X %s%n", e.getStatus() & 0xFF, e.getMessage()); |
| 70 | + } catch (InterruptedException e) { |
| 71 | + Thread.currentThread().interrupt(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments