/// <summary> /// Drops an existing table. /// DROP TABLE [IF EXISTS] table_name [DELETE TOPIC]; /// </summary> /// <param name="tableName">Name of the table to delete.</param> /// <param name="useIfExistsClause">If the IF EXISTS clause is present, the statement doesn't fail if the table doesn't exist.</param> /// <param name="deleteTopic">If the DELETE TOPIC clause is present, the table's source topic is marked for deletion.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns></returns> public Task <HttpResponseMessage> DropTableAsync(string tableName, bool useIfExistsClause, bool deleteTopic, CancellationToken cancellationToken = default) { string dropStatement = StatementTemplates.DropTable(tableName, useIfExistsClause, deleteTopic); KSqlDbStatement ksqlDbStatement = new(dropStatement); return(ExecuteStatementAsync(ksqlDbStatement, cancellationToken)); }
internal static string CreateExplainStatement <TSource>(KStreamSet <TSource> kStreamSet) { var ksqlQuery = kStreamSet?.BuildKsql(); string explainStatement = StatementTemplates.Explain($"{ksqlQuery}"); return(explainStatement); }
/// <summary> /// Drops an existing table. /// DROP TABLE table_name; /// </summary> /// <param name="tableName">Name of the table to delete.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns></returns> public Task <HttpResponseMessage> DropTableAsync(string tableName, CancellationToken cancellationToken = default) { string dropStatement = StatementTemplates.DropTable(tableName); KSqlDbStatement ksqlDbStatement = new(dropStatement); return(ExecuteStatementAsync(ksqlDbStatement, cancellationToken)); }
/// <summary> /// Drops an existing stream. /// DROP STREAM stream_name; /// </summary> /// <param name="streamName">Name of the stream to delete.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns></returns> public Task <HttpResponseMessage> DropStreamAsync(string streamName, CancellationToken cancellationToken = default) { string dropStatement = StatementTemplates.DropStream(streamName, useIfExists: false, deleteTopic: false); KSqlDbStatement ksqlDbStatement = new(dropStatement); return(ExecuteStatementAsync(ksqlDbStatement, cancellationToken)); }
public void DropTable() { //Arrange string tableName = "TABLE_NAME"; //Act var statement = StatementTemplates.DropTable(tableName); //Assert statement.Should().Be($"DROP TABLE {tableName};"); }
public void DropStream_IfExistsAndDeleteTopic() { //Arrange string streamName = "STREAM_NAME"; //Act var statement = StatementTemplates.DropStream(streamName, useIfExists: true, deleteTopic: true); //Assert statement.Should().Be($"DROP STREAM IF EXISTS {streamName} DELETE TOPIC;"); }
public void DropStream() { //Arrange string streamName = "STREAM_NAME"; //Act var statement = StatementTemplates.DropStream(streamName); //Assert statement.Should().Be($"DROP STREAM {streamName};"); }
public void DropConnector() { //Arrange string connectorName = "CONNECTOR_NAME"; //Act var statement = StatementTemplates.DropConnector(connectorName); //Assert statement.Should().Be($"DROP CONNECTOR {connectorName};"); }
public void DropTable_IfExistsAndDeleteTopic() { //Arrange string tableName = "TABLE_NAME"; //Act var statement = StatementTemplates.DropTable(tableName, useIfExists: true, deleteTopic: true); //Assert statement.Should().Be($"DROP TABLE IF EXISTS {tableName} DELETE TOPIC;"); }
public void TerminatePushQuery() { //Arrange string queryId = "QUERY_ID"; //Act var statement = StatementTemplates.TerminatePersistentQuery(queryId); //Assert statement.Should().Be($"TERMINATE {queryId};"); }
public void DropTypeIfExists() { //Arrange string typeName = "MY_TYPE"; //Act var statement = StatementTemplates.DropTypeIfExists(typeName); //Assert statement.Should().Be($"DROP TYPE IF EXISTS {typeName};"); }
public void ExplainBy() { //Arrange string queryId = "QUERY_ID"; //Act var statement = StatementTemplates.ExplainBy(queryId); //Assert statement.Should().Be($"EXPLAIN {queryId};"); }
public void Explain() { //Arrange string sqlExpression = "SELECT * FROM My EMIT CHANGES;"; //Act var statement = StatementTemplates.Explain(sqlExpression); //Assert statement.Should().Be($"EXPLAIN {sqlExpression}"); }
/// <summary> /// Terminate a persistent query. Persistent queries run continuously until they are explicitly terminated. /// </summary> /// <param name="queryId">Id of the query to terminate.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns></returns> public async Task <StatementResponse[]> TerminatePersistentQueryAsync(string queryId, CancellationToken cancellationToken = default) { string terminateStatement = StatementTemplates.TerminatePersistentQuery(queryId); KSqlDbStatement ksqlDbStatement = new(terminateStatement); var httpResponseMessage = await ExecuteStatementAsync(ksqlDbStatement, cancellationToken).ConfigureAwait(false); var statementResponse = await httpResponseMessage.ToStatementResponsesAsync().ConfigureAwait(false); return(statementResponse); }
public async Task CreateOrReplaceStreamStatement_ToStatementString_ComplexQueryWasGenerated() { //Arrange var restApiClient = KSqlDbRestApiProvider.Create(); var statement = new KSqlDbStatement(StatementTemplates.DropStream(StreamName)); var response = await restApiClient.ExecuteStatementAsync(statement); EntityCreationMetadata metadata = new() { EntityName = StreamEntityName, KafkaTopic = nameof(Movie) + "Test2", Partitions = 1, Replicas = 1 }; var httpResponseMessage = await restApiClient.CreateStreamAsync <Movie>(metadata, ifNotExists : false); var creationMetadata = new CreationMetadata { KafkaTopic = "moviesByTitle", KeyFormat = SerializationFormats.Json, ValueFormat = SerializationFormats.Json, Replicas = 1, Partitions = 1 }; var createStatement = Context.CreateOrReplaceStreamStatement(StreamName) .With(creationMetadata) .As <Movie>(StreamEntityName) .Where(c => c.Id < 3) .Select(c => new { c.Id, c.Title, ReleaseYear = c.Release_Year }) .PartitionBy(c => c.Id); //Act var ksql = createStatement.ToStatementString(); httpResponseMessage = await createStatement.ExecuteStatementAsync(); //Assert ksql.Should().BeEquivalentTo(@$ "CREATE OR REPLACE STREAM {StreamName} WITH ( KAFKA_TOPIC='moviesByTitle', KEY_FORMAT='Json', VALUE_FORMAT='Json', PARTITIONS='1', REPLICAS='1' ) AS SELECT Id, Title, Release_Year AS ReleaseYear FROM {StreamEntityName} WHERE Id < 3 PARTITION BY Id EMIT CHANGES;"); var responses = await httpResponseMessage.ToStatementResponsesAsync(); responses[0].CommandStatus.Status.Should().BeOneOf("SUCCESS", "EXECUTING"); }
public async Task DropTableAsync() { //Arrange CreateHttpMocks(@"[]"); string tableName = "TEST_TABLE"; //Act var response = await ClassUnderTest.DropTableAsync(tableName); //Assert var expectedContent = GetExpectedContent(StatementTemplates.DropTable(tableName)); VerifySendAsync(expectedContent); }
public async Task DropStreamAsync() { //Arrange CreateHttpMocks(@"[]"); string streamName = "TEST_STREAM"; //Act var response = await ClassUnderTest.DropStreamAsync(streamName); //Assert var expectedContent = GetExpectedContent(StatementTemplates.DropStream(streamName)); VerifySendAsync(expectedContent); }
public async Task DropTableAsync_IfExistsAndDeleteTopic() { //Arrange CreateHttpMocks(@"[]"); string tableName = "TEST_TABLE"; bool useIfExistsClause = true; bool deleteTopic = true; //Act var response = await ClassUnderTest.DropTableAsync(tableName, useIfExistsClause, deleteTopic); //Assert var expectedContent = GetExpectedContent(StatementTemplates.DropTable(tableName, useIfExistsClause, deleteTopic)); VerifySendAsync(expectedContent); }
public async Task TerminatePersistentQueryAsync() { //Arrange CreateHttpMocks(TerminatePersistentQueryResponse); //Act var responses = await ClassUnderTest.TerminatePersistentQueryAsync(queryId); //Assert string terminateStatement = StatementTemplates.TerminatePersistentQuery(queryId); var expectedContent = GetExpectedContent(terminateStatement); VerifySendAsync(expectedContent); string expectedStatement = StatementTemplates.TerminatePersistentQuery(queryId); responses[0].StatementText.Should().Be(expectedStatement); }
public async Task CreateOrReplaceTableStatement_ExecuteStatementAsync_ResponseWasReceived() { //Arrange var restApiClient = KSqlDbRestApiProvider.Create(); await restApiClient.CreateStreamAsync <Movie>(new EntityCreationMetadata(EntityName, 1) { EntityName = EntityName, ShouldPluralizeEntityName = false }); var statement = new KSqlDbStatement(StatementTemplates.DropTable(TableName)); var response = await restApiClient.ExecuteStatementAsync(statement); int retryCount = 0; while ((await KSqlDbRestApiProvider.Create().GetTablesAsync()).SelectMany(c => c.Tables).Any(c => c.Name == TableName.ToUpper())) { if (retryCount++ > 5) { return; } await Task.Delay(TimeSpan.FromSeconds(1)); } var createStatement = Context.CreateTableStatement(TableName) .As <Movie>(EntityName) .GroupBy(c => c.Title) .Select(c => new { Title = c.Key, Count = c.Count() }); //Act var httpResponseMessage = await createStatement.ExecuteStatementAsync(); //Assert string responseContent = await httpResponseMessage.Content.ReadAsStringAsync(); responseContent.Should().NotBeNull(); var responses = await httpResponseMessage.ToStatementResponsesAsync(); responses[0].CommandStatus.Status.Should().Be("SUCCESS"); }
/// <summary> /// Show the execution plan of a running query, show the execution plan plus additional runtime information and metrics. /// </summary> /// <param name="queryId">Id of the query to explain.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation.</param> /// <returns>ExplainResponse with execution plan plus additional runtime information and metrics.</returns> private Task <ExplainResponse[]> ExplainAsync(string queryId, CancellationToken cancellationToken = default) { string explainStatement = StatementTemplates.ExplainBy(queryId); return(ExecuteStatementAsync <ExplainResponse>(explainStatement, cancellationToken)); }