jdbc.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. [role="xpack"]
  2. [testenv="platinum"]
  3. [[sql-jdbc]]
  4. == SQL JDBC
  5. Elasticsearch's SQL jdbc driver is a rich, fully featured JDBC driver for Elasticsearch.
  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 Elasticsearch SQL.
  8. [float]
  9. === Installation
  10. The JDBC driver can be obtained either by downloading it from the https://www.elastic.co/downloads/jdbc-client[elastic.co] site or by using a http://maven.apache.org/[Maven]-compatible tool with the following dependency:
  11. ["source","xml",subs="attributes"]
  12. ----
  13. <dependency>
  14. <groupId>org.elasticsearch.plugin</groupId>
  15. <artifactId>x-pack-sql-jdbc</artifactId>
  16. <version>{version}</version>
  17. </dependency>
  18. ----
  19. from `artifacts.elastic.co/maven` by adding it to the repositories list:
  20. ["source","xml",subs="attributes"]
  21. ----
  22. <repositories>
  23. <repository>
  24. <id>elastic.co</id>
  25. <url>https://artifacts.elastic.co/maven</url>
  26. </repository>
  27. </repositories>
  28. ----
  29. [[jdbc-setup]]
  30. [float]
  31. === Setup
  32. The driver main class is `org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcDriver`.
  33. Note the driver implements the JDBC 4.0 +Service Provider+ mechanism meaning it is registerd automatically
  34. as long as its available in the classpath.
  35. Once registered, the driver understands the following syntax as an URL:
  36. ["source","text",subs="attributes"]
  37. ----
  38. jdbc:es://<1>[http|https]?<2>[host[:port]]*<3>/[prefix]*<4>[?[option=value]&<5>]*
  39. ----
  40. <1> `jdbc:es://` prefix. Mandatory.
  41. <2> type of HTTP connection to make - `http` (default) or `https`. Optional.
  42. <3> host (`localhost` by default) and port (`9200` by default). Optional.
  43. <4> prefix (empty by default). Typically used when hosting {es} under a certain path. Optional.
  44. <5> Parameters for the JDBC driver. Empty by default. Optional.
  45. The driver recognized the following parameters:
  46. [[jdbc-cfg]]
  47. [float]
  48. ===== Essential
  49. `timezone` (default JVM timezone)::
  50. Timezone used by the driver _per connection_ indicated by its `ID`.
  51. *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.
  52. [[jdbc-cfg-network]]
  53. [float]
  54. ===== Network
  55. `connect.timeout` (default 30s)::
  56. Connection timeout (in seconds). That is the maximum amount of time waiting to make a connection to the server.
  57. `network.timeout` (default 60s)::
  58. Network timeout (in seconds). That is the maximum amount of time waiting for the network.
  59. `page.timeout` (default 45s)::
  60. Page timeout (in seconds). That is the maximum amount of time waiting for a page.
  61. `page.size` (default 1000)::
  62. Page size (in entries). The number of results returned per page by the server.
  63. `query.timeout` (default 90s)::
  64. Query timeout (in seconds). That is the maximum amount of time waiting for a query to return.
  65. [[jdbc-cfg-auth]]
  66. [float]
  67. ==== Basic Authentication
  68. `user`:: Basic Authentication user name
  69. `password`:: Basic Authentication password
  70. [[jdbc-cfg-ssl]]
  71. [float]
  72. ==== SSL
  73. `ssl` (default false):: Enable SSL
  74. `ssl.keystore.location`:: key store (if used) location
  75. `ssl.keystore.pass`:: key store password
  76. `ssl.keystore.type` (default `JKS`):: key store type. `PKCS12` is a common, alternative format
  77. `ssl.truststore.location`:: trust store location
  78. `ssl.truststore.pass`:: trust store password
  79. `ssl.cert.allow.self.signed` (default `false`):: Whether or not to allow self signed certificates
  80. `ssl.protocol`(default `TLS`):: SSL protocol to be used
  81. [float]
  82. ==== Proxy
  83. `proxy.http`:: Http proxy host name
  84. `proxy.socks`:: SOCKS proxy host name
  85. To put all of it together, the following URL:
  86. ["source","text"]
  87. ----
  88. jdbc:es://http://server:3456/timezone=UTC&page.size=250
  89. ----
  90. Opens up a {es-sql} connection to `server` on port `3456`, setting the JDBC connection timezone to `UTC` and its pagesize to `250` entries.
  91. === API usage
  92. One can use JDBC through the official `java.sql` and `javax.sql` packages:
  93. ==== `java.sql`
  94. The former through `java.sql.Driver` and `DriverManager`:
  95. ["source","java",subs="attributes,callouts,macros"]
  96. --------------------------------------------------
  97. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-dm]
  98. --------------------------------------------------
  99. <1> The server and port on which Elasticsearch is listening for
  100. HTTP traffic. The port is by default 9200.
  101. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  102. instance is fine for unsecured Elasticsearch.
  103. ==== `javax.sql`
  104. Accessible through the `javax.sql.DataSource` API:
  105. ["source","java",subs="attributes,callouts,macros"]
  106. --------------------------------------------------
  107. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-ds]
  108. --------------------------------------------------
  109. <1> The server and port on which Elasticsearch is listening for
  110. HTTP traffic. By default 9200.
  111. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  112. instance is fine for unsecured Elasticsearch.
  113. Which one to use? Typically client applications that provide most
  114. configuration parameters in the URL rely on the `DriverManager`-style
  115. while `DataSource` is preferred when being _passed_ around since it can be
  116. configured in one place and the consumer only has to call `getConnection`
  117. without having to worry about any other parameters.
  118. To connect to a secured Elasticsearch server the `Properties`
  119. should look like:
  120. ["source","java",subs="attributes,callouts,macros"]
  121. --------------------------------------------------
  122. include-tagged::{security-tests}/JdbcSecurityIT.java[admin_properties]
  123. --------------------------------------------------
  124. Once you have the connection you can use it like any other JDBC
  125. connection. For example:
  126. ["source","java",subs="attributes,callouts,macros"]
  127. --------------------------------------------------
  128. include-tagged::{jdbc-tests}/SimpleExampleTestCase.java[simple_example]
  129. --------------------------------------------------