You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@solr.apache.org by "epugh (via GitHub)" <gi...@apache.org> on 2024/05/25 14:02:45 UTC
[PR] SOLR-14673: Add bin/solr stream CLI [solr]
epugh opened a new pull request, #2479:
URL: https://github.com/apache/solr/pull/2479
https://issues.apache.org/jira/browse/SOLR-14673
# Description
Bring in code that @joel-bernstein wrote, but using the SolrCLI infrastructure.
# Solution
Another CLI client ;-)
# Tests
Copied over the basic tests from the patch. I still need to write an integration style test and ideally one that exercies the basic auth.
# Checklist
Please review the following and check all that apply:
- [ ] I have reviewed the guidelines for [How to Contribute](https://github.com/apache/solr/blob/main/CONTRIBUTING.md) and my code conforms to the standards described there to the best of my ability.
- [ ] I have created a Jira issue and added the issue ID to my pull request title.
- [ ] I have given Solr maintainers [access](https://help.github.com/en/articles/allowing-changes-to-a-pull-request-branch-created-from-a-fork) to contribute to my PR branch. (optional but recommended)
- [ ] I have developed this patch against the `main` branch.
- [ ] I have run `./gradlew check`.
- [ ] I have added tests for my changes.
- [ ] I have added documentation for the [Reference Guide](https://github.com/apache/solr/tree/main/solr/solr-ref-guide)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r2095761990
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java:
##########
@@ -55,6 +50,8 @@ public class SolrClientCache implements Closeable {
private static final int minSocketTimeout =
Math.max(Integer.getInteger(HttpClientUtil.PROP_SO_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);
+ private String basicAuthCredentials = null; // Only support with the http2SolrClient
Review Comment:
I could get behind that. It's not always obvious which settings are done on the http client level and which are at the solr level ;-). Would you be interested in opening a ticket? Maybe a good "newdev"?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1834961429
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
Review Comment:
Apparently, this works for both cloud and user-managed, strange. But it is what it is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2246259400
Okay, I think this is ready for review! I've added some docs.. I especially liked being able to cat some local data right into a Solr collection!
```
cat example/exampledocs/books.csv | bin/solr stream -e local 'update(gettingstarted,parseCSV(stdin()))'
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840275323
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
Review Comment:
This change makes only sense if the file is supposed to have the file extension `.expr`. Not sure if there is a pattern we follow project-wide for custom file extensions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1841104745
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
Review Comment:
only cloud mode for stream...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1773657326
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -71,13 +72,15 @@ CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Me
A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
----
-The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
-
== Using the bin/solr stream Tool
+NOTE: The Stream Tool is classified as "experimental".
Review Comment:
love it
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r2095533510
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java:
##########
@@ -55,6 +50,8 @@ public class SolrClientCache implements Closeable {
private static final int minSocketTimeout =
Math.max(Integer.getInteger(HttpClientUtil.PROP_SO_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);
+ private String basicAuthCredentials = null; // Only support with the http2SolrClient
Review Comment:
@epugh I don't agree with this because it's basically a one-off setting when there are many other potential settings. I don't think basic auth is so special or so extremely widely used that it deserves its very own setter. It sets an unfortunate precedent from someone interesting in setting something else. The SolrClientCache _already_ provides a constructor that accepts an existing HTTP client , and that which can be initialized with basic auth.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840261645
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
Review Comment:
I think we said tab! So trying it with a tab. Yet, it could be just two spaces...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2473616312
@gerlowskija @malliaridis What do you think about using the word "environment" versus "context" for the option you pass for execution? I.e, your environment is either local or solr... Context to me means more like "this is athing that you set up that has lots of variables".... "enviroment" is where you run it...?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "gerlowskija (via GitHub)" <gi...@apache.org>.
gerlowskija commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2145825393
A few high-level questions/concerns:
1. `bin/solr` already has an "api" tool, which can be used to invoke streaming expressions e.g. `bin/solr api -get "$SOLR_URL/techproducts/stream?expr=search(techproducts)"`. I'm all for syntactic-sugar, but I wonder whether this is worth the maintenance cost if the main thing that it "buys" us is saving people from having to provide the full API path as the "api" tool requires?
2. If I'm reading the PR correctly, it looks like one other capability of the proposed `bin/solr stream` tool is that it can evaluate streams "locally" in some cases i.e. without a full running Solr. Which is pretty cool - you could imagine a real super-user doing some pretty involved ETL that builds off of an expression like: `update(techproducts, unique(cat(...)))`.
But I'd worry about some of the documentation challenges surrounding this. For instance, how would a user know which expressions can be run locally, and which require a Solr to execute on? For expressions that have a mix of both locally and remotely-executed clauses, is there any way for a user to know which clauses are executed where?
To clarify - I think the upside here is pretty cool, I'm just worried about the documentation end and what we might need to make it usable by folks in practice.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2465603881
A few more notes:
- [ ] `bin/solr stream --help` does not include any reference to the stream expression argument required at position 0 (or any other position that is valid)
- [ ] When providing an invalid stream expression, error message is not helpful
```sh
bin\solr.cmd stream "search(q=*:*)" -e local -s http://localhost:8983 -c techproducts
```
(missing collection in expression) returns "Unable to construct instance of org.apache.solr.client.solrj.io.stream.SearchFacadeStream"
- I would like a message that at least says something like "double check your stream expression" if it is one of the causes
- [ ] For my understanding, is it a technical limitation that `-e local` does not require a collection (`-c`), but `-e solr` requires one?
- [ ] I also noticed that providing `-s` requires a URI scheme, but `-z` does not allow one. Is this inconsistency for all commands?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "gerlowskija (via GitHub)" <gi...@apache.org>.
gerlowskija commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1761633393
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
Review Comment:
[0] I don't love this terminology, but I don't have anything better in mind (yet). "Local" to me could be misconstrued by folks running 'bin/solr' on a box that also happens to have Solr running.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1834874458
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
Review Comment:
Any `.required(false)` is obsolete and could be removed.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
Review Comment:
This is block no 2
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
+
+ TupleStream stream;
+ PushBackStream pushBackStream;
+
+ StreamExpression streamExpression = StreamExpressionParser.parse(expr);
+ StreamFactory streamFactory = new StreamFactory();
+
+ // stdin is ONLY available in the local mode, not in the remote mode as it
+ // requires access to System.in
+ streamFactory.withFunctionName("stdin", StandardInStream.class);
+
+ // LocalCatStream extends CatStream and disables the Solr cluster specific
+ // logic about where to read data from.
+ streamFactory.withFunctionName("cat", LocalCatStream.class);
+
+ streamFactory.withDefaultZkHost(zkHost);
+
+ Lang.register(streamFactory);
+
+ stream = StreamTool.constructStream(streamFactory, streamExpression);
+
+ pushBackStream = new PushBackStream(stream);
+
+ // Now we can run the stream and return the results.
+ StreamContext streamContext = new StreamContext();
+ streamContext.setSolrClientCache(solrClientCache);
+
+ // Output the headers
+ pushBackStream.setStreamContext(streamContext);
+
+ return pushBackStream;
+ }
+
+ public PushBackStream doRemoteMode(CommandLine cli, String expr) throws Exception {
+
+ String solrUrl = SolrCLI.normalizeSolrUrl(cli);
+ if (!cli.hasOption("name")) {
Review Comment:
Use option object instead for consistency.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
Review Comment:
I believe these methods could all be private if the tests do not directly access them.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
Review Comment:
What about user-managed mode? Is it still supported, or is only cloud mode supported yet? If I am not mistaken `getConnectionOptions` include a solr-url as well.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
Review Comment:
There is a lot happening in the `runImpl`, maybe extracting the two blocks into separate private methods would keep a better overview? (this is block no 1)
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
+
+ TupleStream stream;
+ PushBackStream pushBackStream;
+
+ StreamExpression streamExpression = StreamExpressionParser.parse(expr);
+ StreamFactory streamFactory = new StreamFactory();
+
+ // stdin is ONLY available in the local mode, not in the remote mode as it
+ // requires access to System.in
+ streamFactory.withFunctionName("stdin", StandardInStream.class);
+
+ // LocalCatStream extends CatStream and disables the Solr cluster specific
+ // logic about where to read data from.
+ streamFactory.withFunctionName("cat", LocalCatStream.class);
+
+ streamFactory.withDefaultZkHost(zkHost);
+
+ Lang.register(streamFactory);
+
+ stream = StreamTool.constructStream(streamFactory, streamExpression);
+
+ pushBackStream = new PushBackStream(stream);
+
+ // Now we can run the stream and return the results.
+ StreamContext streamContext = new StreamContext();
+ streamContext.setSolrClientCache(solrClientCache);
+
+ // Output the headers
+ pushBackStream.setStreamContext(streamContext);
+
+ return pushBackStream;
+ }
+
+ public PushBackStream doRemoteMode(CommandLine cli, String expr) throws Exception {
+
+ String solrUrl = SolrCLI.normalizeSolrUrl(cli);
+ if (!cli.hasOption("name")) {
+ throw new IllegalStateException(
+ "You must provide --name COLLECTION with --worker solr parameter.");
+ }
+ String collection = cli.getOptionValue("name");
Review Comment:
Use option object instead for consistency.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
Review Comment:
If this loads an exprssion from a file, would it not be better to use ".expr" instead of "expr"?
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
Review Comment:
I think you could simplify it by directly returning the value of `opts` here. Prevents any "surprises" further down this method.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
+
+ TupleStream stream;
+ PushBackStream pushBackStream;
+
+ StreamExpression streamExpression = StreamExpressionParser.parse(expr);
+ StreamFactory streamFactory = new StreamFactory();
+
+ // stdin is ONLY available in the local mode, not in the remote mode as it
+ // requires access to System.in
+ streamFactory.withFunctionName("stdin", StandardInStream.class);
+
+ // LocalCatStream extends CatStream and disables the Solr cluster specific
+ // logic about where to read data from.
+ streamFactory.withFunctionName("cat", LocalCatStream.class);
+
+ streamFactory.withDefaultZkHost(zkHost);
+
+ Lang.register(streamFactory);
+
+ stream = StreamTool.constructStream(streamFactory, streamExpression);
+
+ pushBackStream = new PushBackStream(stream);
+
+ // Now we can run the stream and return the results.
+ StreamContext streamContext = new StreamContext();
+ streamContext.setSolrClientCache(solrClientCache);
+
+ // Output the headers
+ pushBackStream.setStreamContext(streamContext);
+
+ return pushBackStream;
+ }
+
+ public PushBackStream doRemoteMode(CommandLine cli, String expr) throws Exception {
+
+ String solrUrl = SolrCLI.normalizeSolrUrl(cli);
+ if (!cli.hasOption("name")) {
+ throw new IllegalStateException(
+ "You must provide --name COLLECTION with --worker solr parameter.");
+ }
+ String collection = cli.getOptionValue("name");
+
+ if (expr.toLowerCase(Locale.ROOT).contains("stdin(")) {
+ throw new IllegalStateException(
+ "The stdin() expression is only usable with --worker local set up.");
+ }
+
+ final SolrStream solrStream =
+ new SolrStream(solrUrl + "/solr/" + collection, params("qt", "/stream", "expr", expr));
+
+ String credentials = cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION);
+ if (credentials != null) {
+ String username = credentials.split(":")[0];
+ String password = credentials.split(":")[1];
+ solrStream.setCredentials(username, password);
+ }
+ return new PushBackStream(solrStream);
+ }
+
+ public static ModifiableSolrParams params(String... params) {
+ if (params.length % 2 != 0) throw new RuntimeException("Params length should be even");
+ ModifiableSolrParams msp = new ModifiableSolrParams();
+ for (int i = 0; i < params.length; i += 2) {
+ msp.add(params[i], params[i + 1]);
+ }
+ return msp;
+ }
+
+ public static class StandardInStream extends TupleStream implements Expressible {
+
+ private BufferedReader reader;
+ private InputStream inputStream = System.in;
+ private boolean doClose = false;
+
+ public StandardInStream() {}
+
+ public StandardInStream(StreamExpression expression, StreamFactory factory)
+ throws IOException {}
+
+ @Override
+ public List<TupleStream> children() {
+ return null;
+ }
+
+ public void setInputStream(InputStream inputStream) {
+ this.inputStream = inputStream;
+ this.doClose = true;
+ }
+
+ @Override
+ public void open() {
+ reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (doClose) {
+ inputStream.close();
+ }
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override
+ public Tuple read() throws IOException {
+ String line = reader.readLine();
+ HashMap map = new HashMap();
+ Tuple tuple = new Tuple(map);
+ if (line != null) {
+ tuple.put("line", line);
+ tuple.put("file", "cat");
+ } else {
+ tuple.put("EOF", "true");
+ }
+ return tuple;
+ }
+
+ @Override
+ public void setStreamContext(StreamContext context) {}
+
+ @Override
+ public StreamExpression toExpression(StreamFactory factory) {
+ return null;
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) {
+ return null;
+ }
+
+ @Override
+ public StreamComparator getStreamSort() {
+ return null;
+ }
+ }
+
+ public static String[] getOutputFields(CommandLine cli) {
+ if (cli.hasOption("fields")) {
+
+ String fl = cli.getOptionValue("fields");
Review Comment:
Use option object instead for consistency.
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
Review Comment:
Are multiple spaces as delimeter fine / expected?
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
+
+ TupleStream stream;
+ PushBackStream pushBackStream;
+
+ StreamExpression streamExpression = StreamExpressionParser.parse(expr);
+ StreamFactory streamFactory = new StreamFactory();
+
+ // stdin is ONLY available in the local mode, not in the remote mode as it
+ // requires access to System.in
+ streamFactory.withFunctionName("stdin", StandardInStream.class);
+
+ // LocalCatStream extends CatStream and disables the Solr cluster specific
+ // logic about where to read data from.
+ streamFactory.withFunctionName("cat", LocalCatStream.class);
+
+ streamFactory.withDefaultZkHost(zkHost);
+
+ Lang.register(streamFactory);
+
+ stream = StreamTool.constructStream(streamFactory, streamExpression);
+
+ pushBackStream = new PushBackStream(stream);
+
+ // Now we can run the stream and return the results.
+ StreamContext streamContext = new StreamContext();
+ streamContext.setSolrClientCache(solrClientCache);
+
+ // Output the headers
+ pushBackStream.setStreamContext(streamContext);
+
+ return pushBackStream;
+ }
+
+ public PushBackStream doRemoteMode(CommandLine cli, String expr) throws Exception {
+
+ String solrUrl = SolrCLI.normalizeSolrUrl(cli);
+ if (!cli.hasOption("name")) {
+ throw new IllegalStateException(
+ "You must provide --name COLLECTION with --worker solr parameter.");
+ }
+ String collection = cli.getOptionValue("name");
+
+ if (expr.toLowerCase(Locale.ROOT).contains("stdin(")) {
+ throw new IllegalStateException(
+ "The stdin() expression is only usable with --worker local set up.");
+ }
+
+ final SolrStream solrStream =
+ new SolrStream(solrUrl + "/solr/" + collection, params("qt", "/stream", "expr", expr));
Review Comment:
Don't like string concats for URLs, because we get into "v1 / v2" dilemma, but I am not sure if there is another way for this?
##########
solr/benchmark/.factorypath:
##########
@@ -0,0 +1,6 @@
+<factorypath>
+ <factorypathentry kind="EXTJAR" id="/Users/epugh/.gradle/caches/modules-2/files-2.1/org.apache.commons/commons-math3/3.6.1/e4ba98f1d4b3c80ec46392f25e094a6a2e58fcbf/commons-math3-3.6.1.jar" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="EXTJAR" id="/Users/epugh/.gradle/caches/modules-2/files-2.1/org.openjdk.jmh/jmh-generator-annprocess/1.37/da93888682df163144edf9b13d2b78e54166063a/jmh-generator-annprocess-1.37.jar" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="EXTJAR" id="/Users/epugh/.gradle/caches/modules-2/files-2.1/net.sf.jopt-simple/jopt-simple/5.0.4/4fdac2fbe92dfad86aa6e9301736f6b4342a3f5c/jopt-simple-5.0.4.jar" enabled="true" runInBatchMode="false"/>
+ <factorypathentry kind="EXTJAR" id="/Users/epugh/.gradle/caches/modules-2/files-2.1/org.openjdk.jmh/jmh-core/1.37/896f27e49105b35ea1964319c83d12082e7a79ef/jmh-core-1.37.jar" enabled="true" runInBatchMode="false"/>
+</factorypath>
Review Comment:
I believe you didn't mean to push this file?
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/stream/LetStream.java:
##########
@@ -224,4 +224,9 @@ public StreamComparator getStreamSort() {
public int getCost() {
return 0;
}
+
+ @SuppressWarnings({"rawtypes"})
+ public Map getLetParams() {
+ return this.letParams;
+ }
Review Comment:
This method has no usage, is it needed? And if so, should we not simply add the types to `Map<String, Object>` rather than suppressing them?
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
+
+ TupleStream stream;
+ PushBackStream pushBackStream;
+
+ StreamExpression streamExpression = StreamExpressionParser.parse(expr);
+ StreamFactory streamFactory = new StreamFactory();
+
+ // stdin is ONLY available in the local mode, not in the remote mode as it
+ // requires access to System.in
+ streamFactory.withFunctionName("stdin", StandardInStream.class);
+
+ // LocalCatStream extends CatStream and disables the Solr cluster specific
+ // logic about where to read data from.
+ streamFactory.withFunctionName("cat", LocalCatStream.class);
+
+ streamFactory.withDefaultZkHost(zkHost);
+
+ Lang.register(streamFactory);
+
+ stream = StreamTool.constructStream(streamFactory, streamExpression);
+
+ pushBackStream = new PushBackStream(stream);
+
+ // Now we can run the stream and return the results.
+ StreamContext streamContext = new StreamContext();
+ streamContext.setSolrClientCache(solrClientCache);
+
+ // Output the headers
+ pushBackStream.setStreamContext(streamContext);
+
+ return pushBackStream;
+ }
+
+ public PushBackStream doRemoteMode(CommandLine cli, String expr) throws Exception {
+
+ String solrUrl = SolrCLI.normalizeSolrUrl(cli);
+ if (!cli.hasOption("name")) {
+ throw new IllegalStateException(
+ "You must provide --name COLLECTION with --worker solr parameter.");
+ }
+ String collection = cli.getOptionValue("name");
+
+ if (expr.toLowerCase(Locale.ROOT).contains("stdin(")) {
+ throw new IllegalStateException(
+ "The stdin() expression is only usable with --worker local set up.");
+ }
+
+ final SolrStream solrStream =
+ new SolrStream(solrUrl + "/solr/" + collection, params("qt", "/stream", "expr", expr));
+
+ String credentials = cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION);
+ if (credentials != null) {
+ String username = credentials.split(":")[0];
+ String password = credentials.split(":")[1];
+ solrStream.setCredentials(username, password);
+ }
+ return new PushBackStream(solrStream);
+ }
+
+ public static ModifiableSolrParams params(String... params) {
+ if (params.length % 2 != 0) throw new RuntimeException("Params length should be even");
+ ModifiableSolrParams msp = new ModifiableSolrParams();
+ for (int i = 0; i < params.length; i += 2) {
+ msp.add(params[i], params[i + 1]);
+ }
+ return msp;
+ }
+
+ public static class StandardInStream extends TupleStream implements Expressible {
+
+ private BufferedReader reader;
+ private InputStream inputStream = System.in;
+ private boolean doClose = false;
+
+ public StandardInStream() {}
+
+ public StandardInStream(StreamExpression expression, StreamFactory factory)
+ throws IOException {}
+
+ @Override
+ public List<TupleStream> children() {
+ return null;
+ }
+
+ public void setInputStream(InputStream inputStream) {
+ this.inputStream = inputStream;
+ this.doClose = true;
+ }
+
+ @Override
+ public void open() {
+ reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (doClose) {
+ inputStream.close();
+ }
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override
+ public Tuple read() throws IOException {
+ String line = reader.readLine();
+ HashMap map = new HashMap();
+ Tuple tuple = new Tuple(map);
+ if (line != null) {
+ tuple.put("line", line);
+ tuple.put("file", "cat");
+ } else {
+ tuple.put("EOF", "true");
+ }
+ return tuple;
+ }
+
+ @Override
+ public void setStreamContext(StreamContext context) {}
+
+ @Override
+ public StreamExpression toExpression(StreamFactory factory) {
+ return null;
+ }
+
+ @Override
+ public Explanation toExplanation(StreamFactory factory) {
+ return null;
+ }
+
+ @Override
+ public StreamComparator getStreamSort() {
+ return null;
+ }
+ }
+
+ public static String[] getOutputFields(CommandLine cli) {
+ if (cli.hasOption("fields")) {
Review Comment:
Use option object instead for consistency.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840282656
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
Review Comment:
These are space characters (3 spaces to be precise), I am not sure if this is intended if you wanted a tab? IDEs usually convert tabs to spaces by default.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r2095761990
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java:
##########
@@ -55,6 +50,8 @@ public class SolrClientCache implements Closeable {
private static final int minSocketTimeout =
Math.max(Integer.getInteger(HttpClientUtil.PROP_SO_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);
+ private String basicAuthCredentials = null; // Only support with the http2SolrClient
Review Comment:
I could get behind that. It's not always obvious which settings are done on the http client level and which are at the solr level ;-).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840262175
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
Review Comment:
sure! Might make it more readable.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "gerlowskija (via GitHub)" <gi...@apache.org>.
gerlowskija commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1773483777
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
Review Comment:
I noodled on this a bit but still couldn't come up with anything I liked better, so maybe you picked the best option. +1 for the current enum values with the "(i.e. CLI process)" clarification you suggested.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "gerlowskija (via GitHub)" <gi...@apache.org>.
gerlowskija commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1773493224
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -71,13 +72,15 @@ CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Me
A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
----
-The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
-
== Using the bin/solr stream Tool
+NOTE: The Stream Tool is classified as "experimental".
Review Comment:
Might be worth putting this at the top of the page. Seems a little "buried" as-is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1763521501
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
Review Comment:
Thoughts on:
```
Execution context is either 'local' (i.e CLI process) or 'solr'.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840260149
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
Review Comment:
we could.. yet we have this pattern everywehre!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2474698022
@malliaridis in reference to your question, yeah -z never has a scheme, but -s always does. Why exactly, not totally sure.. I guess that ZK communciates over same port regardless of ssl or not?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2473647474
Leaning towards "execution" and either "local" or "remote".
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1774352198
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
+ -z,--zk-host <HOST> Zookeeper connection string; unnecessary if ZK_HOST is defined in solr.in.sh;
+ otherwise, defaults to localhost:9983.
+----
+
+== Examples Using bin/solr stream
+
+There are several ways to use `bin/solr stream`.
+This section presents several examples.
+
+=== Executing Expression Locally
+
+Streaming Expressions by default are executed in the Solr cluster.
+However there are use cases where you want to interact with data in your local environment, or even run a streaming expression independent of Solr.
+
+The Stream Tool allows you to specify `--execution local` to process the expression in the Solr CLI's JVM.
+
+Assuming you have create the file `load_data.expr` with the contents:
+
+----
+# Index CSV File
+
+update(
+ gettingstarted,
+ parseCSV(
+ cat(./example/exampledocs/books.csv, maxLines=2)
+ )
+)
+----
+
+Running this expression will read in the local file and send the first two lines to the collection `gettingstarted`.
Review Comment:
okay, I took a stab at it... If I didn't quite nail it, please edit it but with myabe the suggestion thing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "malliaridis (via GitHub)" <gi...@apache.org>.
malliaridis commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1834961429
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
+ : new StringReader(expressionArgument);
+
+ bufferedReader = new LineNumberReader(inputStream);
+ expr = StreamTool.readExpression(bufferedReader, cli.getArgs());
+ echoIfVerbose("Running Expression: " + expr);
+ } finally {
+ if (bufferedReader != null) {
+ bufferedReader.close();
+ }
+ }
+
+ PushBackStream pushBackStream;
+ if (execution.equalsIgnoreCase("local")) {
+ pushBackStream = doLocalMode(cli, expr);
+ } else {
+ pushBackStream = doRemoteMode(cli, expr);
+ }
+
+ try {
+ pushBackStream.open();
+
+ if (outputHeaders == null) {
+
+ Tuple tuple = pushBackStream.read();
+
+ if (!tuple.EOF) {
+ outputHeaders = getHeadersFromFirstTuple(tuple);
+ }
+
+ pushBackStream.pushBack(tuple);
+ }
+
+ if (includeHeaders) {
+ StringBuilder headersOut = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ headersOut.append(delimiter);
+ }
+ headersOut.append(outputHeaders[i]);
+ }
+ }
+ CLIO.out(headersOut.toString());
+ }
+
+ while (true) {
+ Tuple tuple = pushBackStream.read();
+ if (tuple.EOF) {
+ break;
+ } else {
+ StringBuilder outLine = new StringBuilder();
+ if (outputHeaders != null) {
+ for (int i = 0; i < outputHeaders.length; i++) {
+ if (i > 0) {
+ outLine.append(delimiter);
+ }
+
+ Object o = tuple.get(outputHeaders[i]);
+ if (o != null) {
+ if (o instanceof List) {
+ List outfields = (List) o;
+ outLine.append(listToString(outfields, arrayDelimiter));
+ } else {
+ outLine.append(o);
+ }
+ }
+ }
+ }
+ CLIO.out(outLine.toString());
+ }
+ }
+ } finally {
+
+ if (pushBackStream != null) {
+ pushBackStream.close();
+ }
+
+ solrClientCache.close();
+ }
+
+ echoIfVerbose("StreamTool -- Done.");
+ }
+
+ public PushBackStream doLocalMode(CommandLine cli, String expr) throws Exception {
+ String zkHost = SolrCLI.getZkHost(cli);
+
+ echoIfVerbose("Connecting to ZooKeeper at " + zkHost);
+ solrClientCache.getCloudSolrClient(zkHost);
+ solrClientCache.setBasicAuthCredentials(cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION));
Review Comment:
Apparently, this works for both cloud and user-managed. Strange, but it is what it is.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2282307377
@gerlowskija since you provided some early review, do you think the docs I've added etc are enough that I can merge this in?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "gerlowskija (via GitHub)" <gi...@apache.org>.
gerlowskija commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1760947380
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
Review Comment:
[0] Might be worth mentioning here the other differentiator - that this executes some streams "locally"?
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
+ -z,--zk-host <HOST> Zookeeper connection string; unnecessary if ZK_HOST is defined in solr.in.sh;
+ otherwise, defaults to localhost:9983.
+----
+
+== Examples Using bin/solr stream
+
+There are several ways to use `bin/solr stream`.
+This section presents several examples.
+
+=== Executing Expression Locally
+
+Streaming Expressions by default are executed in the Solr cluster.
+However there are use cases where you want to interact with data in your local environment, or even run a streaming expression independent of Solr.
+
+The Stream Tool allows you to specify `--execution local` to process the expression in the Solr CLI's JVM.
+
+Assuming you have create the file `load_data.expr` with the contents:
+
+----
+# Index CSV File
+
+update(
+ gettingstarted,
+ parseCSV(
+ cat(./example/exampledocs/books.csv, maxLines=2)
+ )
+)
+----
+
+Running this expression will read in the local file and send the first two lines to the collection `gettingstarted`.
Review Comment:
[0] Might be worth hitting this point a little more strongly: even "local" processing is likely to reach out to a remote host.
Maybe something like:
> All streaming expressions are processed "locally" if that execution mode is selected.
> However, "local" processing does not imply a networking sandbox.
> Many streaming expressions, such as `search` and `update`, will make network requests to remote Solr nodes if configured to do so, even in "local" execution mode.
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
+ -z,--zk-host <HOST> Zookeeper connection string; unnecessary if ZK_HOST is defined in solr.in.sh;
+ otherwise, defaults to localhost:9983.
+----
+
+== Examples Using bin/solr stream
+
+There are several ways to use `bin/solr stream`.
+This section presents several examples.
+
+=== Executing Expression Locally
+
+Streaming Expressions by default are executed in the Solr cluster.
+However there are use cases where you want to interact with data in your local environment, or even run a streaming expression independent of Solr.
+
+The Stream Tool allows you to specify `--execution local` to process the expression in the Solr CLI's JVM.
+
+Assuming you have create the file `load_data.expr` with the contents:
+
+----
+# Index CSV File
+
+update(
+ gettingstarted,
+ parseCSV(
+ cat(./example/exampledocs/books.csv, maxLines=2)
+ )
+)
+----
+
+Running this expression will read in the local file and send the first two lines to the collection `gettingstarted`.
+
+TIP: Want to send data to a remote Solr? pass in `--solr-url http://solr.remote:8983`.
+
+
+[,console]
+----
+$ bin/solr stream --execution local --header load_data.expr
+----
+
+
+The StreamTool adds some Streaming Expressions specifically for local use:
+
+* stdin() lets you pipe data directly into the streaming expression.
+* cat() that allows you to read ANY file on your local system. This is different from the xref:stream-source-reference.adoc#cat[`cat`] operator that runs in Solr that only accesses `$SOLR_HOME/userfiles/`.
+
+Caveats:
+
+ * You don't get to use any of the parallelization support that is available when you run the expression on the cluster.
Review Comment:
[Q] Is this only a limitation of `--execution=local` is specified?
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
Review Comment:
[Q] Should this sentence go in the "Using the ..." section below, since that section essentially pastes the "bin/solr stream -h" output in its entirety?
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
Review Comment:
[0] I don't love this terminology, but I don't have anything better in mind (yet). "Local" to me could be misconstrued by folks running 'bin/solr' on a box that also happens to have Solr running.
[0] How does the 'execution' option interact with '--solr-url'? Maybe the help docs, or ref-guide docs, or both should make a mention of those edge cases?
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
Review Comment:
[0] "with in" -> "within"
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
Review Comment:
[0] "window" -> "terminal" or maybe "shell"?
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
Review Comment:
[0] "or if you having" -> or if you have"
Might also be worth clarifying that the file approach primarily helps with shell character-escaping issues, and not parsing/syntax issues generally.
##########
solr/solr-ref-guide/modules/query-guide/pages/stream-tool.adoc:
##########
@@ -0,0 +1,167 @@
+= Stream Tool
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+The Stream tool allows you to run a xref:streaming-expressions.adoc[] and see the results from the command line.
+It is very similar to the xref:stream-screen.adoc[], but is part of the `bin/solr` CLI.
+
+To run it, open a window and enter:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts --delimiter=\| 'search(techproducts,q="name:memory",fl="name,price")'
+----
+
+This will run the provided streaming expression on the `techproducts` collection and produce:
+
+[,console]
+----
+name|price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail|185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail|74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM|
+----
+
+TIP: Notice how we used the pipe character (|) as the delimiter? It required a backslash for escaping it so it wouldn't be treated as a pipe with in the shell script.
+
+You can also specify a file with the suffix `.expr` containing your streaming expression.
+This is useful for longer expressions or if you having command line parsing issues with your expression.
+
+Assuming you have create the file `stream.expr` with the contents:
+
+----
+# Stream a search
+
+search(
+ techproducts,
+ q="name:memory",
+ fl="name,price",
+ sort="price desc"
+)
+----
+
+Then you can run it on the Solr collection `techproducts`, specifying you want a header row:
+
+[,console]
+----
+$ bin/solr stream --header -c techproducts stream.expr
+----
+
+And this will produce:
+
+[,console]
+----
+name price
+CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail 185.0
+CORSAIR ValueSelect 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - Retail 74.99
+A-DATA V-Series 1GB 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) System Memory - OEM
+----
+
+The `--help` (or simply `-h`) option will output information on its usage (i.e., `bin/solr stream --help)`.
+
+== Using the bin/solr stream Tool
+
+To use the tool you need to provide the streaming expression either inline as the last argument, or provide a file ending in `.expr` that contains the expression.
+
+The basic usage of `bin/solr stream` is:
+
+[source,plain]
+----
+usage: bin/solr stream [--array-delimiter <CHARACTER>] [-c <NAME>] [--delimiter <CHARACTER>] [-f <FIELDS>] [--header]
+ [-u <credentials>] [-url <HOST>] [--workers <arg>] [-z <HOST>]
+
+List of options:
+ --array-delimiter <CHARACTER> The delimiter multi-valued fields. (default=|)
+ -c,--name <NAME> Name of the collection to execute on if workers are 'solr'. Required for 'solr'
+ worker.
+ --delimiter <CHARACTER> The output delimiter. (default=tab).
+ -f,--fields <FIELDS> The fields in the tuples to output. (defaults to fields in the first tuple of result
+ set).
+ --header Whether or not to include a header line. (default=false)
+ -u,--credentials <credentials> Credentials in the format username:password. Example: --credentials solr:SolrRocks
+ -url,--solr-url <HOST> Base Solr URL, which can be used to determine the zk-host if that's not known;
+ defaults to: http://localhost:8983.
+ -e,--execution <CONTEXT> Execution context is either 'local' or 'solr'. Default is 'solr'
+ -z,--zk-host <HOST> Zookeeper connection string; unnecessary if ZK_HOST is defined in solr.in.sh;
+ otherwise, defaults to localhost:9983.
+----
+
+== Examples Using bin/solr stream
+
+There are several ways to use `bin/solr stream`.
+This section presents several examples.
+
+=== Executing Expression Locally
+
+Streaming Expressions by default are executed in the Solr cluster.
+However there are use cases where you want to interact with data in your local environment, or even run a streaming expression independent of Solr.
+
+The Stream Tool allows you to specify `--execution local` to process the expression in the Solr CLI's JVM.
+
+Assuming you have create the file `load_data.expr` with the contents:
+
+----
+# Index CSV File
+
+update(
+ gettingstarted,
+ parseCSV(
+ cat(./example/exampledocs/books.csv, maxLines=2)
+ )
+)
+----
+
+Running this expression will read in the local file and send the first two lines to the collection `gettingstarted`.
+
+TIP: Want to send data to a remote Solr? pass in `--solr-url http://solr.remote:8983`.
+
+
+[,console]
+----
+$ bin/solr stream --execution local --header load_data.expr
+----
+
+
+The StreamTool adds some Streaming Expressions specifically for local use:
+
+* stdin() lets you pipe data directly into the streaming expression.
+* cat() that allows you to read ANY file on your local system. This is different from the xref:stream-source-reference.adoc#cat[`cat`] operator that runs in Solr that only accesses `$SOLR_HOME/userfiles/`.
+
+Caveats:
+
+ * You don't get to use any of the parallelization support that is available when you run the expression on the cluster.
+
+=== Piping data to an expression
+
+Index a CSV file into `gettingstarted` collection.
+
+[,console]
+----
+$ cat example/exampledocs/books.csv | bin/solr stream -e local 'update(gettingstarted,parseCSV(stdin()))'
+----
+
+=== Variable interpolation
+
+You can do variable interpolation via having `$1`, `$2` etc in your streaming expression, and then passing those values as arguments.
+
+[,console]
+----
+$ bin/solr stream -c techproducts 'echo("$1")' "Hello World"
+Hello World
+----
+
+This also works with a `.expr` files.
Review Comment:
[0] "also works with a `.expr` files." -> "also works when using `.expr` files."
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1840259437
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
Review Comment:
Thanks!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh merged PR #2479:
URL: https://github.com/apache/solr/pull/2479
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r1841105384
##########
solr/core/src/java/org/apache/solr/cli/StreamTool.java:
##########
@@ -0,0 +1,507 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.cli;
+
+import java.io.BufferedReader;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.LineNumberReader;
+import java.io.PrintStream;
+import java.io.Reader;
+import java.io.StringReader;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.solr.client.solrj.io.Lang;
+import org.apache.solr.client.solrj.io.SolrClientCache;
+import org.apache.solr.client.solrj.io.Tuple;
+import org.apache.solr.client.solrj.io.comp.StreamComparator;
+import org.apache.solr.client.solrj.io.stream.PushBackStream;
+import org.apache.solr.client.solrj.io.stream.SolrStream;
+import org.apache.solr.client.solrj.io.stream.StreamContext;
+import org.apache.solr.client.solrj.io.stream.TupleStream;
+import org.apache.solr.client.solrj.io.stream.expr.Explanation;
+import org.apache.solr.client.solrj.io.stream.expr.Expressible;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionParser;
+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.handler.CatStream;
+
+/** Supports stream command in the bin/solr script. */
+public class StreamTool extends ToolBase {
+
+ public StreamTool() {
+ this(CLIO.getOutStream());
+ }
+
+ public StreamTool(PrintStream stdout) {
+ super(stdout);
+ }
+
+ private final SolrClientCache solrClientCache = new SolrClientCache();
+
+ @Override
+ public String getName() {
+ return "stream";
+ }
+
+ private static final Option EXECUTION_OPTION =
+ Option.builder("e")
+ .longOpt("execution")
+ .hasArg()
+ .argName("CONTEXT")
+ .required(false)
+ .desc(
+ "Execution context is either 'local' (i.e CLI process) or 'solr'. Default is 'solr'")
+ .build();
+
+ private static final Option COLLECTION_OPTION = Option.builder("c")
+ .longOpt("name")
+ .argName("NAME")
+ .hasArg()
+ .desc(
+ "Name of the collection to execute on if workers are 'solr'. Required for 'solr' worker.")
+ .build();
+
+ private static final Option FIELDS_OPTION = Option.builder("f")
+ .longOpt("fields")
+ .argName("FIELDS")
+ .hasArg()
+ .required(false)
+ .desc(
+ "The fields in the tuples to output. (defaults to fields in the first tuple of result set).")
+ .build();
+
+ private static final Option HEADER_OPTION =
+ Option.builder()
+ .longOpt("header")
+ .required(false)
+ .desc("Whether or not to include a header line. (default=false)")
+ .build();
+ private static final Option DELIMITER_OPTION = Option.builder()
+ .longOpt("delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The output delimiter. (default=tab).")
+ .build();
+ private static final Option ARRAY_DELIMITER_OPTION = Option.builder()
+ .longOpt("array-delimiter")
+ .argName("CHARACTER")
+ .hasArg()
+ .required(false)
+ .desc("The delimiter multi-valued fields. (default=|)")
+ .build();
+
+ @Override
+ public Options getOptions() {
+ Options opts =
+ super.getOptions()
+ .addOption(EXECUTION_OPTION)
+ .addOption(COLLECTION_OPTION)
+ .addOption(FIELDS_OPTION)
+ .addOption(HEADER_OPTION)
+ .addOption(DELIMITER_OPTION)
+ .addOption(ARRAY_DELIMITER_OPTION)
+ .addOption(CommonCLIOptions.CREDENTIALS_OPTION)
+ .addOptionGroup(getConnectionOptions());
+
+ return opts;
+ }
+
+
+ @Override
+ @SuppressWarnings({"rawtypes"})
+ public void runImpl(CommandLine cli) throws Exception {
+
+ String expressionArgument = cli.getArgs()[0];
+ String execution = cli.getOptionValue(EXECUTION_OPTION,"solr");
+ String arrayDelimiter = cli.getOptionValue(ARRAY_DELIMITER_OPTION,"|");
+ String delimiter = cli.getOptionValue(DELIMITER_OPTION, " ");
+ boolean includeHeaders = cli.hasOption(HEADER_OPTION);
+ String[] outputHeaders = getOutputFields(cli);
+
+ LineNumberReader bufferedReader = null;
+ String expr;
+ try {
+ Reader inputStream =
+ expressionArgument.toLowerCase(Locale.ROOT).endsWith("expr")
+ ? new InputStreamReader(
+ new FileInputStream(expressionArgument), Charset.defaultCharset())
Review Comment:
i think for expressions they historically have had `.expr`!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "epugh (via GitHub)" <gi...@apache.org>.
epugh commented on PR #2479:
URL: https://github.com/apache/solr/pull/2479#issuecomment-2149443128
Thanks for sharing the feedback @gerlowskija ! I think the value of the tool is only there if your second comment about being able to run a streaming expression locally is valid, and then having it do what yoru first comment highlights falls out easy, otherwise it really is a thin wrapper/duplication of the `bin/solr api` call. Especially without any special value add in formatting tuples or error handling etc.
I do believe the second part is the really cool thing, that I can run a streaming expression locally and use it to process some data.
We clearly need some way of specifying where the processing is happening, in the cluster or locally. I was trying to think if we have any other places in Solr where we define "Where am I doing work" that might provide a name for a parameter. `bin/solr stream --environment cluster BLAH` ? The `search()` expression has a `qt` parameter.. `bin/solr stream -qt=/stream BLAH` ?
Reading through docs more, we have the `parralel()` and it refers to workers. Maybe the command should be something like `bin/solr stream --workers=local BLAH` which would run on your laptop, and if you don't specify --workers then it runs on the cluster via `/stream`?
I have found that lots of streaming expressions don't require a Solr connection, especially during development. I'm just iterating on the logic, and I'm starting and ending iwth tuples.. it's only later when I get the mappings etc working that I then move to adding in my `search()` or `update()` clauses.
Also, as far as docs go, we have a LONG way to go in Streaming expressions. It's both the best docuemnted code, with all the howtos and guides, but also, I find a million expressions that exist but don't show up in our reference docs ;-).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org
Re: [PR] SOLR-14673: Add bin/solr stream CLI [solr]
Posted by "dsmiley (via GitHub)" <gi...@apache.org>.
dsmiley commented on code in PR #2479:
URL: https://github.com/apache/solr/pull/2479#discussion_r2097944163
##########
solr/solrj-streaming/src/java/org/apache/solr/client/solrj/io/SolrClientCache.java:
##########
@@ -55,6 +50,8 @@ public class SolrClientCache implements Closeable {
private static final int minSocketTimeout =
Math.max(Integer.getInteger(HttpClientUtil.PROP_SO_TIMEOUT, MIN_TIMEOUT), MIN_TIMEOUT);
+ private String basicAuthCredentials = null; // Only support with the http2SolrClient
Review Comment:
> It's not always obvious which settings are done on the http client level and which are at the solr level ;-).
Hmm; I'm not sure how that's relevant to this situation. If you create an Http2SolrClient (with basic auth and whatever else) and pass it to SolrClientCache, it'll be used completely not partially.
I'm marking this setter deprecated in an upcoming PR loosely related to at least signal we don't want this.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@solr.apache.org
For additional commands, e-mail: issues-help@solr.apache.org