jdbc.asciidoc 7.2 KB

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