private void write(final OutputStream output, final GenericRow row) throws IOException { objectMapper.writeValue(output, StreamedRow.row(row)); output.write("\n".getBytes(StandardCharsets.UTF_8)); output.flush(); }
@Override public Collection<StreamedRow> poll() { final List<KeyValue<String, GenericRow>> rows = Lists.newLinkedList(); queryMetadata.getRowQueue().drainTo(rows); if (rows.isEmpty()) { return null; } else { return rows.stream().map(row -> StreamedRow.row(row.value)) .collect(Collectors.toCollection(Lists::newLinkedList)); } }
private String formatData(final String data) throws JsonProcessingException { final List<Object> rowColumns = new ArrayList<>(); rowColumns.add(data); final GenericRow row = new GenericRow(rowColumns); return objectMapper.writeValueAsString(StreamedRow.row(row)); }
@Test public void testPrintGenericStreamedRow() throws IOException { final StreamedRow row = StreamedRow.row(new GenericRow(ImmutableList.of("col_1", "col_2"))); console.printStreamedRow(row); }
@Test public void shouldHandleSlowResponsesFromServer() throws InterruptedException { // Given: givenResponsesDelayedBy(Duration.ofSeconds(3)); final RestResponse<KsqlRestClient.QueryStream> queryResponse = ksqlRestClient.makeQueryRequest("Select *"); final ReceiverThread receiver = new ReceiverThread(queryResponse); final MockStreamedQueryResource.TestStreamWriter writer = getResponseWriter(); // When: writer.enq("hello"); writer.enq("world"); writer.enq("{\"row\":null,\"errorMessage\":null,\"finalMessage\":\"Limit Reached\"}"); writer.finished(); // Then: assertThat(receiver.getRows(), contains( StreamedRow.row(new GenericRow(ImmutableList.of("hello"))), StreamedRow.row(new GenericRow(ImmutableList.of("world"))), StreamedRow.finalMessage("Limit Reached"))); }
@Test public void testStreamRowFromServer() throws InterruptedException { // Given: final RestResponse<KsqlRestClient.QueryStream> queryResponse = ksqlRestClient.makeQueryRequest("Select *"); final ReceiverThread receiver = new ReceiverThread(queryResponse); final MockStreamedQueryResource.TestStreamWriter writer = getResponseWriter(); // When: writer.enq("hello"); writer.enq("world"); writer.enq("{\"row\":null,\"errorMessage\":null,\"finalMessage\":\"Limit Reached\"}"); writer.finished(); // Then: assertThat(receiver.getRows(), contains( StreamedRow.row(new GenericRow(ImmutableList.of("hello"))), StreamedRow.row(new GenericRow(ImmutableList.of("world"))), StreamedRow.finalMessage("Limit Reached"))); }