This document provides a high-level overview of td-client-java, a Java client library for interacting with the Treasure Data REST API. The library enables Java applications to programmatically submit queries, manage databases and tables, execute jobs, import/export data, and monitor job execution on the Treasure Data platform.
This page covers the library's purpose, architecture, and main capabilities. For specific implementation details, refer to:
Sources: README.md1-14 pom.xml1-10
td-client-java is the official Java client for Treasure Data's cloud-based data platform. It abstracts the complexity of REST API calls into a type-safe, fluent Java API, handling authentication, retries, error handling, and response parsing automatically.
Primary Use Cases:
Sources: README.md3-10 Example.java56-346
| Requirement | Specification |
|---|---|
| Java Version | 1.8 or higher |
| Build System | Maven 3.x |
| License | Apache License 2.0 |
| Current Version | 1.2.0 |
| Maven Coordinates | com.treasuredata.client:td-client |
The library is available as both a regular JAR with transitive dependencies and a standalone "shade" JAR with all dependencies bundled under a relocated package namespace to prevent conflicts.
Sources: pom.xml1-60 README.md12-13 LICENSE.txt1-203
The library follows a layered architecture with clear separation of concerns:
Architecture Description
The architecture consists of four primary layers:
Public API Layer: TDClient serves as the main entry point, providing ~30 high-level methods. TDClientBuilder handles configuration from multiple sources (environment variables, system properties, td.conf file, programmatic settings).
HTTP Communication Layer: TDHttpClient manages all HTTP operations, wrapping OkHttp3. It delegates request preparation to TDHttpRequestHandler, error categorization to TDRequestErrorHandler, and uses pluggable BackOff strategies for retry logic.
Infrastructure Layer: Immutable configuration objects (TDClientConfig, ProxyConfig) ensure thread-safety. Retry strategies implement exponential backoff with jitter to prevent thundering herd problems.
Dependencies: OkHttp3 handles HTTP communication with connection pooling and timeout management. Jackson handles JSON serialization/deserialization with custom annotations and type adapters.
Sources: TDClient.java1-100 TDClientBuilder.java1-51 pom.xml98-173
| Class | Package | Role |
|---|---|---|
TDClient | com.treasuredata.client | Main API facade; provides all public methods for database, table, job, and query operations |
TDClientBuilder | com.treasuredata.client | Builder for configuring and constructing TDClient instances |
TDHttpClient | com.treasuredata.client | Internal HTTP client; manages request/response lifecycle, retries, and error handling |
TDClientConfig | com.treasuredata.client | Immutable configuration object containing all client settings |
TDApiRequest | com.treasuredata.client | Represents an HTTP API request with method, path, parameters, and body |
TDHttpRequestHandler | com.treasuredata.client | Interface for customizing request preparation and response processing |
TDRequestErrorHandler | com.treasuredata.client | Categorizes HTTP errors into retryable/non-retryable and resolves appropriate exceptions |
BackOff | com.treasuredata.client | Interface for retry strategies; implementations include ExponentialBackOff, FullJitterBackOff |
Sources: TDClient.java TDClientBuilder.java1-51
This diagram shows how key code entities interact during a typical API request:
Key Interactions:
TDClient methods (e.g., listTables(), submit(), jobStatus())TDClient creates TDApiRequest objects with method, path, and parametersTDHttpClient.submitRequest() executes the request using OkHttpClientTDHttpRequestHandler allows pre-request modifications (headers, authentication)onSuccess() parses JSON responses into model objects using JacksonTDRequestErrorHandler categorizes errors and determines retry behaviorBackOff strategies calculate wait times between retriesSources: TDClient.java TDHttpClient.java TDHttpRequestHandler.java
| Operation | Method | Description |
|---|---|---|
| Create Database | createDatabase(name) | Creates a new database |
| List Databases | listDatabases() | Retrieves all databases |
| Show Database | showDatabase(name) | Gets detailed database info |
| Delete Database | deleteDatabase(name) | Removes a database |
| Create Table | createTable(db, table) | Creates a new table |
| List Tables | listTables(db) | Lists tables in a database |
| Show Table | showTable(db, table) | Gets table metadata |
| Delete Table | deleteTable(db, table) | Removes a table |
| Update Schema | updateTableSchema(db, table, columns) | Modifies table schema |
Sources: TDClient.java Example.java99-209
| Operation | Method | Return Type |
|---|---|---|
| Submit Query | submit(TDJobRequest) | String (jobId) |
| Check Status | jobStatus(jobId) | TDJobSummary |
| Get Job Info | jobInfo(jobId) | TDJob |
| Get Results | jobResult(jobId, format, handler) | <T> T |
| Kill Job | kill(jobId) | TDJob |
Job Types Supported:
TDJobRequest.newPrestoQuery()TDJobRequest.newTrinoQuery()TDJobRequest.newHiveQuery()TDJobRequest.newPigQuery()TDJobRequest.newBulkLoad()Sources: Example.java121-160 TDJobRequest.java
The bulk import workflow involves creating a session, uploading data parts, performing the import, and committing:
Key Methods:
createBulkImportSession(name, database, table): Initialize sessionuploadBulkImportPart(session, partName, file): Upload msgpack.gz dataperformBulkImportSession(session): Start import jobcommitBulkImportSession(session): Finalize importSources: Example.java271-296 TDClient.java
| Operation | Method | Description |
|---|---|---|
| Save Query | saveQuery(request) | Register a new scheduled query |
| List Queries | listSavedQueries() | Get all saved queries |
| Start Query | startSavedQuery(name, time) | Execute a saved query |
| Update Query | updateSavedQuery(name, request) | Modify saved query |
| Delete Query | deleteSavedQuery(name) | Remove saved query |
| Get History | getSavedQueryHistory(name) | Retrieve execution history |
Sources: Example.java298-345 TDSavedQuery.java
Configuration is loaded from multiple sources with the following precedence (highest to lowest):
TDClientBuilder.setProperties()$HOME/.td/td.conf file-D flags)TD_API_KEY only)| Parameter | Default | Description |
|---|---|---|
apikey | - | API key for authentication (or use TD_API_KEY env var) |
td.client.endpoint | api.treasuredata.com | API endpoint hostname |
td.client.port | 443 (SSL), 80 (non-SSL) | API port |
td.client.usessl | true | Enable SSL/TLS |
td.client.retry.limit | 7 | Maximum retry attempts |
td.client.retry.max-interval | 60000 | Max backoff interval (ms) |
td.client.connect-timeout | 15000 | Connection timeout (ms) |
td.client.read-timeout | 60000 | Read timeout (ms) |
td.client.proxy.host | - | Proxy hostname |
td.client.proxy.port | - | Proxy port |
Configuration Example:
Sources: README.md58-229 TDClientBuilder.java1-51 AbstractTDClientBuilder.java
The library provides strongly-typed model classes for all Treasure Data entities:
All model classes are immutable and use the Immutables library for type-safe builders. Jackson annotations handle JSON serialization. See Data Models for detailed documentation.
Sources: pom.xml162-173 TDDatabase.java TDTable.java TDJob.java
The library implements sophisticated error handling with automatic retries:
TDClientException: Base exception for all client errors
TDClientHttpException: HTTP-related errors (includes status code)
TDClientHttpUnauthorizedException: 401 errorsTDClientHttpNotFoundException: 404 errorsTDClientHttpConflictException: 409 errorsTDClientHttpTooManyRequestsException: 429 rate limiting| Error Type | Status Codes | Retryable | Notes |
|---|---|---|---|
| Server Errors | 5xx | Yes | Always retried with backoff |
| Rate Limiting | 429 | Yes | Respects Retry-After header |
| Network Errors | - | Yes | Socket/timeout exceptions |
| Authorization | 401 | No | Invalid credentials |
| Not Found | 404 | Context-dependent | Retried for read ops, not for create ops |
| Invalid Input | 422 | No | Client error, no retry |
Sources: TDClientException.java TDRequestErrorHandler.java BackOff.java
The library uses minimal external dependencies with shading to prevent conflicts:
| Dependency | Version | Purpose | Shaded |
|---|---|---|---|
| OkHttp3 | 3.14.9 | HTTP client | Yes |
| Jackson Core | 2.14.2 | JSON processing | No |
| Jackson Databind | 2.14.2 | JSON binding | No |
| Guava | 31.1-jre | Utilities | Yes |
| SLF4J API | 2.0.12 | Logging facade | No |
| Immutables | 2.10.1 | Code generation (compile-only) | No |
Shaded Packages (in td-client-(version)-shade.jar):
com.google.* → com.treasuredata.client.thirdparty.com.google.*okhttp3.* → com.treasuredata.client.thirdparty.okhttp3.*okio.* → com.treasuredata.client.thirdparty.okio.*Sources: pom.xml98-322
For complete examples including bulk import and saved queries, see Example.java65-346
Sources: README.md94-151 Example.java65-346
TDClient methodsSources: README.md1-260
Refresh this wiki
This wiki was recently refreshed. Please wait 5 days to refresh again.