lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject lucene-solr:jira/solr-8593: Cleanup SQLHandler to use JDBCStream if not requiring metadata
Date Sat, 30 Apr 2016 14:48:01 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/jira/solr-8593 1bd2e8247 -> 1520ff381


Cleanup SQLHandler to use JDBCStream if not requiring metadata


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/1520ff38
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/1520ff38
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/1520ff38

Branch: refs/heads/jira/solr-8593
Commit: 1520ff3810050f70ad7175dd8f19ca1748279485
Parents: 1bd2e82
Author: Kevin Risden <krisden@apache.org>
Authored: Sat Apr 30 09:31:40 2016 -0500
Committer: Kevin Risden <krisden@apache.org>
Committed: Sat Apr 30 09:31:40 2016 -0500

----------------------------------------------------------------------
 .../org/apache/solr/handler/SQLHandler.java     | 72 ++++++++++++--------
 1 file changed, 44 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/1520ff38/solr/core/src/java/org/apache/solr/handler/SQLHandler.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/SQLHandler.java b/solr/core/src/java/org/apache/solr/handler/SQLHandler.java
index 7cc347a..6b3192c 100644
--- a/solr/core/src/java/org/apache/solr/handler/SQLHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/SQLHandler.java
@@ -36,6 +36,7 @@ import org.apache.calcite.config.Lex;
 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.ExceptionStream;
+import org.apache.solr.client.solrj.io.stream.JDBCStream;
 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;
@@ -63,7 +64,6 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
   private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
   public void inform(SolrCore core) {
-
     CoreContainer coreContainer = core.getCoreDescriptor().getCoreContainer();
 
     if(coreContainer.isZooKeeperAware()) {
@@ -85,8 +85,7 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
     params.set("workerCollection", params.get("workerCollection", defaultWorkerCollection));
     params.set("workerZkhost", params.get("workerZkhost", defaultZkhost));
     params.set("aggregationMode", params.get("aggregationMode", "map_reduce"));
-    // JDBC driver requires metadata from the SQLHandler. Default to false since this adds
a new Metadata stream.
-    params.set("includeMetadata", params.getBool("includeMetadata", false));
+
 
     TupleStream tupleStream = null;
     try {
@@ -94,11 +93,35 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
         throw new Exception("stmt parameter cannot be null");
       }
 
-      /*
-       * Would be great to replace this with the JDBCStream. Can't do that currently since
need to have metadata
-       * added to the stream for the JDBC driver. This could be fixed by using the Calcite
Avatica server and client.
-       */
-      tupleStream = new StreamHandler.TimerStream(new ExceptionStream(new SqlHandlerStream(sql,
params)));
+      String url = "jdbc:calcitesolr:";
+
+      Properties properties = new Properties();
+      // Add all query parameters
+      Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
+      while(parameterNamesIterator.hasNext()) {
+        String param = parameterNamesIterator.next();
+        properties.setProperty(param, params.get(param));
+      }
+
+      // Set these last to ensure that they are set properly
+      properties.setProperty("lex", Lex.MYSQL.toString());
+      properties.setProperty("zk", defaultZkhost);
+
+      String driverClass = CalciteSolrDriver.class.getCanonicalName();
+
+      // JDBC driver requires metadata from the SQLHandler. Default to false since this adds
a new Metadata stream.
+      if(params.getBool("includeMetadata", false)) {
+        /*
+         * Would be great to replace this with the JDBCStream. Can't do that currently since
need to have metadata
+         * added to the stream for the JDBC driver. This could be fixed by using the Calcite
Avatica server and client.
+         */
+        tupleStream = new SqlHandlerStream(url, sql, properties, driverClass);
+      } else {
+        tupleStream = new JDBCStream(url, sql, null, properties, driverClass);
+      }
+
+      tupleStream = new StreamHandler.TimerStream(new ExceptionStream(tupleStream));
+
 
       rsp.add("result-set", tupleStream);
     } catch(Exception e) {
@@ -119,9 +142,14 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
     return null;
   }
 
+  /*
+   * Only necessary for SolrJ JDBC driver since metadata has to be passed back
+   */
   private class SqlHandlerStream extends TupleStream {
+    private final String url;
     private final String sql;
-    private final SolrParams params;
+    private final Properties properties;
+    private final String driverClass;
     private boolean firstTuple = true;
     private Connection connection;
     private Statement statement;
@@ -129,9 +157,11 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
     private ResultSetMetaData resultSetMetaData;
     private int numColumns;
 
-    SqlHandlerStream(String sql, SolrParams params) {
+    SqlHandlerStream(String url, String sql, Properties properties, String driverClass) {
+      this.url = url;
       this.sql = sql;
-      this.params = params;
+      this.properties = properties;
+      this.driverClass = driverClass;
     }
 
     public List<TupleStream> children() {
@@ -139,26 +169,14 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
     }
 
     public void open() throws IOException {
-      Properties properties = new Properties();
-      // Add all query parameters
-      Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
-      while(parameterNamesIterator.hasNext()) {
-        String param = parameterNamesIterator.next();
-        properties.setProperty(param, params.get(param));
-      }
-
-      // Set these last to ensure that they are set properly
-      properties.setProperty("lex", Lex.MYSQL.toString());
-      properties.setProperty("zk", defaultZkhost);
-
       try {
-        Class.forName(CalciteSolrDriver.class.getCanonicalName());
+        Class.forName(driverClass);
       } catch (ClassNotFoundException e) {
         throw new IOException(e);
       }
 
       try {
-        connection = DriverManager.getConnection("jdbc:calcitesolr:", properties);
+        connection = DriverManager.getConnection(url, properties);
         statement = connection.createStatement();
         resultSet = statement.executeQuery(sql);
         resultSetMetaData = this.resultSet.getMetaData();
@@ -183,7 +201,7 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
     public Tuple read() throws IOException {
       try {
         Map<String, Object> fields = new HashMap<>();
-        if(firstTuple && params.getBool("includeMetadata")) {
+        if(firstTuple) {
           firstTuple = false;
 
           List<String> metadataFields = new ArrayList<>();
@@ -223,8 +241,6 @@ public class SQLHandler extends RequestHandlerBase implements SolrCoreAware
, Pe
         try {
           closeable.close();
         } catch (Exception ignore) {
-        } finally {
-          closeable = null;
         }
       }
     }


Mime
View raw message