jdbc.asciidoc 6.2 KB

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