Package com.clickhouse.client
Class ClickHouseRequest<SelfT extends ClickHouseRequest<SelfT>>
- java.lang.Object
-
- com.clickhouse.client.ClickHouseRequest<SelfT>
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
ClickHouseRequest.Mutation
public class ClickHouseRequest<SelfT extends ClickHouseRequest<SelfT>> extends Object implements Serializable
Request object holding references toClickHouseClient
,ClickHouseNode
, format, sql, options and settings etc. for execution.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ClickHouseRequest.Mutation
Mutation request.
-
Field Summary
Fields Modifier and Type Field Description protected ClickHouseConfig
clientConfig
protected ClickHouseConfig
config
protected List<ClickHouseExternalTable>
externalTables
protected CompletableFuture<InputStream>
input
protected Map<String,String>
namedParameters
protected Map<ClickHouseOption,Serializable>
options
protected ClickHouseParameterizedQuery
preparedQuery
protected String
queryId
protected Function<ClickHouseNodeSelector,ClickHouseNode>
server
protected String
sessionId
protected Map<String,Serializable>
settings
protected String
sql
protected List<String>
statements
-
Constructor Summary
Constructors Modifier Constructor Description protected
ClickHouseRequest(ClickHouseClient client, Function<ClickHouseNodeSelector,ClickHouseNode> server, boolean sealed)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SelfT
addExternal(ClickHouseExternalTable table)
Adds an external table.protected void
checkSealed()
SelfT
clearSession()
Clears session configuration including session id, whether to validate the id and session timeout.SelfT
compressServerResponse(boolean enable)
Enable or disable compression of server response.SelfT
compressServerResponse(boolean enable, ClickHouseCompression compressAlgorithm)
Enable or disable compression of server response.SelfT
compressServerResponse(boolean enable, ClickHouseCompression compressAlgorithm, int compressLevel)
Enable or disable compression of server response.ClickHouseRequest<SelfT>
copy()
Creates a copy of this request object.SelfT
decompressClientRequest(boolean enable)
Enable or disable compression of client request.SelfT
decompressClientRequest(boolean enable, ClickHouseCompression compressAlgorithm)
Enable or disable compression of client request.SelfT
decompressClientRequest(boolean enable, ClickHouseCompression compressAlgorithm, int compressLevel)
Enable or disable compression of client request.CompletableFuture<ClickHouseResponse>
execute()
Executes the request.SelfT
external(ClickHouseExternalTable table, ClickHouseExternalTable... more)
Sets one or more external tables.SelfT
external(Collection<ClickHouseExternalTable> tables)
Sets external tables.SelfT
format(ClickHouseFormat format)
Sets format to be used for communication between server and client.protected ClickHouseClient
getClient()
ClickHouseConfig
getConfig()
Gets request configuration.List<ClickHouseExternalTable>
getExternalTables()
Gets immutable list of external tables.ClickHouseFormat
getFormat()
Gets data format used for communication between server and client.Optional<InputStream>
getInputStream()
Gets input stream.ClickHouseParameterizedQuery
getPreparedQuery()
Gets prepared query, which is a loosely parsed query with the origianl query and list of parameters.protected String
getQuery()
Gets query, either set byquery()
ortable()
.Optional<String>
getQueryId()
Gets query id.ClickHouseNode
getServer()
Depending on theFunction
passed to the constructor, this method may return different node for each call.Optional<String>
getSessionId()
Gets session id.Map<String,Object>
getSettings()
Gets immutable settings.List<String>
getStatements()
Gets list of SQL statements.List<String>
getStatements(boolean withSettings)
Gets list of SQL statements.boolean
hasInputStream()
Checks if the request contains any input stream.boolean
isSealed()
Checks if the request is sealed(immutable).SelfT
option(ClickHouseOption option, Serializable value)
Sets an option.SelfT
options(Map<ClickHouseOption,Serializable> options)
Sets all options.SelfT
options(Properties options)
Sets all options.SelfT
params(ClickHouseValue[] values)
Sets parameters wrapped byClickHouseValue
.SelfT
params(ClickHouseValue value, ClickHouseValue... more)
Sets parameters wrapped byClickHouseValue
.SelfT
params(Object[] values)
Set raw parameters, which will later be stringified usingClickHouseValues.convertToSqlExpression(Object)
.SelfT
params(Object value, Object... more)
Set raw parameters, which will later be stringified usingClickHouseValues.convertToSqlExpression(Object)
.SelfT
params(String[] values)
Sets stringified parameters which are used to substitude named parameters in SQL query without further transformation and validation.SelfT
params(String value, String... more)
Sets stringified parameters which are used to substitude named parameters in SQL query without further transformation and validation.SelfT
params(Collection<String> values)
Sets stringified parameters.SelfT
params(Map<String,String> namedParams)
Sets named parameters.SelfT
query(ClickHouseParameterizedQuery query)
Sets parameterized query.SelfT
query(ClickHouseParameterizedQuery query, String queryId)
Sets parameterized query and optinally query id.SelfT
query(String sql)
Sets query.SelfT
query(String sql, String queryId)
Sets query and optinally query id.SelfT
removeExternal(ClickHouseExternalTable external)
Removes an external table.SelfT
removeExternal(String name)
Removes an external table by name.SelfT
removeOption(ClickHouseOption option)
Removes an option.SelfT
removeSetting(String setting)
Removes a setting.SelfT
reset()
Resets the request to start all over.protected void
resetCache()
ClickHouseRequest<SelfT>
seal()
Creates a sealed request, which is an immutable copy of the current request.SelfT
session(String sessionId)
Sets current session using custom id.SelfT
session(String sessionId, Boolean check)
Sets session.SelfT
session(String sessionId, Boolean check, Integer timeout)
Sets current session.SelfT
session(String sessionId, Integer timeout)
Sets current session.SelfT
set(String setting, Serializable value)
Sets a setting.SelfT
set(String setting, String value)
Sets a setting.SelfT
table(String table)
Sets target table.SelfT
table(String table, String queryId)
Sets target table and optionally query id.SelfT
use(String database)
Changes current database.ClickHouseRequest.Mutation
write()
Creates a new request for mutation.
-
-
-
Field Detail
-
clientConfig
protected final ClickHouseConfig clientConfig
-
server
protected final Function<ClickHouseNodeSelector,ClickHouseNode> server
-
externalTables
protected final transient List<ClickHouseExternalTable> externalTables
-
options
protected final Map<ClickHouseOption,Serializable> options
-
settings
protected final Map<String,Serializable> settings
-
input
protected transient CompletableFuture<InputStream> input
-
queryId
protected String queryId
-
sessionId
protected String sessionId
-
sql
protected String sql
-
preparedQuery
protected ClickHouseParameterizedQuery preparedQuery
-
config
protected transient ClickHouseConfig config
-
-
Constructor Detail
-
ClickHouseRequest
protected ClickHouseRequest(ClickHouseClient client, Function<ClickHouseNodeSelector,ClickHouseNode> server, boolean sealed)
-
-
Method Detail
-
checkSealed
protected void checkSealed()
-
getClient
protected ClickHouseClient getClient()
-
getQuery
protected String getQuery()
Gets query, either set byquery()
ortable()
.- Returns:
- sql query
-
resetCache
protected void resetCache()
-
copy
public ClickHouseRequest<SelfT> copy()
Creates a copy of this request object.- Returns:
- copy of this request
-
isSealed
public boolean isSealed()
Checks if the request is sealed(immutable).- Returns:
- true if the request is sealed; false otherwise
-
hasInputStream
public boolean hasInputStream()
Checks if the request contains any input stream.- Returns:
- true if there's input stream; false otherwise
-
getServer
public final ClickHouseNode getServer()
Depending on theFunction
passed to the constructor, this method may return different node for each call.- Returns:
- node defined by
Function
-
getConfig
public ClickHouseConfig getConfig()
Gets request configuration.- Returns:
- request configuration
-
getInputStream
public Optional<InputStream> getInputStream()
Gets input stream.- Returns:
- input stream
-
getExternalTables
public List<ClickHouseExternalTable> getExternalTables()
Gets immutable list of external tables.- Returns:
- immutable list of external tables
-
getFormat
public ClickHouseFormat getFormat()
Gets data format used for communication between server and client.- Returns:
- data format used for communication between server and client
-
getPreparedQuery
public ClickHouseParameterizedQuery getPreparedQuery()
Gets prepared query, which is a loosely parsed query with the origianl query and list of parameters.- Returns:
- prepared query
-
getSettings
public Map<String,Object> getSettings()
Gets immutable settings.- Returns:
- immutable settings
-
getStatements
public List<String> getStatements()
Gets list of SQL statements. Same asgetStatements(true)
.- Returns:
- list of SQL statements
-
getStatements
public List<String> getStatements(boolean withSettings)
Gets list of SQL statements.- Parameters:
withSettings
- true to treat settings as SQL statement; false otherwise- Returns:
- list of SQL statements
-
compressServerResponse
public SelfT compressServerResponse(boolean enable)
Enable or disable compression of server response. Pay attention thatClickHouseClientOption.COMPRESS_ALGORITHM
andClickHouseClientOption.COMPRESS_LEVEL
will be used.- Parameters:
enable
- true to enable compression of server response; false otherwise- Returns:
- the request itself
-
compressServerResponse
public SelfT compressServerResponse(boolean enable, ClickHouseCompression compressAlgorithm)
Enable or disable compression of server response. Pay attention thatClickHouseClientOption.COMPRESS_LEVEL
will be used.- Parameters:
enable
- true to enable compression of server response; false otherwisecompressAlgorithm
- compression algorithm, null is treated asClickHouseCompression.NONE
orClickHouseClientOption.COMPRESS_ALGORITHM
depending on whether enabled- Returns:
- the request itself
-
compressServerResponse
public SelfT compressServerResponse(boolean enable, ClickHouseCompression compressAlgorithm, int compressLevel)
Enable or disable compression of server response.- Parameters:
enable
- true to enable compression of server response; false otherwisecompressAlgorithm
- compression algorithm, null is treated asClickHouseCompression.NONE
orClickHouseClientOption.COMPRESS_ALGORITHM
depending on whether enabledcompressLevel
- compression level- Returns:
- the request itself
-
decompressClientRequest
public SelfT decompressClientRequest(boolean enable)
Enable or disable compression of client request. Pay attention thatClickHouseClientOption.DECOMPRESS_ALGORITHM
andClickHouseClientOption.DECOMPRESS_LEVEL
will be used.- Parameters:
enable
- true to enable compression of client request; false otherwise- Returns:
- the request itself
-
decompressClientRequest
public SelfT decompressClientRequest(boolean enable, ClickHouseCompression compressAlgorithm)
Enable or disable compression of client request. Pay attention thatClickHouseClientOption.DECOMPRESS_LEVEL
will be used.- Parameters:
enable
- true to enable compression of client request; false otherwisecompressAlgorithm
- compression algorithm, null is treated asClickHouseCompression.NONE
orClickHouseClientOption.DECOMPRESS_ALGORITHM
depending on whether enabled- Returns:
- the request itself
-
decompressClientRequest
public SelfT decompressClientRequest(boolean enable, ClickHouseCompression compressAlgorithm, int compressLevel)
Enable or disable compression of client request.- Parameters:
enable
- true to enable compression of client request; false otherwisecompressAlgorithm
- compression algorithm, null is treated asClickHouseCompression.NONE
compressLevel
- compression level- Returns:
- the request itself
-
addExternal
public SelfT addExternal(ClickHouseExternalTable table)
Adds an external table.- Parameters:
table
- non-null external table- Returns:
- the request itself
-
external
public SelfT external(ClickHouseExternalTable table, ClickHouseExternalTable... more)
Sets one or more external tables.- Parameters:
table
- non-null external tablemore
- more external tables- Returns:
- the request itself
-
external
public SelfT external(Collection<ClickHouseExternalTable> tables)
Sets external tables.- Parameters:
tables
- non-null external tables- Returns:
- the request itself
-
format
public SelfT format(ClickHouseFormat format)
Sets format to be used for communication between server and client.- Parameters:
format
- non-null format- Returns:
- the request itself
-
option
public SelfT option(ClickHouseOption option, Serializable value)
Sets an option.option
is for configuring client's behaviour, whilesetting
is for server.- Parameters:
option
- optionvalue
- value- Returns:
- the request itself
-
options
public SelfT options(Map<ClickHouseOption,Serializable> options)
Sets all options.option
is for configuring client's behaviour, whilesetting
is for server.- Parameters:
options
- options- Returns:
- the request itself
-
options
public SelfT options(Properties options)
Sets all options.option
is for configuring client's behaviour, whilesetting
is for server.- Parameters:
options
- options- Returns:
- the request itself
-
params
public SelfT params(Collection<String> values)
Sets stringified parameters. Be aware of SQL injection risk as mentioned inparams(String, String...)
.- Parameters:
values
- stringified parameters- Returns:
- the request itself
-
params
public SelfT params(ClickHouseValue value, ClickHouseValue... more)
Sets parameters wrapped byClickHouseValue
. Safer but a bit slower thanparams(String, String...)
. Consider to reuse ClickHouseValue object and its update methods for less overhead in batch processing.- Parameters:
value
- parametermore
- more parameters- Returns:
- the request itself
-
params
public SelfT params(ClickHouseValue[] values)
Sets parameters wrapped byClickHouseValue
. Safer but a bit slower thanparams(String, String...)
. Consider to reuse ClickHouseValue object and its update methods for less overhead in batch processing.- Parameters:
values
- parameters- Returns:
- the request itself
-
params
public SelfT params(String value, String... more)
Sets stringified parameters which are used to substitude named parameters in SQL query without further transformation and validation. Keep in mind that stringified parameter is a SQL expression, meaning it could be a sub-query(SQL injection) in addition to value like number and string.- Parameters:
value
- stringified parametermore
- more stringified parameters- Returns:
- the request itself
-
params
public SelfT params(String[] values)
Sets stringified parameters which are used to substitude named parameters in SQL query without further transformation and validation. Keep in mind that stringified parameter is a SQL expression, meaning it could be a sub-query(SQL injection) in addition to value like number and string.- Parameters:
values
- stringified parameters- Returns:
- the request itself
-
params
public SelfT params(Object value, Object... more)
Set raw parameters, which will later be stringified usingClickHouseValues.convertToSqlExpression(Object)
. Although it is convenient to use, it's NOT recommended in most cases except for a few parameters and/or testing.- Parameters:
value
- raw parametermore
- more raw parameters- Returns:
- the request itself
-
params
public SelfT params(Object[] values)
Set raw parameters, which will later be stringified usingClickHouseValues.convertToSqlExpression(Object)
. Although it is convenient to use, it's NOT recommended in most cases except for a few parameters and/or testing.- Parameters:
values
- raw parameters- Returns:
- the request itself
-
params
public SelfT params(Map<String,String> namedParams)
Sets named parameters. Be aware of SQL injection risk as mentioned inparams(String, String...)
.- Parameters:
namedParams
- named parameters- Returns:
- the request itself
-
query
public SelfT query(ClickHouseParameterizedQuery query)
Sets parameterized query. Same asquery(query, null)
.- Parameters:
query
- non-null parameterized query- Returns:
- the request itself
-
query
public SelfT query(String sql)
Sets query. Same asquery(sql, null)
.- Parameters:
sql
- non-empty query- Returns:
- the request itself
-
query
public SelfT query(ClickHouseParameterizedQuery query, String queryId)
Sets parameterized query and optinally query id.- Parameters:
query
- non-null parameterized queryqueryId
- query id, null means no query id- Returns:
- the request itself
-
query
public SelfT query(String sql, String queryId)
Sets query and optinally query id.- Parameters:
sql
- non-empty queryqueryId
- query id, null means no query id- Returns:
- the request itself
-
clearSession
public SelfT clearSession()
Clears session configuration including session id, whether to validate the id and session timeout.- Returns:
- the request itself
-
session
public SelfT session(String sessionId)
Sets current session using custom id. Same assession(sessionId, null, null)
.- Parameters:
sessionId
- session id, null means no session- Returns:
- the request itself
-
session
public SelfT session(String sessionId, Boolean check)
Sets session. Same assession(sessionId, check, null)
.- Parameters:
sessionId
- session id, null means no sessioncheck
- whether the server should check if the session id exists or not- Returns:
- the request itself
-
session
public SelfT session(String sessionId, Integer timeout)
Sets current session. Same assession(sessionId, null, timeout)
.- Parameters:
sessionId
- session id, null means no sessiontimeout
- timeout in milliseconds- Returns:
- the request itself
-
session
public SelfT session(String sessionId, Boolean check, Integer timeout)
Sets current session.- Parameters:
sessionId
- session id, null means no sessioncheck
- whether the server should check if the session id exists or nottimeout
- timeout in milliseconds- Returns:
- the request itself
-
set
public SelfT set(String setting, Serializable value)
Sets a setting. See https://clickhouse.tech/docs/en/operations/settings/settings/ for more information.- Parameters:
setting
- non-empty setting to setvalue
- value of the setting- Returns:
- the request itself
-
set
public SelfT set(String setting, String value)
Sets a setting. See https://clickhouse.tech/docs/en/operations/settings/settings/ for more information.- Parameters:
setting
- non-empty setting to setvalue
- value of the setting- Returns:
- the request itself
-
table
public SelfT table(String table)
Sets target table. Same astable(table, null)
.- Parameters:
table
- non-empty table name- Returns:
- the request itself
-
table
public SelfT table(String table, String queryId)
Sets target table and optionally query id. This will generate a query likeSELECT * FROM [table]
and override the one set byquery(String, String)
.- Parameters:
table
- non-empty table namequeryId
- query id, null means no query id- Returns:
- the request itself
-
use
public SelfT use(String database)
Changes current database.- Parameters:
database
- non-empty database name- Returns:
- the request itself
-
removeExternal
public SelfT removeExternal(ClickHouseExternalTable external)
Removes an external table.- Parameters:
external
- non-null external table- Returns:
- the request itself
-
removeExternal
public SelfT removeExternal(String name)
Removes an external table by name.- Parameters:
name
- non-empty external table name- Returns:
- the request itself
-
removeOption
public SelfT removeOption(ClickHouseOption option)
Removes an option.- Parameters:
option
- option to be removed- Returns:
- the request itself
-
removeSetting
public SelfT removeSetting(String setting)
Removes a setting.- Parameters:
setting
- name of the setting- Returns:
- the request itself
-
reset
public SelfT reset()
Resets the request to start all over.- Returns:
- the request itself
-
seal
public ClickHouseRequest<SelfT> seal()
Creates a sealed request, which is an immutable copy of the current request.- Returns:
- sealed request, an immutable copy of the current request
-
write
public ClickHouseRequest.Mutation write()
Creates a new request for mutation.- Returns:
- request for mutation
-
execute
public CompletableFuture<ClickHouseResponse> execute()
Executes the request. Same asclient.execute(request.seal())
.- Returns:
- future to get response
- Throws:
CompletionException
- when error occurred during execution
-
-