|
| 1 | +// This file is part of https://github.com/SpringQL/SpringQL-client-c which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details. |
| 2 | + |
| 3 | +// Usage: |
| 4 | +// |
| 5 | +// $ ./a.out # waiting for connection... |
| 6 | +// $ echo '{"ts": "2022-01-01 13:00:00.000000000", "symbol": "ORCL", "amount": 10}' |nc localhost 54300 |
| 7 | +// $ echo '{"ts": "2022-01-01 13:00:01.000000000", "symbol": "ORCL", "amount": 30}' |nc localhost 54300 |
| 8 | +// $ echo '{"ts": "2022-01-01 13:00:01.000000000", "symbol": "GOOGL", "amount": 50}' |nc localhost 54300 |
| 9 | +// $ echo '{"ts": "2022-01-01 13:00:02.000000000", "symbol": "ORCL", "amount": 40}' |nc localhost 54300 |
| 10 | +// $ echo '{"ts": "2022-01-01 13:00:05.000000000", "symbol": "GOOGL", "amount": 60}' |nc localhost 54300 |
| 11 | +// $ echo '{"ts": "2022-01-01 13:00:10.000000000", "symbol": "APPL", "amount": 100}' |nc localhost 54300 |
| 12 | + |
| 13 | +#include <assert.h> |
| 14 | +#include <string.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <unistd.h> |
| 17 | + |
| 18 | +#include <springql.h> |
| 19 | + |
| 20 | +void abort_with_report() |
| 21 | +{ |
| 22 | + SpringErrno errno; |
| 23 | + char errmsg[1024]; |
| 24 | + spring_last_err(&errno, errmsg, 1024); |
| 25 | + fprintf(stderr, "Error occurred (%d): %s", errno, errmsg); |
| 26 | + abort(); |
| 27 | +} |
| 28 | + |
| 29 | +void assert_ok(SpringErrno ret) |
| 30 | +{ |
| 31 | + if (ret != Ok) |
| 32 | + { |
| 33 | + abort_with_report(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void assert_not_null(void *p) |
| 38 | +{ |
| 39 | + if (p == NULL) |
| 40 | + { |
| 41 | + abort_with_report(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +int main() |
| 46 | +{ |
| 47 | + SpringErrno ret; |
| 48 | + |
| 49 | + SpringConfig *config = spring_config_default(); |
| 50 | + assert_not_null(config); |
| 51 | + |
| 52 | + SpringPipeline *pipeline = spring_open(config); |
| 53 | + assert_not_null(pipeline); |
| 54 | + |
| 55 | + ret = spring_command( |
| 56 | + pipeline, |
| 57 | + "CREATE SOURCE STREAM source_trade (" |
| 58 | + " ts TIMESTAMP NOT NULL ROWTIME," |
| 59 | + " symbol TEXT NOT NULL," |
| 60 | + " amount INTEGER NOT NULL" |
| 61 | + ");"); |
| 62 | + assert_ok(ret); |
| 63 | + |
| 64 | + ret = spring_command( |
| 65 | + pipeline, |
| 66 | + "CREATE SINK STREAM sink_avg_all (" |
| 67 | + " ts TIMESTAMP NOT NULL ROWTIME," |
| 68 | + " avg_amount FLOAT NOT NULL" |
| 69 | + ");"); |
| 70 | + assert_ok(ret); |
| 71 | + |
| 72 | + ret = spring_command( |
| 73 | + pipeline, |
| 74 | + "CREATE SINK STREAM sink_avg_by_symbol (" |
| 75 | + " ts TIMESTAMP NOT NULL ROWTIME," |
| 76 | + " symbol TEXT NOT NULL," |
| 77 | + " avg_amount FLOAT NOT NULL" |
| 78 | + ");"); |
| 79 | + assert_ok(ret); |
| 80 | + |
| 81 | + // Creates windows per 10 seconds ([:00, :10), [:10, :20), ...), |
| 82 | + // and calculate the average amount over the rows inside each window. |
| 83 | + // |
| 84 | + // Second parameter `DURATION_SECS(0)` means allowed latency for late data. You can ignore here. |
| 85 | + ret = spring_command( |
| 86 | + pipeline, |
| 87 | + "CREATE PUMP avg_all AS" |
| 88 | + " INSERT INTO sink_avg_all (ts, avg_amount)" |
| 89 | + " SELECT STREAM" |
| 90 | + " FLOOR_TIME(source_trade.ts, DURATION_SECS(10)) AS min_ts," |
| 91 | + " AVG(source_trade.amount) AS avg_amount" |
| 92 | + " FROM source_trade" |
| 93 | + " GROUP BY min_ts" |
| 94 | + " FIXED WINDOW DURATION_SECS(10), DURATION_SECS(0);"); |
| 95 | + assert_ok(ret); |
| 96 | + |
| 97 | + // Creates windows per 2 seconds ([:00, :02), [:02, :04), ...), |
| 98 | + // and then group the rows inside each window having the same symbol. |
| 99 | + // Calculate the average amount for each group. |
| 100 | + ret = spring_command( |
| 101 | + pipeline, |
| 102 | + "CREATE PUMP avg_by_symbol AS" |
| 103 | + " INSERT INTO sink_avg_by_symbol (ts, symbol, avg_amount)" |
| 104 | + " SELECT STREAM" |
| 105 | + " FLOOR_TIME(source_trade.ts, DURATION_SECS(2)) AS min_ts," |
| 106 | + " source_trade.symbol AS symbol," |
| 107 | + " AVG(source_trade.amount) AS avg_amount" |
| 108 | + " FROM source_trade" |
| 109 | + " GROUP BY min_ts, symbol" |
| 110 | + " FIXED WINDOW DURATION_SECS(2), DURATION_SECS(0);"); |
| 111 | + assert_ok(ret); |
| 112 | + |
| 113 | + ret = spring_command( |
| 114 | + pipeline, |
| 115 | + "CREATE SINK WRITER queue_avg_all FOR sink_avg_all" |
| 116 | + " TYPE IN_MEMORY_QUEUE OPTIONS (" |
| 117 | + " NAME 'q_avg_all'" |
| 118 | + " );"); |
| 119 | + assert_ok(ret); |
| 120 | + |
| 121 | + ret = spring_command( |
| 122 | + pipeline, |
| 123 | + "CREATE SINK WRITER queue_avg_by_symbol FOR sink_avg_by_symbol" |
| 124 | + " TYPE IN_MEMORY_QUEUE OPTIONS (" |
| 125 | + " NAME 'q_avg_by_symbol'" |
| 126 | + " );"); |
| 127 | + assert_ok(ret); |
| 128 | + |
| 129 | + ret = spring_command( |
| 130 | + pipeline, |
| 131 | + "CREATE SOURCE READER tcp_trade FOR source_trade" |
| 132 | + " TYPE NET_SERVER OPTIONS (" |
| 133 | + " PROTOCOL 'TCP'," |
| 134 | + " PORT '54300'" |
| 135 | + " );"); |
| 136 | + assert_ok(ret); |
| 137 | + |
| 138 | + fprintf(stderr, "waiting JSON records in tcp/54300...\n"); |
| 139 | + |
| 140 | + SpringRow *row; |
| 141 | + bool is_err = false; |
| 142 | + while (1) |
| 143 | + { |
| 144 | +#define TS_LEN 128 |
| 145 | +#define SYMBOL_LEN 6 |
| 146 | + char ts[TS_LEN]; |
| 147 | + char symbol[SYMBOL_LEN]; |
| 148 | + |
| 149 | + // Fetching rows from q_avg_all. |
| 150 | + { |
| 151 | + row = spring_pop_non_blocking(pipeline, "q_avg_all", &is_err); |
| 152 | + if (row) |
| 153 | + { |
| 154 | + int r = spring_column_text(row, 0, (char *)ts, TS_LEN); |
| 155 | + assert((size_t)r == strlen(ts)); |
| 156 | + |
| 157 | + float avg_amount; |
| 158 | + ret = spring_column_float(row, 1, &avg_amount); |
| 159 | + assert_ok(ret); |
| 160 | + |
| 161 | + fprintf(stderr, "[q_avg_all] %s\t%f\n", ts, avg_amount); |
| 162 | + spring_row_close(row); |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + assert(!is_err); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Fetching rows from q_avg_by_symbol. |
| 171 | + row = spring_pop_non_blocking(pipeline, "q_avg_by_symbol", &is_err); |
| 172 | + if (row) |
| 173 | + { |
| 174 | + int r = spring_column_text(row, 0, (char *)ts, TS_LEN); |
| 175 | + assert((size_t)r == strlen(ts)); |
| 176 | + |
| 177 | + r = spring_column_text(row, 1, (char *)symbol, SYMBOL_LEN); |
| 178 | + assert((size_t)r == strlen(symbol)); |
| 179 | + |
| 180 | + float avg_amount; |
| 181 | + ret = spring_column_float(row, 2, &avg_amount); |
| 182 | + assert_ok(ret); |
| 183 | + |
| 184 | + fprintf(stderr, "[q_avg_by_symbol] %s\t%s\t%f\n", ts, symbol, avg_amount); |
| 185 | + spring_row_close(row); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + assert(!is_err); |
| 190 | + } |
| 191 | + |
| 192 | + // Avoid busy sleep. |
| 193 | + usleep(100000); |
| 194 | + } |
| 195 | + |
| 196 | + ret = spring_close(pipeline); |
| 197 | + assert_ok(ret); |
| 198 | + |
| 199 | + ret = spring_config_close(config); |
| 200 | + assert_ok(ret); |
| 201 | + |
| 202 | + return 0; |
| 203 | +} |
0 commit comments