-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1693 from ClickHouse/feat_exec_api
[client-v2] Added executCommand API
- Loading branch information
Showing
4 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
client-v2/src/main/java/com/clickhouse/client/api/command/CommandResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.clickhouse.client.api.command; | ||
|
||
import com.clickhouse.client.api.ClientException; | ||
import com.clickhouse.client.api.metrics.OperationMetrics; | ||
import com.clickhouse.client.api.metrics.ServerMetrics; | ||
import com.clickhouse.client.api.query.QueryResponse; | ||
|
||
public class CommandResponse{ | ||
|
||
private final QueryResponse response; | ||
|
||
public CommandResponse(QueryResponse response) { | ||
this.response = response; | ||
try { | ||
response.close(); | ||
} catch (Exception e) { | ||
throw new ClientException("Failed to close underlying resource", e); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the metrics of this operation. | ||
* | ||
* @return metrics of this operation | ||
*/ | ||
public OperationMetrics getMetrics() { | ||
return response.getMetrics(); | ||
} | ||
|
||
/** | ||
* Alias for {@link ServerMetrics#NUM_ROWS_READ} | ||
* | ||
* @return number of rows read by server from the storage | ||
*/ | ||
public long getReadRows() { | ||
return response.getReadRows(); | ||
} | ||
|
||
/** | ||
* Alias for {@link ServerMetrics#NUM_BYTES_READ} | ||
* | ||
* @return number of bytes read by server from the storage | ||
*/ | ||
public long getReadBytes() { | ||
return response.getReadBytes(); | ||
} | ||
|
||
/** | ||
* Alias for {@link ServerMetrics#NUM_ROWS_WRITTEN} | ||
* | ||
* @return number of rows written by server to the storage | ||
*/ | ||
public long getWrittenRows() { | ||
return response.getWrittenRows(); | ||
} | ||
|
||
/** | ||
* Alias for {@link ServerMetrics#NUM_BYTES_WRITTEN} | ||
* | ||
* @return number of bytes written by server to the storage | ||
*/ | ||
public long getWrittenBytes() { | ||
return response.getWrittenBytes(); | ||
} | ||
|
||
/** | ||
* Alias for {@link ServerMetrics#ELAPSED_TIME} | ||
* | ||
* @return elapsed time in nanoseconds | ||
*/ | ||
public long getServerTime() { | ||
return response.getServerTime(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
client-v2/src/main/java/com/clickhouse/client/api/command/CommandSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.clickhouse.client.api.command; | ||
|
||
import com.clickhouse.client.api.query.QuerySettings; | ||
|
||
public class CommandSettings extends QuerySettings { | ||
} |
56 changes: 56 additions & 0 deletions
56
client-v2/src/test/java/com/clickhouse/client/command/CommandTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.clickhouse.client.command; | ||
|
||
import com.clickhouse.client.BaseIntegrationTest; | ||
import com.clickhouse.client.ClickHouseNode; | ||
import com.clickhouse.client.ClickHouseProtocol; | ||
import com.clickhouse.client.api.Client; | ||
import com.clickhouse.client.api.ClientException; | ||
import com.clickhouse.client.api.command.CommandResponse; | ||
import com.clickhouse.client.api.enums.Protocol; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CommandTests extends BaseIntegrationTest { | ||
|
||
private Client client; | ||
|
||
@BeforeMethod(groups = {"integration"}) | ||
public void setUp() { | ||
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP); | ||
client = new Client.Builder() | ||
.addEndpoint(Protocol.HTTP, node.getHost(), node.getPort(), false) | ||
.setUsername("default") | ||
.setPassword("") | ||
.build(); | ||
|
||
System.out.println("Real port: " + node.getPort()); | ||
} | ||
|
||
|
||
@Test(groups = {"integration"}) | ||
public void testCreateTable() throws Exception { | ||
client.execute("DROP TABLE IF EXISTS test_table").get(10, TimeUnit.SECONDS); | ||
CommandResponse response = | ||
client.execute("CREATE TABLE IF NOT EXISTS test_table (id UInt32, name String) ENGINE = Memory") | ||
.get(10, TimeUnit.SECONDS); | ||
|
||
Assert.assertNotNull(response); | ||
} | ||
|
||
@Test(groups = {"integration"}) | ||
public void testInvalidCommandExecution() throws Exception { | ||
CommandResponse response = client.execute("ALTER TABLE non_existing_table ADD COLUMN id2 UInt32") | ||
.exceptionally(e -> { | ||
|
||
if (!(e.getCause() instanceof ClientException)) { | ||
Assert.fail("Cause should be a ClientException"); | ||
} | ||
return null; | ||
}).get(10, TimeUnit.SECONDS); | ||
|
||
Assert.assertNull(response); | ||
} | ||
} |