Java Client

## Node Discovery[​](#v1-node-discovery "Direct link to Node Discovery")

Java client provides the ability to discover ClickHouse nodes automatically. Auto-discovery is disabled by default. To manually enable it, set `auto_discovery` to `true`:

```
properties.setProperty("auto_discovery", "true");
```

Or in the connection URL:

```
jdbc:ch://my-server/system?auto_discovery=true
```

If auto-discovery is enabled, there is no need to specify all ClickHouse nodes in the connection URL. Nodes specified in the URL will be treated as seeds, and the Java client will automatically discover more nodes from system tables and/or clickhouse-keeper or zookeeper.

The following options are responsible for auto-discovery configuration:

|  |  |  |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Property Default Description|  |  |  |  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | auto\_discovery `false` Whether the client should discover more nodes from system tables and/or clickhouse-keeper/zookeeper.|  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | | node\_discovery\_interval `0` Node discovery interval in milliseconds, zero or negative value means one-time discovery.|  |  |  | | --- | --- | --- | | node\_discovery\_limit `100` Maximum number of nodes that can be discovered at a time; zero or negative value means no limit. | | | | | | | | | | | |

### Load Balancing[​](#v1-load-balancing "Direct link to Load Balancing")

The Java client chooses a ClickHouse node to send requests to, according to the load-balancing policy. In general, the load-balancing policy is responsible for the following things:

1. Get a node from a managed node list.
2. Managing node's status.
3. Optionally schedule a background process for node discovery (if auto-discovery is enabled) and run a health check.

Here is a list of options to configure load balancing:

|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Property Default Description|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | load\_balancing\_policy `""` The load-balancing policy can be one of:- `firstAlive` - request is sent to the first healthy node from the managed node list - `random` - request is sent to a random node from the managed node list - `roundRobin` - request is sent to each node from the managed node list, in turn. - full qualified class name implementing `ClickHouseLoadBalancingPolicy` - custom load balancing policy If it isn't specified the request is sent to the first node from the managed node list| load\_balancing\_tags `""` Load balancing tags for filtering out nodes. Requests are sent only to nodes that have the specified tags|  |  |  |  |  |  |  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | health\_check\_interval `0` Health check interval in milliseconds, zero or negative value means one-time.|  |  |  |  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | health\_check\_method `ClickHouseHealthCheckMethod.SELECT_ONE` Health check method. Can be one of:- `ClickHouseHealthCheckMethod.SELECT_ONE` - check with `select 1` query - `ClickHouseHealthCheckMethod.PING` - protocol-specific check, which is generally faster | node\_check\_interval `0` Node check interval in milliseconds, negative number is treated as zero. The node status is checked if the specified amount of time has passed since the last check. The difference between `health_check_interval` and `node_check_interval` is that the `health_check_interval` option schedules the background job, which checks the status for the list of nodes (all or faulty), but `node_check_interval` specifies the amount of time has passed since the last check for the particular node| check\_all\_nodes `false` Whether to perform a health check against all nodes or just faulty ones. | | | | | | | | | | | | | | | | | | | | |

### Failover and retry[​](#v1-failover-and-retry "Direct link to Failover and retry")

Java client provides configuration options to set up failover and retry behavior for failed queries:

|  |  |  |  |  |  |  |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Property Default Description|  |  |  |  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | --- | --- | --- | | failover `0` Maximum number of times a failover can happen for a request. Zero or a negative value means no failover. Failover sends the failed request to a different node (according to the load-balancing policy) in order to recover from failover.|  |  |  |  |  |  | | --- | --- | --- | --- | --- | --- | | retry `0` Maximum number of times retry can happen for a request. Zero or a negative value means no retry. Retry sends a request to the same node and only if the ClickHouse server returns the `NETWORK_ERROR` error code| repeat\_on\_session\_lock `true` Whether to repeat execution when the session is locked until timed out(according to `session_timeout` or `connect_timeout`). The failed request is repeated if the ClickHouse server returns the `SESSION_IS_LOCKED` error code | | | | | | | | | | | |

### Adding custom http headers[​](#v1-adding-custom-http-headers "Direct link to Adding custom http headers")

Java client support HTTP/S transport layer in case we want to add custom HTTP headers to the request.
We should use the custom\_http\_headers property, and the headers need to be `,` separated. The header key/value should be divided using `=`

## Java Client support[​](#v1-java-client-support "Direct link to Java Client support")

```
options.put("custom_http_headers", "X-ClickHouse-Quota=test, X-ClickHouse-Test=test");
```

## JDBC Driver[​](#v1-jdbc-driver "Direct link to JDBC Driver")

```
properties.setProperty("custom_http_headers", "X-ClickHouse-Quota=test, X-ClickHouse-Test=test");
```