jdbc.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. [float]
  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. http://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 `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-setup]]
  35. [float]
  36. === Setup
  37. The driver main class is `org.elasticsearch.xpack.sql.jdbc.EsDriver`.
  38. Note the driver implements the JDBC 4.0 +Service Provider+ mechanism meaning it is registered automatically
  39. as long as it is available in the classpath.
  40. Once registered, the driver understands the following syntax as an URL:
  41. ["source","text",subs="attributes"]
  42. ----
  43. jdbc:es://[[http|https]://]*[host[:port]]*/[prefix]*<[?[option=value]&]*
  44. ----
  45. `jdbc:es://`:: Prefix. Mandatory.
  46. `[[http|https]://]`:: Type of HTTP connection to make. Possible values are
  47. `http` (default) or `https`. Optional.
  48. `[host[:port]]`:: Host (`localhost` by default) and port (`9200` by default).
  49. Optional.
  50. `[prefix]`:: Prefix (empty by default). Typically used when hosting {es} under
  51. a certain path. Optional.
  52. `[option=value]`:: Properties for the JDBC driver. Empty by default.
  53. Optional.
  54. The driver recognized the following properties:
  55. [[jdbc-cfg]]
  56. [float]
  57. ===== Essential
  58. `timezone` (default JVM timezone)::
  59. Timezone used by the driver _per connection_ indicated by its `ID`.
  60. *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.
  61. [[jdbc-cfg-network]]
  62. [float]
  63. ===== Network
  64. `connect.timeout` (default 30s)::
  65. Connection timeout (in seconds). That is the maximum amount of time waiting to make a connection to the server.
  66. `network.timeout` (default 60s)::
  67. Network timeout (in seconds). That is the maximum amount of time waiting for the network.
  68. `page.timeout` (default 45s)::
  69. Page timeout (in seconds). That is the maximum amount of time waiting for a page.
  70. `page.size` (default 1000)::
  71. Page size (in entries). The number of results returned per page by the server.
  72. `query.timeout` (default 90s)::
  73. Query timeout (in seconds). That is the maximum amount of time waiting for a query to return.
  74. [[jdbc-cfg-auth]]
  75. [float]
  76. ==== Basic Authentication
  77. `user`:: Basic Authentication user name
  78. `password`:: Basic Authentication password
  79. [[jdbc-cfg-ssl]]
  80. [float]
  81. ==== SSL
  82. `ssl` (default false):: Enable SSL
  83. `ssl.keystore.location`:: key store (if used) location
  84. `ssl.keystore.pass`:: key store password
  85. `ssl.keystore.type` (default `JKS`):: key store type. `PKCS12` is a common, alternative format
  86. `ssl.truststore.location`:: trust store location
  87. `ssl.truststore.pass`:: trust store password
  88. `ssl.truststore.type` (default `JKS`):: trust store type. `PKCS12` is a common, alternative format
  89. `ssl.protocol`(default `TLS`):: SSL protocol to be used
  90. [float]
  91. ==== Proxy
  92. `proxy.http`:: Http proxy host name
  93. `proxy.socks`:: SOCKS proxy host name
  94. [float]
  95. ==== Mapping
  96. `field.multi.value.leniency` (default `true`):: Whether to be lenient and return the first value (without any guarantees of what that
  97. will be - typically the first in natural ascending order) for fields with multiple values (true) or throw an exception.
  98. [float]
  99. ==== Index
  100. `index.include.frozen` (default `false`):: Whether to include <<frozen-indices, frozen-indices>> in the query execution or not (default).
  101. [float]
  102. ==== Additional
  103. `validate.properties` (default true):: If disabled, it will ignore any misspellings or unrecognizable properties. When enabled, an exception
  104. will be thrown if the provided property cannot be recognized.
  105. To put all of it together, the following URL:
  106. ["source","text"]
  107. ----
  108. jdbc:es://http://server:3456/?timezone=UTC&page.size=250
  109. ----
  110. Opens up a {es-sql} connection to `server` on port `3456`, setting the JDBC connection timezone to `UTC` and its pagesize to `250` entries.
  111. === API usage
  112. One can use JDBC through the official `java.sql` and `javax.sql` packages:
  113. [[java-sql]]
  114. ==== `java.sql`
  115. The former through `java.sql.Driver` and `DriverManager`:
  116. ["source","java",subs="attributes,callouts,macros"]
  117. --------------------------------------------------
  118. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-dm]
  119. --------------------------------------------------
  120. <1> The server and port on which Elasticsearch is listening for
  121. HTTP traffic. The port is by default 9200.
  122. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  123. instance is fine for unsecured Elasticsearch.
  124. [[javax-sql]]
  125. ==== `javax.sql`
  126. Accessible through the `javax.sql.DataSource` API:
  127. ["source","java",subs="attributes,callouts,macros"]
  128. --------------------------------------------------
  129. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-ds]
  130. --------------------------------------------------
  131. <1> The server and port on which Elasticsearch is listening for
  132. HTTP traffic. By default 9200.
  133. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  134. instance is fine for unsecured Elasticsearch.
  135. Which one to use? Typically client applications that provide most
  136. configuration properties in the URL rely on the `DriverManager`-style
  137. while `DataSource` is preferred when being _passed_ around since it can be
  138. configured in one place and the consumer only has to call `getConnection`
  139. without having to worry about any other properties.
  140. To connect to a secured Elasticsearch server the `Properties`
  141. should look like:
  142. ["source","java",subs="attributes,callouts,macros"]
  143. --------------------------------------------------
  144. include-tagged::{security-tests}/JdbcSecurityIT.java[admin_properties]
  145. --------------------------------------------------
  146. Once you have the connection you can use it like any other JDBC
  147. connection. For example:
  148. ["source","java",subs="attributes,callouts,macros"]
  149. --------------------------------------------------
  150. include-tagged::{jdbc-tests}/SimpleExampleTestCase.java[simple_example]
  151. --------------------------------------------------