Search in sources :

Example 1 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientImpl method streamInserts.

@Override
public CompletableFuture<AcksPublisher> streamInserts(final String streamName, final Publisher<KsqlObject> insertsPublisher) {
    final CompletableFuture<AcksPublisher> cf = new CompletableFuture<>();
    final Buffer requestBody = Buffer.buffer();
    final JsonObject params = new JsonObject().put("target", streamName);
    requestBody.appendBuffer(params.toBuffer()).appendString("\n");
    makePostRequest("/inserts-stream", requestBody, cf, response -> handleStreamedResponse(response, cf, (ctx, rp, fut, req) -> new StreamInsertsResponseHandler(ctx, rp, fut, req, insertsPublisher)), false);
    return cf;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ConnectorInfo(io.confluent.ksql.api.client.ConnectorInfo) JksOptions(io.vertx.core.net.JksOptions) DdlDmlRequestValidators.validateExecuteStatementRequest(io.confluent.ksql.api.client.impl.DdlDmlRequestValidators.validateExecuteStatementRequest) Context(io.vertx.core.Context) StreamInfo(io.confluent.ksql.api.client.StreamInfo) HttpVersion(io.vertx.core.http.HttpVersion) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ConsistencyLevel(io.confluent.ksql.util.ClientConfig.ConsistencyLevel) Base64(java.util.Base64) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Entry(java.util.Map.Entry) Optional(java.util.Optional) ClientOptions(io.confluent.ksql.api.client.ClientOptions) Client(io.confluent.ksql.api.client.Client) AUTHORIZATION(io.netty.handler.codec.http.HttpHeaderNames.AUTHORIZATION) TableInfo(io.confluent.ksql.api.client.TableInfo) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) HttpClient(io.vertx.core.http.HttpClient) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecuteStatementResult(io.confluent.ksql.api.client.ExecuteStatementResult) TopicInfo(io.confluent.ksql.api.client.TopicInfo) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ServerInfo(io.confluent.ksql.api.client.ServerInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult) Charset(java.nio.charset.Charset) RecordParser(io.vertx.core.parsetools.RecordParser) HttpClientOptions(io.vertx.core.http.HttpClientOptions) SocketAddress(io.vertx.core.net.SocketAddress) Publisher(org.reactivestreams.Publisher) AcksPublisher(io.confluent.ksql.api.client.AcksPublisher) KsqlObject(io.confluent.ksql.api.client.KsqlObject) QueryInfo(io.confluent.ksql.api.client.QueryInfo) Vertx(io.vertx.core.Vertx) VertxSslOptionsFactory(io.confluent.ksql.util.VertxSslOptionsFactory) SourceDescription(io.confluent.ksql.api.client.SourceDescription) JsonArray(io.vertx.core.json.JsonArray) HttpMethod(io.vertx.core.http.HttpMethod) ConnectorDescription(io.confluent.ksql.api.client.ConnectorDescription) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AcksPublisher(io.confluent.ksql.api.client.AcksPublisher) CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject)

Example 2 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientImpl method insertInto.

@Override
public CompletableFuture<Void> insertInto(final String streamName, final KsqlObject row) {
    final CompletableFuture<Void> cf = new CompletableFuture<>();
    final Buffer requestBody = Buffer.buffer();
    final JsonObject params = new JsonObject().put("target", streamName);
    requestBody.appendBuffer(params.toBuffer()).appendString("\n");
    requestBody.appendString(row.toJsonString()).appendString("\n");
    makePostRequest(INSERTS_ENDPOINT, requestBody, cf, response -> handleStreamedResponse(response, cf, (ctx, rp, fut, req) -> new InsertIntoResponseHandler(ctx, rp, fut)));
    return cf;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ConnectorInfo(io.confluent.ksql.api.client.ConnectorInfo) JksOptions(io.vertx.core.net.JksOptions) DdlDmlRequestValidators.validateExecuteStatementRequest(io.confluent.ksql.api.client.impl.DdlDmlRequestValidators.validateExecuteStatementRequest) Context(io.vertx.core.Context) StreamInfo(io.confluent.ksql.api.client.StreamInfo) HttpVersion(io.vertx.core.http.HttpVersion) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ConsistencyLevel(io.confluent.ksql.util.ClientConfig.ConsistencyLevel) Base64(java.util.Base64) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Entry(java.util.Map.Entry) Optional(java.util.Optional) ClientOptions(io.confluent.ksql.api.client.ClientOptions) Client(io.confluent.ksql.api.client.Client) AUTHORIZATION(io.netty.handler.codec.http.HttpHeaderNames.AUTHORIZATION) TableInfo(io.confluent.ksql.api.client.TableInfo) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) HttpClient(io.vertx.core.http.HttpClient) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecuteStatementResult(io.confluent.ksql.api.client.ExecuteStatementResult) TopicInfo(io.confluent.ksql.api.client.TopicInfo) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ServerInfo(io.confluent.ksql.api.client.ServerInfo) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientResponse(io.vertx.core.http.HttpClientResponse) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult) Charset(java.nio.charset.Charset) RecordParser(io.vertx.core.parsetools.RecordParser) HttpClientOptions(io.vertx.core.http.HttpClientOptions) SocketAddress(io.vertx.core.net.SocketAddress) Publisher(org.reactivestreams.Publisher) AcksPublisher(io.confluent.ksql.api.client.AcksPublisher) KsqlObject(io.confluent.ksql.api.client.KsqlObject) QueryInfo(io.confluent.ksql.api.client.QueryInfo) Vertx(io.vertx.core.Vertx) VertxSslOptionsFactory(io.confluent.ksql.util.VertxSslOptionsFactory) SourceDescription(io.confluent.ksql.api.client.SourceDescription) JsonArray(io.vertx.core.json.JsonArray) HttpMethod(io.vertx.core.http.HttpMethod) ConnectorDescription(io.confluent.ksql.api.client.ConnectorDescription) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Handler(io.vertx.core.Handler) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientIntegrationTest method verifyPullQueryRow.

private static void verifyPullQueryRow(final Row row) {
    // verify metadata
    assertThat(row.values(), equalTo(PULL_QUERY_EXPECTED_ROW));
    assertThat(row.columnNames(), equalTo(PULL_QUERY_COLUMN_NAMES));
    assertThat(row.columnTypes(), equalTo(PULL_QUERY_COLUMN_TYPES));
    // verify type-based getters
    assertThat(row.getKsqlObject("K"), is(PULL_QUERY_EXPECTED_ROW.getKsqlObject(0)));
    assertThat(row.getLong("LONG"), is(PULL_QUERY_EXPECTED_ROW.getLong(1)));
    // verify index-based getters are 1-indexed
    assertThat(row.getKsqlObject(1), is(row.getKsqlObject("K")));
    assertThat(row.getLong(2), is(row.getLong("LONG")));
    // verify isNull() evaluation
    assertThat(row.isNull("K"), is(false));
    assertThat(row.isNull("LONG"), is(false));
    // verify exception on invalid cast
    assertThrows(ClassCastException.class, () -> row.getInteger("K"));
    // verify KsqlArray methods
    final KsqlArray values = row.values();
    assertThat(values.size(), is(PULL_QUERY_COLUMN_NAMES.size()));
    assertThat(values.isEmpty(), is(false));
    assertThat(values.getKsqlObject(0), is(row.getKsqlObject("K")));
    assertThat(values.getLong(1), is(row.getLong("LONG")));
    assertThat(values.toJsonString(), is((new JsonArray(values.getList())).toString()));
    assertThat(values.toString(), is(values.toJsonString()));
    // verify KsqlObject methods
    final KsqlObject obj = row.asObject();
    assertThat(obj.size(), is(PULL_QUERY_COLUMN_NAMES.size()));
    assertThat(obj.isEmpty(), is(false));
    assertThat(obj.fieldNames(), contains(PULL_QUERY_COLUMN_NAMES.toArray()));
    assertThat(obj.getKsqlObject("K"), is(row.getKsqlObject("K")));
    assertThat(obj.getLong("LONG"), is(row.getLong("LONG")));
    assertThat(obj.containsKey("LONG"), is(true));
    assertThat(obj.containsKey("notafield"), is(false));
    assertThat(obj.toJsonString(), is((new JsonObject(obj.getMap())).toString()));
    assertThat(obj.toString(), is(obj.toJsonString()));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) KsqlArray(io.confluent.ksql.api.client.KsqlArray) KsqlObject(io.confluent.ksql.api.client.KsqlObject)

Example 4 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldStreamQueryWithProperties.

@Test
public void shouldStreamQueryWithProperties() throws Exception {
    // Given
    final Map<String, Object> properties = new HashMap<>();
    properties.put("auto.offset.reset", "latest");
    final String sql = "SELECT * FROM " + TEST_STREAM + " EMIT CHANGES LIMIT 1;";
    final KsqlObject insertRow = new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldStreamQueryWithProperties"))).put("STR", "Value_shouldStreamQueryWithProperties").put("LONG", 2000L).put("DEC", new BigDecimal("12.34")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v1_shouldStreamQueryWithProperties").add("v2_shouldStreamQueryWithProperties")).put("MAP", new KsqlObject().put("test_name", "shouldStreamQueryWithProperties")).put("STRUCT", new KsqlObject().put("F1", 4)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00:00");
    // When
    final StreamedQueryResult queryResult = client.streamQuery(sql, properties).get();
    // Then: a newly inserted row arrives
    final Row row = assertThatEventually(() -> {
        // Potentially try inserting multiple times, in case the query wasn't started by the first time
        try {
            client.insertInto(TEST_STREAM, insertRow).get();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
        return queryResult.poll(Duration.ofMillis(10));
    }, is(notNullValue()));
    assertThat(row.getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldStreamQueryWithProperties"))));
    assertThat(row.getString("STR"), is("Value_shouldStreamQueryWithProperties"));
    assertThat(row.getLong("LONG"), is(2000L));
    assertThat(row.getDecimal("DEC"), is(new BigDecimal("12.34")));
    assertThat(row.getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
    assertThat(row.getKsqlArray("ARRAY"), is(new KsqlArray().add("v1_shouldStreamQueryWithProperties").add("v2_shouldStreamQueryWithProperties")));
    assertThat(row.getKsqlObject("MAP"), is(new KsqlObject().put("test_name", "shouldStreamQueryWithProperties")));
    assertThat(row.getKsqlObject("STRUCT"), is(new KsqlObject().put("F1", 4)));
    assertThat(row.getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
    assertThat(row.getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
    assertThat(row.getString("DATE"), is("1970-01-01"));
    assertThat(row.getString("TIME"), is("00:00"));
}
Also used : HashMap(java.util.HashMap) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(io.confluent.ksql.api.client.Row) KsqlArray(io.confluent.ksql.api.client.KsqlArray) BigDecimal(java.math.BigDecimal) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecutionException(java.util.concurrent.ExecutionException) KsqlObject(io.confluent.ksql.api.client.KsqlObject) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 5 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldStreamInserts.

@Test
public void shouldStreamInserts() throws Exception {
    // Given
    final InsertsPublisher insertsPublisher = new InsertsPublisher();
    final int numRows = 5;
    // When
    final AcksPublisher acksPublisher = client.streamInserts(EMPTY_TEST_STREAM_2, insertsPublisher).get();
    final TestSubscriber<InsertAck> acksSubscriber = subscribeAndWait(acksPublisher);
    assertThat(acksSubscriber.getValues(), hasSize(0));
    acksSubscriber.getSub().request(numRows);
    for (int i = 0; i < numRows; i++) {
        insertsPublisher.accept(new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key_" + i))).put("STR", "TEST_" + i).put("LONG", i).put("DEC", new BigDecimal("13.31")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v_" + i)).put("MAP", new KsqlObject().put("k_" + i, "v_" + i)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00"));
    }
    // Then
    assertThatEventually(acksSubscriber::getValues, hasSize(numRows));
    for (int i = 0; i < numRows; i++) {
        assertThat(acksSubscriber.getValues().get(i).seqNum(), is(Long.valueOf(i)));
    }
    assertThat(acksSubscriber.getError(), is(nullValue()));
    assertThat(acksSubscriber.isCompleted(), is(false));
    assertThat(acksPublisher.isComplete(), is(false));
    assertThat(acksPublisher.isFailed(), is(false));
    // Then: should receive new rows
    final String query = "SELECT * FROM " + EMPTY_TEST_STREAM_2 + " EMIT CHANGES LIMIT " + numRows + ";";
    final List<Row> rows = client.executeQuery(query).get();
    // Verify inserted rows are as expected
    assertThat(rows, hasSize(numRows));
    for (int i = 0; i < numRows; i++) {
        assertThat(rows.get(i).getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_" + i))));
        assertThat(rows.get(i).getString("STR"), is("TEST_" + i));
        assertThat(rows.get(i).getLong("LONG"), is(Long.valueOf(i)));
        assertThat(rows.get(i).getDecimal("DEC"), is(new BigDecimal("13.31")));
        assertThat(rows.get(i).getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
        assertThat(rows.get(i).getKsqlArray("ARRAY"), is(new KsqlArray().add("v_" + i)));
        assertThat(rows.get(i).getKsqlObject("MAP"), is(new KsqlObject().put("k_" + i, "v_" + i)));
        assertThat(rows.get(i).getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
        assertThat(rows.get(i).getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
        assertThat(rows.get(i).getString("DATE"), is("1970-01-01"));
        assertThat(rows.get(i).getString("TIME"), is("00:00"));
    }
    // When: end connection
    insertsPublisher.complete();
    // Then
    assertThatEventually(acksSubscriber::isCompleted, is(true));
    assertThat(acksSubscriber.getError(), is(nullValue()));
    assertThat(acksPublisher.isComplete(), is(true));
    assertThat(acksPublisher.isFailed(), is(false));
}
Also used : AcksPublisher(io.confluent.ksql.api.client.AcksPublisher) InsertsPublisher(io.confluent.ksql.api.client.InsertsPublisher) InsertAck(io.confluent.ksql.api.client.InsertAck) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(io.confluent.ksql.api.client.Row) KsqlArray(io.confluent.ksql.api.client.KsqlArray) BigDecimal(java.math.BigDecimal) KsqlObject(io.confluent.ksql.api.client.KsqlObject) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Aggregations

KsqlObject (io.confluent.ksql.api.client.KsqlObject)16 Test (org.junit.Test)12 KsqlArray (io.confluent.ksql.api.client.KsqlArray)8 IntegrationTest (io.confluent.common.utils.IntegrationTest)6 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)6 Row (io.confluent.ksql.api.client.Row)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InOrder (org.mockito.InOrder)5 StreamedQueryResult (io.confluent.ksql.api.client.StreamedQueryResult)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 AcksPublisher (io.confluent.ksql.api.client.AcksPublisher)3 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Client (io.confluent.ksql.api.client.Client)2 ClientOptions (io.confluent.ksql.api.client.ClientOptions)2 ConnectorDescription (io.confluent.ksql.api.client.ConnectorDescription)2 ConnectorInfo (io.confluent.ksql.api.client.ConnectorInfo)2