jdbc.asciidoc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. [role="xpack"]
  2. [[sql-jdbc]]
  3. == SQL JDBC
  4. {es}'s SQL jdbc driver is a rich, fully featured JDBC driver for {es}.
  5. It is Type 4 driver, meaning it is a platform independent, stand-alone, Direct to Database,
  6. pure Java driver that converts JDBC calls to {es-sql}.
  7. [[sql-jdbc-installation]]
  8. [discrete]
  9. === Installation
  10. The JDBC driver can be obtained from:
  11. Dedicated page::
  12. https://www.elastic.co/downloads/jdbc-client[elastic.co] provides links, typically for manual downloads.
  13. Maven dependency::
  14. https://maven.apache.org/[Maven]-compatible tools can retrieve it automatically as a dependency:
  15. ["source","xml",subs="attributes"]
  16. ----
  17. <dependency>
  18. <groupId>org.elasticsearch.plugin</groupId>
  19. <artifactId>x-pack-sql-jdbc</artifactId>
  20. <version>{version}</version>
  21. </dependency>
  22. ----
  23. from https://search.maven.org/artifact/org.elasticsearch.plugin/x-pack-sql-jdbc[Maven Central Repository],
  24. or from `artifacts.elastic.co/maven` by adding it to the repositories list:
  25. ["source","xml",subs="attributes"]
  26. ----
  27. <repositories>
  28. <repository>
  29. <id>elastic.co</id>
  30. <url>https://artifacts.elastic.co/maven</url>
  31. </repository>
  32. </repositories>
  33. ----
  34. [[jdbc-compatibility]]
  35. [discrete]
  36. === Version compatibility
  37. include::version-compat.asciidoc[]
  38. [[jdbc-setup]]
  39. [discrete]
  40. === Setup
  41. The driver main class is `org.elasticsearch.xpack.sql.jdbc.EsDriver`.
  42. Note the driver implements the JDBC 4.0 +Service Provider+ mechanism meaning it is registered automatically
  43. as long as it is available in the classpath.
  44. Once registered, the driver understands the following syntax as an URL:
  45. ["source","text",subs="attributes"]
  46. ----
  47. jdbc:[es|elasticsearch]://[[http|https]://]?[host[:port]]?/[prefix]?[\?[option=value]&]*
  48. ----
  49. `jdbc:[es|elasticsearch]://`:: Prefix. Mandatory.
  50. `[[http|https]://]`:: Type of HTTP connection to make. Possible values are
  51. `http` (default) or `https`. Optional.
  52. `[host[:port]]`:: Host (`localhost` by default) and port (`9200` by default).
  53. Optional.
  54. `[prefix]`:: Prefix (empty by default). Typically used when hosting {es} under
  55. a certain path. Optional.
  56. `[option=value]`:: Properties for the JDBC driver. Empty by default.
  57. Optional.
  58. The driver recognized the following properties:
  59. [[jdbc-cfg]]
  60. [discrete]
  61. ===== Essential
  62. [[jdbc-cfg-timezone]]
  63. `timezone` (default JVM timezone)::
  64. Timezone used by the driver _per connection_ indicated by its `ID`.
  65. *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.
  66. [[jdbc-cfg-network]]
  67. [discrete]
  68. ===== Network
  69. `connect.timeout` (default `30000`)::
  70. Connection timeout (in milliseconds). That is the maximum amount of time waiting to make a connection to the server.
  71. `network.timeout` (default `60000`)::
  72. Network timeout (in milliseconds). That is the maximum amount of time waiting for the network.
  73. `page.size` (default `1000`)::
  74. Page size (in entries). The number of results returned per page by the server.
  75. `page.timeout` (default `45000`)::
  76. Page timeout (in milliseconds). Minimum retention period for the scroll cursor on the server. Queries that require
  77. a stateful scroll cursor on the server side might fail after this timeout. Hence, when scrolling through large result sets,
  78. processing `page.size` records should not take longer than `page.timeout` milliseconds.
  79. `query.timeout` (default `90000`)::
  80. Query timeout (in milliseconds). That is the maximum amount of time waiting for a query to return.
  81. [[jdbc-cfg-auth]]
  82. [discrete]
  83. ==== Basic Authentication
  84. `user`:: Basic Authentication user name
  85. `password`:: Basic Authentication password
  86. [[jdbc-cfg-ssl]]
  87. [discrete]
  88. ==== SSL
  89. `ssl` (default `false`):: Enable SSL
  90. `ssl.keystore.location`:: key store (if used) location
  91. `ssl.keystore.pass`:: key store password
  92. `ssl.keystore.type` (default `JKS`):: key store type. `PKCS12` is a common, alternative format
  93. `ssl.truststore.location`:: trust store location
  94. `ssl.truststore.pass`:: trust store password
  95. `ssl.truststore.type` (default `JKS`):: trust store type. `PKCS12` is a common, alternative format
  96. `ssl.protocol`(default `TLS`):: SSL protocol to be used
  97. [discrete]
  98. ==== Proxy
  99. `proxy.http`:: Http proxy host name
  100. `proxy.socks`:: SOCKS proxy host name
  101. [discrete]
  102. ==== Mapping
  103. `field.multi.value.leniency` (default `true`):: Whether to be lenient and return the first value (without any guarantees of what that
  104. will be - typically the first in natural ascending order) for fields with multiple values (true) or throw an exception.
  105. [discrete]
  106. ==== Index
  107. `index.include.frozen` (default `false`):: Whether to include frozen indices in the query execution or not (default).
  108. [discrete]
  109. ==== Cluster
  110. `catalog`:: Default catalog (cluster) for queries. If unspecified, the
  111. queries execute on the data in the local cluster only.
  112. +
  113. experimental:[] See <<modules-cross-cluster-search,{ccs}>>.
  114. [discrete]
  115. ==== Error handling
  116. `allow.partial.search.results` (default `false`):: Whether to return partial results in case of shard failure or fail the query throwing
  117. the underlying exception (default).
  118. [discrete]
  119. ==== Troubleshooting
  120. `debug` (default `false`):: Setting it to `true` will enable the debug logging.
  121. `debug.output` (default `err`):: The destination of the debug logs. By default, they are sent to standard error. Value `out` will redirect the logging to standard output. A file path can also be specified.
  122. [discrete]
  123. ==== Additional
  124. `validate.properties` (default `true`):: If disabled, it will ignore any misspellings or unrecognizable properties. When enabled, an exception
  125. will be thrown if the provided property cannot be recognized.
  126. To put all of it together, the following URL:
  127. ["source","text"]
  128. ----
  129. jdbc:es://http://server:3456/?timezone=UTC&page.size=250
  130. ----
  131. opens up a {es-sql} connection to `server` on port `3456`, setting the JDBC connection timezone to `UTC` and its pagesize to `250` entries.
  132. === API usage
  133. One can use JDBC through the official `java.sql` and `javax.sql` packages:
  134. [[java-sql]]
  135. ==== `java.sql`
  136. The former through `java.sql.Driver` and `DriverManager`:
  137. ["source","java",subs="attributes,callouts,macros"]
  138. --------------------------------------------------
  139. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-dm]
  140. --------------------------------------------------
  141. <1> The server and port on which Elasticsearch is listening for
  142. HTTP traffic. The port is by default 9200.
  143. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  144. instance is fine for unsecured Elasticsearch.
  145. [[javax-sql]]
  146. ==== `javax.sql`
  147. Accessible through the `javax.sql.DataSource` API:
  148. ["source","java",subs="attributes,callouts,macros"]
  149. --------------------------------------------------
  150. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-ds]
  151. --------------------------------------------------
  152. <1> The server and port on which Elasticsearch is listening for
  153. HTTP traffic. By default 9200.
  154. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  155. instance is fine for unsecured Elasticsearch.
  156. Which one to use? Typically client applications that provide most
  157. configuration properties in the URL rely on the `DriverManager`-style
  158. while `DataSource` is preferred when being _passed_ around since it can be
  159. configured in one place and the consumer only has to call `getConnection`
  160. without having to worry about any other properties.
  161. To connect to a secured Elasticsearch server the `Properties`
  162. should look like:
  163. ["source","java",subs="attributes,callouts,macros"]
  164. --------------------------------------------------
  165. include-tagged::{security-tests}/JdbcSecurityIT.java[admin_properties]
  166. --------------------------------------------------
  167. Once you have the connection you can use it like any other JDBC
  168. connection. For example:
  169. ["source","java",subs="attributes,callouts,macros"]
  170. --------------------------------------------------
  171. include-tagged::{jdbc-tests}/SimpleExampleTestCase.java[simple_example]
  172. --------------------------------------------------
  173. [NOTE]
  174. {es-sql} doesn't provide a connection pooling mechanism, thus the connections
  175. the JDBC driver creates are not pooled. In order to achieve pooled connections,
  176. a third-party connection pooling mechanism is required. Configuring and setting up the
  177. third-party provider is outside the scope of this documentation.