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(); }
private void outputException(final OutputStream out, final Throwable exception) { try { out.write("\n".getBytes(StandardCharsets.UTF_8)); if (exception.getCause() instanceof KsqlException) { objectMapper.writeValue(out, StreamedRow.error(exception.getCause())); } else { objectMapper.writeValue(out, StreamedRow.error(exception)); } out.write("\n".getBytes(StandardCharsets.UTF_8)); out.flush(); } catch (final IOException e) { log.debug("Client disconnected while attempting to write an error message"); } }
public void printStreamedRow(final StreamedRow row) throws IOException { if (row.getErrorMessage() != null) { printErrorMessage(row.getErrorMessage()); return; } if (row.getFinalMessage() != null) { writer().println(row.getFinalMessage()); return; } switch (outputFormat) { case JSON: printAsJson(row.getRow().getColumns()); break; case TABULAR: printAsTable(row.getRow()); break; default: throw new RuntimeException(String.format( "Unexpected output format: '%s'", outputFormat.name() )); } }
@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"))); }
objectMapper.writeValue(out, StreamedRow.finalMessage("Limit Reached")); out.write("\n".getBytes(StandardCharsets.UTF_8)); out.flush();
expectedRow = writtenRows.poll(); final GenericRow testRow = objectMapper.readValue(responseLine, StreamedRow.class).getRow(); assertEquals(expectedRow, testRow);
public static StreamedRow row(final GenericRow row) { return new StreamedRow(row, null, null); }
@JsonCreator public StreamedRow( @JsonProperty("row") final GenericRow row, @JsonProperty("errorMessage") final KsqlErrorMessage errorMessage, @JsonProperty("finalMessage") final String finalMessage ) { checkUnion(row, errorMessage, finalMessage); this.row = row; this.errorMessage = errorMessage; this.finalMessage = finalMessage; }
final StreamedRow row = queryStream.next(); terminal.printStreamedRow(row); if (row.isTerminal()) { break;
@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"))); }
@Test public void testPrintFinalMessageStreamedRow() throws IOException { console.printStreamedRow(StreamedRow.finalMessage("Some message")); assertThat(terminal.getOutputString(), is("Some message\n")); }
public static StreamedRow finalMessage(final String finalMessage) { return new StreamedRow(null, null, finalMessage); }
@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)); } }
@Test public void testPrintErrorStreamedRow() throws IOException { final FakeException exception = new FakeException(); console.printStreamedRow(StreamedRow.error(exception)); assertThat(terminal.getOutputString(), is(exception.getMessage() + "\n")); }
public static StreamedRow error(final Throwable exception) { return new StreamedRow( null, new KsqlErrorMessage(Errors.ERROR_CODE_SERVER_ERROR, exception), null); }
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); }