-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle 'RUN SCRIPT' directly in CLI, rather than post it to KSQL. #2331
Merged
big-andy-coates
merged 20 commits into
confluentinc:master
from
big-andy-coates:cli_run_script
Jan 21, 2019
Merged
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
37606f3
Initial new `run` command
big-andy-coates cdd315d
Merge branch 'master' into cli_run_script
big-andy-coates 9878b3c
- support for CLI commands with spaces in the name.
big-andy-coates 1d87461
remove old runscript functionality
big-andy-coates 64c08ca
fix test
big-andy-coates 196a525
fix test
big-andy-coates e64be32
Hojjat's requested changes.
big-andy-coates 07d6db3
Merge branch 'master' into cli_run_script
big-andy-coates 1dccb8e
Almog's requested changes.
big-andy-coates 3e618ce
Merge branch 'cli_run_script' of github.com:big-andy-coates/ksql into…
big-andy-coates 0604482
Almog's requested changes.
big-andy-coates 6676651
Merge branch 'master' into cli_run_script
big-andy-coates c2653e7
Merge branch 'master' into cli_run_script
big-andy-coates a7d7e2c
Rohan's requested changes (ish)
big-andy-coates 8cebfc8
Merge branch 'cli_run_script' of github.com:big-andy-coates/ksql into…
big-andy-coates 1d3747f
Merge branch 'master' into cli_run_script
big-andy-coates 6f91b84
Fix tests.
big-andy-coates 4ea01cd
Merge branch 'master' into cli_run_script
big-andy-coates 1de4a61
Merge branch 'cli_run_script' of github.com:big-andy-coates/ksql into…
big-andy-coates f57a8ab
Add tests to cover javadoc comments.
big-andy-coates File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import io.confluent.ksql.cli.console.Console; | ||
import io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable; | ||
import io.confluent.ksql.cli.console.OutputFormat; | ||
import io.confluent.ksql.cli.console.cmd.CliCommandRegisterUtil; | ||
import io.confluent.ksql.cli.console.cmd.RemoteServerSpecificCommand; | ||
import io.confluent.ksql.ddl.DdlConfig; | ||
import io.confluent.ksql.parser.AstBuilder; | ||
|
@@ -44,8 +45,6 @@ | |
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.PrintWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
@@ -57,13 +56,14 @@ | |
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Supplier; | ||
import org.jline.reader.EndOfFileException; | ||
import org.jline.reader.UserInterruptException; | ||
import org.jline.terminal.Terminal; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Cli implements Closeable { | ||
public class Cli implements KsqlRequestExecutor, Closeable { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(Cli.class); | ||
|
||
|
@@ -81,7 +81,7 @@ public static Cli build( | |
final OutputFormat outputFormat, | ||
final KsqlRestClient restClient | ||
) { | ||
final Console console = Console.build(outputFormat, restClient); | ||
final Console console = Console.build(outputFormat); | ||
return new Cli(streamedQueryRowLimit, streamedQueryTimeoutMs, restClient, console); | ||
} | ||
|
||
|
@@ -100,8 +100,22 @@ public static Cli build( | |
this.terminal = terminal; | ||
this.queryStreamExecutorService = Executors.newSingleThreadExecutor(); | ||
|
||
final Supplier<String> versionSuppler = | ||
() -> restClient.getServerInfo().getResponse().getVersion(); | ||
|
||
CliCommandRegisterUtil.registerDefaultCommands(this, terminal, versionSuppler); | ||
|
||
terminal | ||
.registerCliSpecificCommand(new RemoteServerSpecificCommand(restClient, terminal.writer())); | ||
.registerCliSpecificCommand(new RemoteServerSpecificCommand(restClient)); | ||
} | ||
|
||
@Override | ||
public void makeKsqlRequest(final String requestBody) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: rename to something like statementText or statements - requestBody implies the http request body There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
try { | ||
printKsqlResponse(restClient.makeKsqlRequest(requestBody)); | ||
} catch (IOException e) { | ||
throw new KsqlException(e); | ||
} | ||
} | ||
|
||
public void runInteractively() { | ||
|
@@ -222,25 +236,21 @@ private void handleStatements(final String line) | |
); | ||
|
||
} else if (statementContext.statement() instanceof SqlBaseParser.ListPropertiesContext) { | ||
listProperties(statementText); | ||
makeKsqlRequest(statementText); | ||
|
||
} else if (statementContext.statement() instanceof SqlBaseParser.SetPropertyContext) { | ||
setProperty(statementContext); | ||
|
||
} else if (statementContext.statement() instanceof SqlBaseParser.UnsetPropertyContext) { | ||
consecutiveStatements = unsetProperty(consecutiveStatements, statementContext); | ||
} else if (statementContext.statement() instanceof SqlBaseParser.RunScriptContext) { | ||
runScript(statementContext, statementText); | ||
} else if (statementContext.statement() instanceof SqlBaseParser.RegisterTopicContext) { | ||
registerTopic(consecutiveStatements, statementContext, statementText); | ||
} else { | ||
consecutiveStatements.append(statementText); | ||
} | ||
} | ||
if (consecutiveStatements.length() != 0) { | ||
printKsqlResponse( | ||
restClient.makeKsqlRequest(consecutiveStatements.toString()) | ||
); | ||
makeKsqlRequest(consecutiveStatements.toString()); | ||
} | ||
} | ||
|
||
|
@@ -256,36 +266,13 @@ private void registerTopic( | |
consecutiveStatements.append(statementText); | ||
} | ||
|
||
private void runScript( | ||
final SqlBaseParser.SingleStatementContext statementContext, | ||
final String statementText | ||
) throws IOException { | ||
final SqlBaseParser.RunScriptContext runScriptContext = | ||
(SqlBaseParser.RunScriptContext) statementContext.statement(); | ||
final String schemaFilePath = AstBuilder.unquote(runScriptContext.STRING().getText(), "'"); | ||
final String fileContent; | ||
try { | ||
fileContent = new String(Files.readAllBytes(Paths.get(schemaFilePath)), UTF_8); | ||
} catch (final IOException e) { | ||
throw new KsqlException( | ||
" Could not read statements from the provided script file " + schemaFilePath + ": " | ||
+ e + " Make sure the file exists and can be read by KSQL CLI.", | ||
e | ||
); | ||
} | ||
setProperty(KsqlConstants.RUN_SCRIPT_STATEMENTS_CONTENT, fileContent); | ||
printKsqlResponse( | ||
restClient.makeKsqlRequest(statementText) | ||
); | ||
} | ||
|
||
private StringBuilder printOrDisplayQueryResults( | ||
final StringBuilder consecutiveStatements, | ||
final SqlBaseParser.SingleStatementContext statementContext, | ||
final String statementText | ||
) throws InterruptedException, IOException, ExecutionException { | ||
if (consecutiveStatements.length() != 0) { | ||
printKsqlResponse(restClient.makeKsqlRequest(consecutiveStatements.toString())); | ||
makeKsqlRequest(consecutiveStatements.toString()); | ||
consecutiveStatements.setLength(0); | ||
} | ||
if (statementContext.statement() instanceof SqlBaseParser.QuerystatementContext) { | ||
|
@@ -296,11 +283,6 @@ private StringBuilder printOrDisplayQueryResults( | |
return consecutiveStatements; | ||
} | ||
|
||
private void listProperties(final String statementText) throws IOException { | ||
final KsqlEntityList ksqlEntityList = restClient.makeKsqlRequest(statementText).getResponse(); | ||
terminal.printKsqlEntityList(ksqlEntityList); | ||
} | ||
|
||
private void printKsqlResponse(final RestResponse<KsqlEntityList> response) throws IOException { | ||
if (response.isSuccessful()) { | ||
final KsqlEntityList ksqlEntities = response.getResponse(); | ||
|
@@ -458,11 +440,9 @@ private void setProperty(final String property, final String value) { | |
private StringBuilder unsetProperty( | ||
final StringBuilder consecutiveStatements, | ||
final SqlBaseParser.SingleStatementContext statementContext | ||
) throws IOException { | ||
) { | ||
if (consecutiveStatements.length() != 0) { | ||
printKsqlResponse( | ||
restClient.makeKsqlRequest(consecutiveStatements.toString()) | ||
); | ||
makeKsqlRequest(consecutiveStatements.toString()); | ||
consecutiveStatements.setLength(0); | ||
} | ||
final SqlBaseParser.UnsetPropertyContext unsetPropertyContext = | ||
|
25 changes: 25 additions & 0 deletions
25
ksql-cli/src/main/java/io/confluent/ksql/cli/KsqlRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2018 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License; you may not use this file | ||
* except in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.cli; | ||
|
||
public interface KsqlRequestExecutor { | ||
|
||
/** | ||
* Execute a request on the KSQL servers and handle the response. | ||
* | ||
* @param body the request body. | ||
*/ | ||
void makeKsqlRequest(String body); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this line into registerDefaultCommands?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.