From 48a16205fafd0d71aadc502be45fd1c9ee47adc1 Mon Sep 17 00:00:00 2001 From: Joel Bernstein Date: Fri, 18 Sep 2015 14:17:39 +0000 Subject: [PATCH] SOLR-7986: JDBC Driver for SQL Interface git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1703867 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 2 + .../client/solrj/io/sql/ConnectionImpl.java | 333 ++++++ .../solr/client/solrj/io/sql/DriverImpl.java | 148 +++ .../client/solrj/io/sql/ResultSetImpl.java | 1026 +++++++++++++++++ .../client/solrj/io/sql/StatementImpl.java | 339 ++++++ .../client/solrj/io/sql/package-info.java | 51 + .../META-INF/services/java.sql.Driver | 16 + .../solr/collection1/conf/schema-sql.xml | 599 ++++++++++ .../solr/collection1/conf/solrconfig-sql.xml | 103 ++ .../solr/client/solrj/io/sql/JdbcTest.java | 270 +++++ 10 files changed, 2887 insertions(+) create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java create mode 100644 solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/package-info.java create mode 100644 solr/solrj/src/resources/META-INF/services/java.sql.Driver create mode 100644 solr/solrj/src/test-files/solrj/solr/collection1/conf/schema-sql.xml create mode 100644 solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig-sql.xml create mode 100644 solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 1cdf4d7807a..0195ae99bbe 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -67,6 +67,8 @@ New Features * SOLR-7903: Add the FacetStream to the Streaming API and wire it into the SQLHandler (Joel Bernstein) +* SOLR-7986: JDBC Driver for SQL Interface (Uwe Schindler, Joel Bernstein) + Optimizations ---------------------- * SOLR-7876: Speed up queries and operations that use many terms when timeAllowed has not been diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java new file mode 100644 index 00000000000..9a8574ca0ce --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ConnectionImpl.java @@ -0,0 +1,333 @@ +package org.apache.solr.client.solrj.io.sql; + +/* + * 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. + */ + +import java.sql.Array; +import java.sql.Blob; +import java.sql.CallableStatement; +import java.sql.Clob; +import java.sql.Connection; +import java.sql.DatabaseMetaData; +import java.sql.NClob; +import java.sql.PreparedStatement; +import java.sql.SQLClientInfoException; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Savepoint; +import java.sql.Statement; +import java.sql.Struct; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.io.SolrClientCache; + +class ConnectionImpl implements Connection { + + private SolrClientCache sqlSolrClientCache = new SolrClientCache(); + private CloudSolrClient client; + private String collection; + Properties props; + private boolean closed; + + ConnectionImpl(String zkHost, String collection, Properties props) { + this.client = sqlSolrClientCache.getCloudSolrClient(zkHost); + this.collection = collection; + this.props = props; + } + + @Override + public Statement createStatement() throws SQLException { + return new StatementImpl(client, this.collection, props, sqlSolrClientCache); + } + + @Override + public PreparedStatement prepareStatement(String sql) throws SQLException { + return null; + } + + @Override + public CallableStatement prepareCall(String sql) throws SQLException { + return null; + } + + @Override + public String nativeSQL(String sql) throws SQLException { + return null; + } + + @Override + public void setAutoCommit(boolean autoCommit) throws SQLException { + + } + + @Override + public boolean getAutoCommit() throws SQLException { + return false; + } + + @Override + public void commit() throws SQLException { + + } + + @Override + public void rollback() throws SQLException { + + } + + @Override + public void close() throws SQLException { + if(closed) { + return; + } + try { + this.sqlSolrClientCache.close(); + this.closed = true; + } catch (Exception e) { + throw new SQLException(e); + } + } + + @Override + public boolean isClosed() throws SQLException { + return closed; + } + + @Override + public DatabaseMetaData getMetaData() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setReadOnly(boolean readOnly) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isReadOnly() throws SQLException { + return true; + } + + @Override + public void setCatalog(String catalog) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getCatalog() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setTransactionIsolation(int level) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getTransactionIsolation() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void clearWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Map> getTypeMap() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setTypeMap(Map> map) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setHoldability(int holdability) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getHoldability() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Savepoint setSavepoint() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Savepoint setSavepoint(String name) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void rollback(Savepoint savepoint) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void releaseSavepoint(Savepoint savepoint) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Clob createClob() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Blob createBlob() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public NClob createNClob() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLXML createSQLXML() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isValid(int timeout) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setClientInfo(String name, String value) throws SQLClientInfoException { + throw new UnsupportedOperationException(); + } + + @Override + public void setClientInfo(Properties properties) throws SQLClientInfoException { + throw new UnsupportedOperationException(); + } + + @Override + public String getClientInfo(String name) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Properties getClientInfo() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Array createArrayOf(String typeName, Object[] elements) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Struct createStruct(String typeName, Object[] attributes) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setSchema(String schema) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getSchema() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void abort(Executor executor) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setNetworkTimeout(Executor executor, int milliseconds) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getNetworkTimeout() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public T unwrap(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java new file mode 100644 index 00000000000..5ca4e686dbd --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/DriverImpl.java @@ -0,0 +1,148 @@ +package org.apache.solr.client.solrj.io.sql; + +/* + * 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. + */ + + +import java.net.URLDecoder; +import java.sql.Connection; +import java.sql.Driver; +import java.sql.DriverManager; +import java.sql.DriverPropertyInfo; +import java.sql.SQLException; +import java.util.Properties; +import java.util.logging.Logger; +import org.apache.solr.common.util.SuppressForbidden; + +/** + * Get a Connection with with a url and properties. + * + * jdbc:solr://zkhost:port?collection=collection&aggregationMode=map_reduce + **/ + + +public class DriverImpl implements Driver { + + static { + try { + DriverManager.registerDriver(new DriverImpl()); + } catch (SQLException e) { + throw new RuntimeException("Can't register driver!", e); + } + } + + public Connection connect(String url, Properties props) throws SQLException { + if(!acceptsURL(url)) { + return null; + } + + StringBuilder buf = new StringBuilder(url); + boolean needsAmp = true; + if(!url.contains("?")) { + buf.append("?"); + needsAmp = false; + } + + for(Object key : props.keySet()) { + Object value = props.get(key); + if(needsAmp) { + buf.append("&"); + } + buf.append(key.toString()).append("=").append(value); + needsAmp = true; + } + + return connect(buf.toString()); + } + + public Connection connect(String url) throws SQLException { + + if(!acceptsURL(url)) { + return null; + } + + String[] parts = url.split("://", 0); + + if(parts.length < 2) { + throw new SQLException("The zkHost must start with ://"); + } + + String zkUrl = parts[1]; + String[] zkUrlParts = zkUrl.split("\\?"); + + if(zkUrlParts.length < 2) { + throw new SQLException("The connection url has no connection properties. At a mininum the collection must be specified."); + } + + String connectionProps = zkUrlParts[1]; + String zkHost = zkUrlParts[0]; + Properties props = new Properties(); + loadParams(connectionProps, props); + String collection = (String)props.remove("collection"); + + if(!props.containsKey("aggregationMode")) { + props.setProperty("aggregationMode","facet"); + } + + return new ConnectionImpl(zkHost, collection, props); + } + + public int getMajorVersion() { + return 1; + } + + public int getMinorVersion() { + return 0; + } + + public boolean acceptsURL(String url) { + if(url.startsWith("jdbc:solr")) { + return true; + } else { + return false; + } + } + + public boolean jdbcCompliant() { + return false; + } + + + @SuppressForbidden(reason="Required by jdbc") + + public Logger getParentLogger() { + return null; + } + + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { + return null; + } + + private void loadParams(String params, Properties props) throws SQLException { + try { + String[] pairs = params.split("&"); + for (String pair : pairs) { + String[] keyValue = pair.split("="); + String key = URLDecoder.decode(keyValue[0], "UTF-8"); + String value = URLDecoder.decode(keyValue[1], "UTF-8"); + props.put(key, value); + } + } catch(Exception e) { + throw new SQLException(e); + } + } +} \ No newline at end of file diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java new file mode 100644 index 00000000000..7e9b226fcb3 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/ResultSetImpl.java @@ -0,0 +1,1026 @@ +package org.apache.solr.client.solrj.io.sql; + +/* + * 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. + */ + +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.net.URL; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.Ref; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Statement; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.Calendar; +import java.util.Map; + +import org.apache.solr.client.solrj.io.stream.SolrStream; +import org.apache.solr.client.solrj.io.Tuple; + +class ResultSetImpl implements ResultSet { + + private SolrStream solrStream; + private Tuple tuple; + private boolean done; + private boolean closed; + + ResultSetImpl(SolrStream solrStream) { + this.solrStream = solrStream; + } + + @Override + public boolean next() throws SQLException { + try { + + if(done) { + return false; + } + + tuple = solrStream.read(); + if(tuple.EOF) { + done = true; + return false; + } else { + return true; + } + } catch (Exception e) { + throw new SQLException(e); + } + } + + @Override + public void close() throws SQLException { + this.done = this.closed = true; + } + + @Override + public boolean wasNull() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getString(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean getBoolean(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public byte getByte(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public short getShort(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getInt(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public long getLong(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public float getFloat(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public double getDouble(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public byte[] getBytes(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Date getDate(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Time getTime(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Timestamp getTimestamp(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getAsciiStream(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getUnicodeStream(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getBinaryStream(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getString(String columnLabel) throws SQLException { + return tuple.getString(columnLabel); + } + + @Override + public boolean getBoolean(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public byte getByte(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public short getShort(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getInt(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public long getLong(String columnLabel) throws SQLException { + return tuple.getLong(columnLabel); + } + + @Override + public float getFloat(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public double getDouble(String columnLabel) throws SQLException { + return tuple.getDouble(columnLabel); + } + + @Override + public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { + return null; + } + + @Override + public byte[] getBytes(String columnLabel) throws SQLException { + return new byte[0]; + } + + @Override + public Date getDate(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Time getTime(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Timestamp getTimestamp(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getAsciiStream(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getUnicodeStream(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public InputStream getBinaryStream(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void clearWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getCursorName() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public ResultSetMetaData getMetaData() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Object getObject(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Object getObject(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int findColumn(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Reader getCharacterStream(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Reader getCharacterStream(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public BigDecimal getBigDecimal(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public BigDecimal getBigDecimal(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isBeforeFirst() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isAfterLast() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isFirst() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLast() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void beforeFirst() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void afterLast() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean first() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean last() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean absolute(int row) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean relative(int rows) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean previous() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getFetchDirection() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setFetchSize(int rows) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getFetchSize() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getType() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getConcurrency() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean rowUpdated() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean rowInserted() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean rowDeleted() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNull(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBoolean(int columnIndex, boolean x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateByte(int columnIndex, byte x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateShort(int columnIndex, short x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateInt(int columnIndex, int x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateLong(int columnIndex, long x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateFloat(int columnIndex, float x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateDouble(int columnIndex, double x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateString(int columnIndex, String x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBytes(int columnIndex, byte[] x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateDate(int columnIndex, Date x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateTime(int columnIndex, Time x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateObject(int columnIndex, Object x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNull(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBoolean(String columnLabel, boolean x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateByte(String columnLabel, byte x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateShort(String columnLabel, short x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateInt(String columnLabel, int x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateLong(String columnLabel, long x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateFloat(String columnLabel, float x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateDouble(String columnLabel, double x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateString(String columnLabel, String x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBytes(String columnLabel, byte[] x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateDate(String columnLabel, Date x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateTime(String columnLabel, Time x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateObject(String columnLabel, Object x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void insertRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void deleteRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void refreshRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void cancelRowUpdates() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void moveToInsertRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void moveToCurrentRow() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Statement getStatement() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Object getObject(int columnIndex, Map> map) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Ref getRef(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Blob getBlob(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Clob getClob(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Array getArray(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Object getObject(String columnLabel, Map> map) throws SQLException { + throw new UnsupportedOperationException(); + + } + + @Override + public Ref getRef(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Blob getBlob(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Clob getClob(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Array getArray(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Date getDate(int columnIndex, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Date getDate(String columnLabel, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Time getTime(int columnIndex, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Time getTime(String columnLabel, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public URL getURL(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public URL getURL(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRef(int columnIndex, Ref x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRef(String columnLabel, Ref x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(int columnIndex, Blob x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(String columnLabel, Blob x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(int columnIndex, Clob x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(String columnLabel, Clob x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateArray(int columnIndex, Array x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateArray(String columnLabel, Array x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public RowId getRowId(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public RowId getRowId(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRowId(int columnIndex, RowId x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateRowId(String columnLabel, RowId x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getHoldability() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isClosed() throws SQLException { + return closed; + } + + @Override + public void updateNString(int columnIndex, String nString) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNString(String columnLabel, String nString) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(int columnIndex, NClob nClob) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(String columnLabel, NClob nClob) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public NClob getNClob(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public NClob getNClob(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLXML getSQLXML(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLXML getSQLXML(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getNString(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public String getNString(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Reader getNCharacterStream(int columnIndex) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Reader getNCharacterStream(String columnLabel) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(int columnIndex, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateClob(String columnLabel, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(int columnIndex, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void updateNClob(String columnLabel, Reader reader) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public T getObject(int columnIndex, Class type) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public T getObject(String columnLabel, Class type) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public T unwrap(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java new file mode 100644 index 00000000000..61c22d4cc98 --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/StatementImpl.java @@ -0,0 +1,339 @@ +package org.apache.solr.client.solrj.io.sql; + +/* + * 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. + */ + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Properties; +import java.util.Random; + +import org.apache.solr.client.solrj.io.stream.SolrStream; +import org.apache.solr.client.solrj.io.SolrClientCache; +import org.apache.solr.client.solrj.impl.CloudSolrClient; +import org.apache.solr.client.solrj.io.stream.StreamContext; +import org.apache.solr.common.cloud.ClusterState; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.cloud.Slice; +import org.apache.solr.common.cloud.ZkCoreNodeProps; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.params.CommonParams; + +class StatementImpl implements Statement { + + private CloudSolrClient client; + private SolrClientCache sqlSolrClientCache; + private String collection; + private Properties properties; + private SolrStream solrStream; + private boolean closed; + + StatementImpl(CloudSolrClient client, String collection, Properties properties, SolrClientCache sqlSolrClientCache) { + this.client = client; + this.collection = collection; + this.properties = properties; + this.sqlSolrClientCache = sqlSolrClientCache; + } + + @Override + public ResultSet executeQuery(String sql) throws SQLException { + + try { + closed = false; // If closed reopen so Statement can be reused. + this.solrStream = constructStream(sql); + StreamContext context = new StreamContext(); + context.setSolrClientCache(sqlSolrClientCache); + this.solrStream.setStreamContext(context); + this.solrStream.open(); + return new ResultSetImpl(this.solrStream); + } catch(Exception e) { + throw new SQLException(e); + } + } + + protected SolrStream constructStream(String sql) throws IOException { + + try { + ZkStateReader zkStateReader = client.getZkStateReader(); + ClusterState clusterState = zkStateReader.getClusterState(); + Collection slices = clusterState.getActiveSlices(this.collection); + + if(slices == null) { + throw new Exception("Collection not found:"+this.collection); + } + + Map params = new HashMap(); + + List shuffler = new ArrayList(); + for(Slice slice : slices) { + Collection replicas = slice.getReplicas(); + for (Replica replica : replicas) { + shuffler.add(replica); + } + } + + Collections.shuffle(shuffler, new Random()); + + params.put(CommonParams.QT, "/sql"); + params.put("sql", sql); + params.putAll(properties); + + Replica rep = shuffler.get(0); + ZkCoreNodeProps zkProps = new ZkCoreNodeProps(rep); + String url = zkProps.getCoreUrl(); + return new SolrStream(url, params); + + } catch (Exception e) { + throw new IOException(e); + } + } + + @Override + public int executeUpdate(String sql) throws SQLException { + return 0; + } + + @Override + public void close() throws SQLException { + + if(closed) { + return; + } + + try { + this.solrStream.close(); + this.closed = true; + } catch (Exception e) { + throw new SQLException(e); + } + } + + @Override + public int getMaxFieldSize() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setMaxFieldSize(int max) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getMaxRows() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setMaxRows(int max) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setEscapeProcessing(boolean enable) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getQueryTimeout() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setQueryTimeout(int seconds) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void cancel() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public SQLWarning getWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void clearWarnings() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setCursorName(String name) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean execute(String sql) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public ResultSet getResultSet() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getUpdateCount() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean getMoreResults() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setFetchDirection(int direction) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getFetchDirection() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void setFetchSize(int rows) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getFetchSize() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getResultSetConcurrency() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getResultSetType() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void addBatch(String sql) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public void clearBatch() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int[] executeBatch() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public Connection getConnection() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean getMoreResults(int current) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public ResultSet getGeneratedKeys() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int executeUpdate(String sql, String[] columnNames) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean execute(String sql, int[] columnIndexes) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean execute(String sql, String[] columnNames) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public int getResultSetHoldability() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isClosed() throws SQLException { + return closed; + } + + @Override + public void setPoolable(boolean poolable) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isPoolable() throws SQLException { + return true; + } + + @Override + public void closeOnCompletion() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isCloseOnCompletion() throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public T unwrap(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isWrapperFor(Class iface) throws SQLException { + throw new UnsupportedOperationException(); + } +} \ No newline at end of file diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/package-info.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/package-info.java new file mode 100644 index 00000000000..cce60be012e --- /dev/null +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/sql/package-info.java @@ -0,0 +1,51 @@ +/* + * 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. + */ + +/** + * JDBC Driver Package + * + * Sample usage + *
+ * Connection con = null;
+ * Statement stmt = null;
+ * ResultSet rs = null;
+ *
+ * try {
+ *  con = DriverManager.getConnection("jdbc:solr://zkHost:port?collection=collection&aggregationMode=map_reduce");
+ *  stmt = con.createStatement();
+ *  rs = stmt.executeQuery("select a, sum(b) from tablex group by a");
+ *  while(rs.next()) {
+ *    String a = rs.getString("a");
+ *    double sumB = rs.getString("sum(b)");
+ *  }
+ * } finally {
+ *  rs.close();
+ *  stmt.close();
+ *  con.close();
+ * }
+ * 
+ * + * Connection properties can also be passed in using a Properties object. + * + * The collection parameter is mandatory and should point to a SolrCloud collection that is configured with the /sql + * request handler. + * + * The aggregationMode parameter is optional. It can be used to switch between Map/Reduce (map_reduce) or the JSON Facet API (facet) for + * group by aggregations. The default is "facet". + **/ + +package org.apache.solr.client.solrj.io.sql; diff --git a/solr/solrj/src/resources/META-INF/services/java.sql.Driver b/solr/solrj/src/resources/META-INF/services/java.sql.Driver new file mode 100644 index 00000000000..49f0ce13ce1 --- /dev/null +++ b/solr/solrj/src/resources/META-INF/services/java.sql.Driver @@ -0,0 +1,16 @@ +# 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. + +org.apache.solr.client.solrj.io.sql.DriverImpl diff --git a/solr/solrj/src/test-files/solrj/solr/collection1/conf/schema-sql.xml b/solr/solrj/src/test-files/solrj/solr/collection1/conf/schema-sql.xml new file mode 100644 index 00000000000..216fa2ce85b --- /dev/null +++ b/solr/solrj/src/test-files/solrj/solr/collection1/conf/schema-sql.xml @@ -0,0 +1,599 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text + id + + + + + + + + + + + + + + + + + + + + + diff --git a/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig-sql.xml b/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig-sql.xml new file mode 100644 index 00000000000..59f30320979 --- /dev/null +++ b/solr/solrj/src/test-files/solrj/solr/collection1/conf/solrconfig-sql.xml @@ -0,0 +1,103 @@ + + + + + + ${tests.luceneMatchVersion:LUCENE_CURRENT} + + ${useCompoundFile:false} + + ${solr.data.dir:} + + + + + ${solr.data.dir:} + + + + + + + true + + + + + + {!xport} + xsort + false + + + + query + + + + + + + + json + false + + + + + + json + false + + + + + + + + + + + + + + + + + *:* + + + all + + server-enabled.txt + + + + + solr + + + + diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java new file mode 100644 index 00000000000..4ee732d2a30 --- /dev/null +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/sql/JdbcTest.java @@ -0,0 +1,270 @@ +package org.apache.solr.client.solrj.io.sql; + +/* + * 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. + */ + +import java.io.File; +import java.sql.Connection; +import java.sql.Statement; +import java.sql.ResultSet; +import java.sql.DriverManager; +import java.util.Properties; + +import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util.LuceneTestCase.Slow; +import org.apache.solr.client.solrj.io.stream.expr.StreamFactory; +import org.apache.solr.cloud.AbstractFullDistribZkTestBase; +import org.apache.solr.cloud.AbstractZkTestCase; +import org.apache.solr.common.SolrInputDocument; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * All base tests will be done with CloudSolrStream. Under the covers CloudSolrStream uses SolrStream so + * SolrStream will get fully exercised through these tests. + * + **/ + +@Slow +@LuceneTestCase.SuppressCodecs({"Lucene3x", "Lucene40","Lucene41","Lucene42","Lucene45"}) +public class JdbcTest extends AbstractFullDistribZkTestBase { + + private static final String SOLR_HOME = getFile("solrj" + File.separator + "solr").getAbsolutePath(); + private StreamFactory streamFactory; + + static { + schemaString = "schema-sql.xml"; + } + + @BeforeClass + public static void beforeSuperClass() { + AbstractZkTestCase.SOLRHOME = new File(SOLR_HOME()); + } + + @AfterClass + public static void afterSuperClass() { + + } + + protected String getCloudSolrConfig() { + return "solrconfig-sql.xml"; + } + + + @Override + public String getSolrHome() { + return SOLR_HOME; + } + + public static String SOLR_HOME() { + return SOLR_HOME; + } + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + // we expect this time of exception as shards go up and down... + //ignoreException(".*"); + //System.setProperty("export.test", "true"); + System.setProperty("numShards", Integer.toString(sliceCount)); + } + + @Override + @After + public void tearDown() throws Exception { + super.tearDown(); + resetExceptionIgnores(); + } + + public JdbcTest() { + super(); + sliceCount = 2; + + + } + + @Test + public void doTest() throws Exception { + + indexr(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1"); + indexr(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2"); + indexr(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3"); + indexr(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4"); + indexr(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5"); + indexr(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6"); + indexr(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7"); + indexr(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8"); + indexr(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9"); + indexr(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10"); + + commit(); + + String zkHost = zkServer.getZkAddress(); + + Properties props = new Properties(); + Connection con = DriverManager.getConnection("jdbc:solr://" + zkHost + "?collection=collection1", props); + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select id, a_i, a_s, a_f from collection1 order by a_i desc limit 2"); + + assert(rs.next()); + assert(rs.getLong("a_i") == 14); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("a_f") == 10); + + assert(rs.next()); + assert(rs.getLong("a_i") == 13); + assert(rs.getString("a_s").equals("hello3")); + assert(rs.getDouble("a_f") == 9); + assert(!rs.next()); + stmt.close(); + + //Test statement reuse + rs = stmt.executeQuery("select id, a_i, a_s, a_f from collection1 order by a_i asc limit 2"); + assert(rs.next()); + assert(rs.getLong("a_i") == 0); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("a_f") == 1); + + assert(rs.next()); + assert(rs.getLong("a_i") == 1); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("a_f") == 5); + assert(!rs.next()); + stmt.close(); + + //Test connection reuse + stmt = con.createStatement(); + rs = stmt.executeQuery("select id, a_i, a_s, a_f from collection1 order by a_i desc limit 2"); + assert(rs.next()); + assert(rs.getLong("a_i") == 14); + assert(rs.next()); + assert(rs.getLong("a_i") == 13); + stmt.close(); + + //Test statement reuse + rs = stmt.executeQuery("select id, a_i, a_s, a_f from collection1 order by a_i asc limit 2"); + assert(rs.next()); + assert(rs.getLong("a_i") == 0); + assert(rs.next()); + assert(rs.getLong("a_i") == 1); + assert(!rs.next()); + stmt.close(); + + //Test simple loop + rs = stmt.executeQuery("select id, a_i, a_s, a_f from collection1 order by a_i asc limit 100"); + int count = 0; + while(rs.next()) { + ++count; + } + + assert(count == 10); + + stmt.close(); + con.close(); + + //Test facet aggregation + + + props = new Properties(); + props.put("aggregationMode", "facet"); + con = DriverManager.getConnection("jdbc:solr://" + zkHost + "?collection=collection1", props); + stmt = con.createStatement(); + rs = stmt.executeQuery("select a_s, sum(a_f) from collection1 group by a_s order by sum(a_f) desc"); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello3")); + assert(rs.getDouble("sum(a_f)") == 26); + + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("sum(a_f)") == 18); + + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello4")); + assert(rs.getDouble("sum(a_f)") == 11); + + stmt.close(); + con.close(); + + + //Test map / reduce aggregation + + props = new Properties(); + props.put("aggregationMode", "map_reduce"); + props.put("numWorkers", "2"); + con = DriverManager.getConnection("jdbc:solr://" + zkHost + "?collection=collection1", props); + stmt = con.createStatement(); + rs = stmt.executeQuery("select a_s, sum(a_f) from collection1 group by a_s order by sum(a_f) desc"); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello3")); + assert(rs.getDouble("sum(a_f)") == 26); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("sum(a_f)") == 18); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello4")); + assert(rs.getDouble("sum(a_f)") == 11); + + stmt.close(); + con.close(); + + //Test params on the url + + con = DriverManager.getConnection("jdbc:solr://" + zkHost + "?collection=collection1&aggregationMode=map_reduce&numWorkers=2"); + + Properties p = ((ConnectionImpl)con).props; + + assert(p.getProperty("aggregationMode").equals("map_reduce")); + assert(p.getProperty("numWorkers").equals("2")); + + stmt = con.createStatement(); + rs = stmt.executeQuery("select a_s, sum(a_f) from collection1 group by a_s order by sum(a_f) desc"); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello3")); + assert(rs.getDouble("sum(a_f)") == 26); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello0")); + assert(rs.getDouble("sum(a_f)") == 18); + + assert(rs.next()); + assert(rs.getString("a_s").equals("hello4")); + assert(rs.getDouble("sum(a_f)") == 11); + + stmt.close(); + con.close(); + + del("*:*"); + commit(); + } + + @Override + protected void indexr(Object... fields) throws Exception { + SolrInputDocument doc = getDoc(fields); + indexDoc(doc); + } +}