blob: 55f37de7414a315db7b144e494615c6becaa9806 [file] [log] [blame]
= Solr JDBC - Python/Jython
// 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.
Solr's JDBC driver supports Python and Jython.
== Python
Python supports accessing JDBC using the https://pypi.python.org/pypi/JayDeBeApi/[JayDeBeApi] library. The CLASSPATH variable must be configured to contain the solr-solrj jar and the supporting solrj-lib jars.
=== JayDeBeApi
.run.sh
[source,bash]
----
#!/usr/bin/env bash
# Java must already be installed
pip install JayDeBeApi
export CLASSPATH="$(echo $(ls /opt/solr/dist/solr-solrj* /opt/solr/dist/solrj-lib/*) | tr ' ' ':')"
python solr_jaydebeapi.py
----
.solr_jaydebeapi.py
[source,python]
----
#!/usr/bin/env python
# https://pypi.python.org/pypi/JayDeBeApi/
import jaydebeapi
import sys
if __name__ == '__main__':
jdbc_url = "jdbc:solr://localhost:9983?collection=test"
driverName = "org.apache.solr.client.solrj.io.sql.DriverImpl"
statement = "select fielda, fieldb, fieldc, fieldd_s, fielde_i from test limit 10"
conn = jaydebeapi.connect(driverName, jdbc_url)
curs = conn.cursor()
curs.execute(statement)
print(curs.fetchall())
conn.close()
sys.exit(0)
----
== Jython
Jython supports accessing JDBC natively with Java interfaces or with the zxJDBC library. The CLASSPATH variable must be configured to contain the solr-solrj jar and the supporting solrj-lib jars.
.run.sh
[source,bash]
----
#!/usr/bin/env bash
# Java and Jython must already be installed
export CLASSPATH="$(echo $(ls /opt/solr/dist/solr-solrj* /opt/solr/dist/solrj-lib/*) | tr ' ' ':')"
jython [solr_java_native.py | solr_zxjdbc.py]
----
=== Java Native
.solr_java_native.py
[source,python]
----
#!/usr/bin/env jython
# http://www.jython.org/jythonbook/en/1.0/DatabasesAndJython.html
# https://wiki.python.org/jython/DatabaseExamples#SQLite_using_JDBC
import sys
from java.lang import Class
from java.sql import DriverManager, SQLException
if __name__ == '__main__':
jdbc_url = "jdbc:solr://localhost:9983?collection=test"
driverName = "org.apache.solr.client.solrj.io.sql.DriverImpl"
statement = "select fielda, fieldb, fieldc, fieldd_s, fielde_i from test limit 10"
dbConn = DriverManager.getConnection(jdbc_url)
stmt = dbConn.createStatement()
resultSet = stmt.executeQuery(statement)
while resultSet.next():
print(resultSet.getString("fielda"))
resultSet.close()
stmt.close()
dbConn.close()
sys.exit(0)
----
=== zxJDBC
.solr_zxjdbc.py
[source,python]
----
#!/usr/bin/env jython
# http://www.jython.org/jythonbook/en/1.0/DatabasesAndJython.html
# https://wiki.python.org/jython/DatabaseExamples#SQLite_using_ziclix
import sys
from com.ziclix.python.sql import zxJDBC
if __name__ == '__main__':
jdbc_url = "jdbc:solr://localhost:9983?collection=test"
driverName = "org.apache.solr.client.solrj.io.sql.DriverImpl"
statement = "select fielda, fieldb, fieldc, fieldd_s, fielde_i from test limit 10"
with zxJDBC.connect(jdbc_url, None, None, driverName) as conn:
with conn:
with conn.cursor() as c:
c.execute(statement)
print(c.fetchall())
sys.exit(0)
----