2018-06-22 18:40:25 -04:00
[role="xpack"]
[testenv="platinum"]
2017-12-13 10:19:31 -05:00
[[sql-jdbc]]
== SQL JDBC
2018-09-08 09:23:43 -04:00
{es}'s SQL jdbc driver is a rich, fully featured JDBC driver for {es}.
2018-02-02 11:06:35 -05:00
It is Type 4 driver, meaning it is a platform independent, stand-alone, Direct to Database,
2018-09-08 09:23:43 -04:00
pure Java driver that converts JDBC calls to {es-sql}.
2017-12-20 13:58:32 -05:00
2018-09-08 09:23:43 -04:00
[[sql-jdbc-installation]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
=== Installation
2017-12-20 13:58:32 -05:00
2018-09-08 09:23:43 -04:00
The JDBC driver can be obtained from:
Dedicated page::
https://www.elastic.co/downloads/jdbc-client[elastic.co] provides links, typically for manual downloads.
Maven dependency::
http://maven.apache.org/[Maven]-compatible tools can retrieve it automatically as a dependency:
2018-06-16 17:14:59 -04:00
["source","xml",subs="attributes"]
----
<dependency>
2018-06-20 14:42:15 -04:00
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>x-pack-sql-jdbc</artifactId>
2018-06-16 17:40:01 -04:00
<version>{version}</version>
2018-06-16 17:14:59 -04:00
</dependency>
----
from `artifacts.elastic.co/maven` by adding it to the repositories list:
["source","xml",subs="attributes"]
----
<repositories>
<repository>
<id>elastic.co</id>
<url>https://artifacts.elastic.co/maven</url>
</repository>
</repositories>
----
[[jdbc-setup]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
=== Setup
2018-11-28 05:48:26 -05:00
The driver main class is `org.elasticsearch.xpack.sql.jdbc.EsDriver`.
2018-10-03 13:11:39 -04:00
Note the driver implements the JDBC 4.0 +Service Provider+ mechanism meaning it is registered automatically
2018-11-28 05:48:26 -05:00
as long as it is available in the classpath.
2018-06-16 17:14:59 -04:00
2018-06-20 14:40:13 -04:00
Once registered, the driver understands the following syntax as an URL:
2018-06-16 17:14:59 -04:00
["source","text",subs="attributes"]
----
2020-07-03 11:03:00 -04:00
jdbc:[es|elasticsearch]://[[http|https]://]?[host[:port]]?/[prefix]?[\?[option=value]&]*
2018-06-16 17:14:59 -04:00
----
2020-07-03 11:03:00 -04:00
`jdbc:[es|elasticsearch]://`:: Prefix. Mandatory.
2018-06-16 17:14:59 -04:00
2019-04-22 09:07:53 -04:00
`[[http|https]://]`:: Type of HTTP connection to make. Possible values are
`http` (default) or `https`. Optional.
`[host[:port]]`:: Host (`localhost` by default) and port (`9200` by default).
Optional.
`[prefix]`:: Prefix (empty by default). Typically used when hosting {es} under
a certain path. Optional.
`[option=value]`:: Properties for the JDBC driver. Empty by default.
Optional.
2018-06-16 17:14:59 -04:00
2019-02-20 04:23:47 -05:00
The driver recognized the following properties:
2018-06-16 17:14:59 -04:00
[[jdbc-cfg]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
===== Essential
2020-05-05 06:08:39 -04:00
[[jdbc-cfg-timezone]]
2018-06-16 17:14:59 -04:00
`timezone` (default JVM timezone)::
Timezone used by the driver _per connection_ indicated by its `ID`.
*Highly* recommended to set it (to, say, `UTC`) as the JVM timezone can vary, is global for the entire JVM and can't be changed easily when running under a security manager.
[[jdbc-cfg-network]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
===== Network
`connect.timeout` (default 30s)::
Connection timeout (in seconds). That is the maximum amount of time waiting to make a connection to the server.
`network.timeout` (default 60s)::
Network timeout (in seconds). That is the maximum amount of time waiting for the network.
`page.timeout` (default 45s)::
Page timeout (in seconds). That is the maximum amount of time waiting for a page.
`page.size` (default 1000)::
Page size (in entries). The number of results returned per page by the server.
`query.timeout` (default 90s)::
Query timeout (in seconds). That is the maximum amount of time waiting for a query to return.
[[jdbc-cfg-auth]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
==== Basic Authentication
`user`:: Basic Authentication user name
`password`:: Basic Authentication password
[[jdbc-cfg-ssl]]
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
==== SSL
`ssl` (default false):: Enable SSL
`ssl.keystore.location`:: key store (if used) location
`ssl.keystore.pass`:: key store password
`ssl.keystore.type` (default `JKS`):: key store type. `PKCS12` is a common, alternative format
`ssl.truststore.location`:: trust store location
`ssl.truststore.pass`:: trust store password
2019-04-01 10:34:54 -04:00
`ssl.truststore.type` (default `JKS`):: trust store type. `PKCS12` is a common, alternative format
2018-06-16 17:14:59 -04:00
`ssl.protocol`(default `TLS`):: SSL protocol to be used
2020-07-23 12:42:33 -04:00
[discrete]
2018-06-16 17:14:59 -04:00
==== Proxy
`proxy.http`:: Http proxy host name
`proxy.socks`:: SOCKS proxy host name
2020-07-23 12:42:33 -04:00
[discrete]
2019-03-18 08:56:00 -04:00
==== Mapping
2019-03-27 11:18:14 -04:00
`field.multi.value.leniency` (default `true`):: Whether to be lenient and return the first value (without any guarantees of what that
will be - typically the first in natural ascending order) for fields with multiple values (true) or throw an exception.
2019-03-18 08:56:00 -04:00
2020-07-23 12:42:33 -04:00
[discrete]
2019-05-10 07:19:26 -04:00
==== Index
`index.include.frozen` (default `false`):: Whether to include <<frozen-indices, frozen-indices>> in the query execution or not (default).
2020-07-23 12:42:33 -04:00
[discrete]
2019-02-20 04:23:47 -05:00
==== Additional
`validate.properties` (default true):: If disabled, it will ignore any misspellings or unrecognizable properties. When enabled, an exception
will be thrown if the provided property cannot be recognized.
2018-06-16 17:14:59 -04:00
To put all of it together, the following URL:
2018-06-20 14:40:13 -04:00
["source","text"]
2018-06-16 17:14:59 -04:00
----
2018-11-14 11:27:18 -05:00
jdbc:es://http://server:3456/?timezone=UTC&page.size=250
2018-06-16 17:14:59 -04:00
----
2020-07-03 11:03:00 -04:00
opens up a {es-sql} connection to `server` on port `3456`, setting the JDBC connection timezone to `UTC` and its pagesize to `250` entries.
2018-06-16 17:14:59 -04:00
=== API usage
One can use JDBC through the official `java.sql` and `javax.sql` packages:
2019-04-30 10:19:09 -04:00
[[java-sql]]
2018-06-16 17:14:59 -04:00
==== `java.sql`
The former through `java.sql.Driver` and `DriverManager`:
2017-12-13 10:19:31 -05:00
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-dm]
--------------------------------------------------
<1> The server and port on which Elasticsearch is listening for
2018-02-02 09:58:39 -05:00
HTTP traffic. The port is by default 9200.
2017-12-13 10:19:31 -05:00
<2> Properties for connecting to Elasticsearch. An empty `Properties`
instance is fine for unsecured Elasticsearch.
2019-04-30 10:19:09 -04:00
[[javax-sql]]
2018-06-16 17:14:59 -04:00
==== `javax.sql`
Accessible through the `javax.sql.DataSource` API:
2017-12-13 10:19:31 -05:00
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-ds]
--------------------------------------------------
<1> The server and port on which Elasticsearch is listening for
2018-02-02 09:58:39 -05:00
HTTP traffic. By default 9200.
2017-12-13 10:19:31 -05:00
<2> Properties for connecting to Elasticsearch. An empty `Properties`
instance is fine for unsecured Elasticsearch.
Which one to use? Typically client applications that provide most
2019-02-20 04:23:47 -05:00
configuration properties in the URL rely on the `DriverManager`-style
2017-12-13 10:19:31 -05:00
while `DataSource` is preferred when being _passed_ around since it can be
configured in one place and the consumer only has to call `getConnection`
2019-02-20 04:23:47 -05:00
without having to worry about any other properties.
2017-12-13 10:19:31 -05:00
To connect to a secured Elasticsearch server the `Properties`
should look like:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{security-tests}/JdbcSecurityIT.java[admin_properties]
--------------------------------------------------
Once you have the connection you can use it like any other JDBC
connection. For example:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{jdbc-tests}/SimpleExampleTestCase.java[simple_example]
2018-10-03 13:11:39 -04:00
--------------------------------------------------
2019-06-11 05:02:46 -04:00
2020-02-14 11:58:45 -05:00
[NOTE]
{es-sql} doesn't provide a connection pooling mechanism, thus the connections
2019-06-11 05:02:46 -04:00
the JDBC driver creates are not pooled. In order to achieve pooled connections,
a third-party connection pooling mechanism is required. Configuring and setting up the
2020-05-05 06:08:39 -04:00
third-party provider is outside the scope of this documentation.