|
| 1 | +# Custom URL Path Configuration |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The custom URL path feature allows you to route requests to different database instances behind a load balancer by configuring a custom path to be appended to the base endpoint URL. |
| 6 | + |
| 7 | +## Use Case |
| 8 | + |
| 9 | +When multiple ClickHouse database instances are behind a load balancer, you may need to route requests to different databases based on URL paths. For example: |
| 10 | + |
| 11 | +- `https://myhost.com:8123/sales/db` → Routes to sales analytics DB |
| 12 | +- `https://myhost.com:8123/app/db` → Routes to app stats DB |
| 13 | + |
| 14 | +## Configuration |
| 15 | + |
| 16 | +### Using the Builder API |
| 17 | + |
| 18 | +```java |
| 19 | +import com.clickhouse.client.api.Client; |
| 20 | + |
| 21 | +// Configure client with custom URL path |
| 22 | +Client client = new Client.Builder() |
| 23 | + .addEndpoint("https://myhost.com:8123") |
| 24 | + .customURLPath("/sales/db") |
| 25 | + .setUsername("default") |
| 26 | + .setPassword("password") |
| 27 | + .setDefaultDatabase("my_database") |
| 28 | + .build(); |
| 29 | +``` |
| 30 | + |
| 31 | +### Using Configuration Properties |
| 32 | + |
| 33 | +```java |
| 34 | +import com.clickhouse.client.api.Client; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +Map<String, String> config = new HashMap<>(); |
| 39 | +config.put("custom_url_path", "/sales/db"); |
| 40 | +config.put("user", "default"); |
| 41 | +config.put("password", "password"); |
| 42 | +config.put("database", "my_database"); |
| 43 | + |
| 44 | +Client client = new Client.Builder() |
| 45 | + .addEndpoint("https://myhost.com:8123") |
| 46 | + .setOptions(config) |
| 47 | + .build(); |
| 48 | +``` |
| 49 | + |
| 50 | +## Important Notes |
| 51 | + |
| 52 | +1. **Database Name Header**: The database name is sent via the `X-ClickHouse-Database` header, not as part of the URL path. This ensures proper database routing even when custom paths are used. |
| 53 | + |
| 54 | +2. **Path Format**: The custom path is appended to the base URL as-is. It's recommended to start with "/" for clarity (e.g., "/sales/db"). |
| 55 | + |
| 56 | +3. **Existing Paths**: If your base endpoint URL already contains a path (e.g., "https://myhost.com:8123/api"), the custom path will be concatenated (e.g., "https://myhost.com:8123/api/sales/db"). |
| 57 | + |
| 58 | +## Example Scenarios |
| 59 | + |
| 60 | +### Scenario 1: Simple Custom Path |
| 61 | + |
| 62 | +```java |
| 63 | +// Base URL: https://myhost.com:8123 |
| 64 | +// Custom Path: /sales/db |
| 65 | +// Result: https://myhost.com:8123/sales/db |
| 66 | + |
| 67 | +Client client = new Client.Builder() |
| 68 | + .addEndpoint("https://myhost.com:8123") |
| 69 | + .customURLPath("/sales/db") |
| 70 | + .setUsername("default") |
| 71 | + .setPassword("password") |
| 72 | + .build(); |
| 73 | +``` |
| 74 | + |
| 75 | +### Scenario 2: Combining with Existing Path |
| 76 | + |
| 77 | +```java |
| 78 | +// Base URL: https://myhost.com:8123/api |
| 79 | +// Custom Path: /sales/db |
| 80 | +// Result: https://myhost.com:8123/api/sales/db |
| 81 | + |
| 82 | +Client client = new Client.Builder() |
| 83 | + .addEndpoint("https://myhost.com:8123/api") |
| 84 | + .customURLPath("/sales/db") |
| 85 | + .setUsername("default") |
| 86 | + .setPassword("password") |
| 87 | + .build(); |
| 88 | +``` |
| 89 | + |
| 90 | +### Scenario 3: Multiple Clients for Different Databases |
| 91 | + |
| 92 | +```java |
| 93 | +// Client for sales database |
| 94 | +Client salesClient = new Client.Builder() |
| 95 | + .addEndpoint("https://myhost.com:8123") |
| 96 | + .customURLPath("/sales/db") |
| 97 | + .setUsername("default") |
| 98 | + .setPassword("password") |
| 99 | + .setDefaultDatabase("sales") |
| 100 | + .build(); |
| 101 | + |
| 102 | +// Client for app stats database |
| 103 | +Client appClient = new Client.Builder() |
| 104 | + .addEndpoint("https://myhost.com:8123") |
| 105 | + .customURLPath("/app/db") |
| 106 | + .setUsername("default") |
| 107 | + .setPassword("password") |
| 108 | + .setDefaultDatabase("app_stats") |
| 109 | + .build(); |
| 110 | +``` |
| 111 | + |
| 112 | +## Load Balancer Configuration |
| 113 | + |
| 114 | +When using this feature, ensure your load balancer is configured to route based on URL paths. For example, with nginx: |
| 115 | + |
| 116 | +```nginx |
| 117 | +location /sales/db { |
| 118 | + proxy_pass http://sales-clickhouse-backend; |
| 119 | +} |
| 120 | +
|
| 121 | +location /app/db { |
| 122 | + proxy_pass http://app-clickhouse-backend; |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Compatibility |
| 127 | + |
| 128 | +- Available in: client-v2 API |
| 129 | +- Minimum version: 0.9.4-SNAPSHOT |
| 130 | +- Server compatibility: All ClickHouse versions (server-side configuration required for routing) |
0 commit comments