jdbc.asciidoc 5.9 KB

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