jdbc.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.jdbc.JdbcDriver`.
  38. Note the driver implements the JDBC 4.0 +Service Provider+ mechanism meaning it is registerd automatically
  39. as long as its 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> Parameters for the JDBC driver. Empty by default. Optional.
  50. The driver recognized the following parameters:
  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.cert.allow.self.signed` (default `false`):: Whether or not to allow self signed certificates
  85. `ssl.protocol`(default `TLS`):: SSL protocol to be used
  86. [float]
  87. ==== Proxy
  88. `proxy.http`:: Http proxy host name
  89. `proxy.socks`:: SOCKS proxy host name
  90. To put all of it together, the following URL:
  91. ["source","text"]
  92. ----
  93. jdbc:es://http://server:3456/timezone=UTC&page.size=250
  94. ----
  95. Opens up a {es-sql} connection to `server` on port `3456`, setting the JDBC connection timezone to `UTC` and its pagesize to `250` entries.
  96. === API usage
  97. One can use JDBC through the official `java.sql` and `javax.sql` packages:
  98. ==== `java.sql`
  99. The former through `java.sql.Driver` and `DriverManager`:
  100. ["source","java",subs="attributes,callouts,macros"]
  101. --------------------------------------------------
  102. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-dm]
  103. --------------------------------------------------
  104. <1> The server and port on which Elasticsearch is listening for
  105. HTTP traffic. The port is by default 9200.
  106. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  107. instance is fine for unsecured Elasticsearch.
  108. ==== `javax.sql`
  109. Accessible through the `javax.sql.DataSource` API:
  110. ["source","java",subs="attributes,callouts,macros"]
  111. --------------------------------------------------
  112. include-tagged::{jdbc-tests}/JdbcIntegrationTestCase.java[connect-ds]
  113. --------------------------------------------------
  114. <1> The server and port on which Elasticsearch is listening for
  115. HTTP traffic. By default 9200.
  116. <2> Properties for connecting to Elasticsearch. An empty `Properties`
  117. instance is fine for unsecured Elasticsearch.
  118. Which one to use? Typically client applications that provide most
  119. configuration parameters in the URL rely on the `DriverManager`-style
  120. while `DataSource` is preferred when being _passed_ around since it can be
  121. configured in one place and the consumer only has to call `getConnection`
  122. without having to worry about any other parameters.
  123. To connect to a secured Elasticsearch server the `Properties`
  124. should look like:
  125. ["source","java",subs="attributes,callouts,macros"]
  126. --------------------------------------------------
  127. include-tagged::{security-tests}/JdbcSecurityIT.java[admin_properties]
  128. --------------------------------------------------
  129. Once you have the connection you can use it like any other JDBC
  130. connection. For example:
  131. ["source","java",subs="attributes,callouts,macros"]
  132. --------------------------------------------------
  133. include-tagged::{jdbc-tests}/SimpleExampleTestCase.java[simple_example]
  134. --------------------------------------------------