Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New java client api #1574

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ default boolean ping(ClickHouseNode server, int timeout) {
return resp != null;
} catch (Exception e) {
// ignore
e.printStackTrace();
}
}

Expand Down
171 changes: 171 additions & 0 deletions client-v2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-java</artifactId>
<version>${revision}</version>
</parent>

<artifactId>client-v2</artifactId>
<packaging>jar</packaging>

<name>ClickHouse Client API</name>
<description>New client api for ClickHouse</description>
<url>https://github.com/ClickHouse/clickhouse-java/tree/main/clickhouse-client-api</url>

<dependencies>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-client</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>clickhouse-http-client</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5-h2</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>com.github.luben</groupId>
<artifactId>zstd-jni</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-pure-java</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<optional>true</optional>
</dependency>
<!-- necessary for Java 9+ -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- <dependency>-->
<!-- <groupId>${project.parent.groupId}</groupId>-->
<!-- <artifactId>clickhouse-client</artifactId>-->
<!-- <version>${revision}</version>-->
<!-- <type>test-jar</type>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>toxiproxy</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<createDependencyReducedPom>true</createDependencyReducedPom>
<createSourcesJar>true</createSourcesJar>
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<artifactSet>
<includes>
<include>com.clickhouse:clickhouse-data</include>
<include>com.clickhouse:clickhouse-client</include>
<include>org.lz4:lz4-pure-java</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>net.jpountz</pattern>
<shadedPattern>${shade.base}.jpountz</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Automatic-Module-Name>com.clickhouse.client.http</Automatic-Module-Name>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>**/module-info.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<executions>
<execution>
<id>flatten</id>
<phase>package</phase>
<goals>
<goal>flatten</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
95 changes: 95 additions & 0 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.clickhouse.client.api;

import com.clickhouse.client.ClickHouseClient;
import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseProtocol;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Client {
public static final int TIMEOUT = 30_000;
private Set<String> endpoints;
private Map<String, String> configuration;
private List<ClickHouseNode> serverNodes = new ArrayList<>();
private Client(Set<String> endpoints, Map<String,String> configuration) {
this.endpoints = endpoints;
this.configuration = configuration;
this.endpoints.forEach(endpoint -> {
this.serverNodes.add(ClickHouseNode.of(endpoint, this.configuration));
});
}

public static class Builder {
private Set<String> endpoints;
private Map<String, String> configuration;

public Builder() {
this.endpoints = new HashSet<>();
this.configuration = new HashMap<String, String>();
}

public Builder addEndpoint(String endpoint) {
// TODO: validate endpoint
this.endpoints.add(endpoint);
return this;
}

public Builder addEndpoint(Protocol protocol, String host, int port) {
String endpoint = String.format("%s://%s:%d", protocol.toString().toLowerCase(), host, port);
this.addEndpoint(endpoint);
return this;
}

public Builder addConfiguration(String key, String value) {
this.configuration.put(key, value);
return this;
}

public Builder addUsername(String username) {
this.configuration.put("user", username);
return this;
}

public Builder addPassword(String password) {
this.configuration.put("password", password);
return this;
}

public Client build() {
// check if endpoint are empty. so can not initiate client
if (this.endpoints.isEmpty()) {
throw new IllegalArgumentException("At least one endpoint is required");
}
// check if username and password are empty. so can not initiate client?
return new Client(this.endpoints, this.configuration);
}
}

private ClickHouseNode getServerNode() {
// TODO: implement load balancing using existing logic
return this.serverNodes.get(0);
}

/**
* Ping the server to check if it is alive
* @return true if the server is alive, false otherwise
*/
public boolean ping() {
return ping(Client.TIMEOUT);
}

/**
* Ping the server to check if it is alive
* @param timeout timeout in milliseconds
* @return true if the server is alive, false otherwise
*/
public boolean ping(int timeout) {
ClickHouseClient clientPing = ClickHouseClient.newInstance(ClickHouseProtocol.HTTP);
return clientPing.ping(getServerNode(), timeout);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.clickhouse.client.api;

public enum Protocol {
HTTP,
HTTPS,
//TCP,
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<module>clickhouse-cli-client</module>
<module>clickhouse-grpc-client</module>
<module>clickhouse-http-client</module>
<module>client-v2</module>
<!-- driver -->
<module>clickhouse-jdbc</module>
<module>clickhouse-r2dbc</module>
Expand Down
Loading