public class Metadata extends Object
Modifier and Type | Method and Description |
---|---|
boolean |
checkSchemaAgreement()
Checks whether hosts that are currently up agree on the schema definition.
|
String |
exportSchemaAsString()
Returns a
String containing CQL queries representing the schema of this cluster. |
Set<Host> |
getAllHosts()
Returns the known hosts of this cluster.
|
String |
getClusterName()
The Cassandra name for the cluster connect to.
|
KeyspaceMetadata |
getKeyspace(String keyspace)
Returns the metadata of a keyspace given its name.
|
List<KeyspaceMetadata> |
getKeyspaces()
Returns a list of all the defined keyspaces.
|
String |
getPartitioner()
The partitioner in use as reported by the Cassandra nodes.
|
Set<Host> |
getReplicas(String keyspace,
com.datastax.driver.core.Token.Factory partitioner,
ByteBuffer partitionKey)
Returns the set of hosts that are replica for a given partition key.
|
Set<Host> |
getReplicas(String keyspace,
TokenRange range)
Returns the set of hosts that are replica for a given token range.
|
Set<TokenRange> |
getTokenRanges()
Returns the token ranges that define data distribution in the ring.
|
Set<TokenRange> |
getTokenRanges(String keyspace,
Host host)
Returns the token ranges that are replicated on the given host, for the given keyspace.
|
static boolean |
isReservedCqlKeyword(String id)
Checks whether an identifier is a known reserved CQL keyword or not.
|
Token |
newToken(String tokenStr)
Builds a new
Token from its string representation, according to the partitioner
reported by the Cassandra nodes. |
Token |
newToken(com.datastax.driver.core.Token.Factory partitioner,
ByteBuffer... components)
Builds a new
Token from a partition key. |
TokenRange |
newTokenRange(Token start,
Token end)
Builds a new
TokenRange . |
TupleType |
newTupleType(DataType... types)
Creates a tuple type given a list of types.
|
TupleType |
newTupleType(List<DataType> types)
Creates a tuple type given a list of types.
|
static String |
quote(String id)
Quote a keyspace, table or column identifier to make it case sensitive.
|
static String |
quoteIfNecessary(String id)
Quotes a CQL identifier if necessary.
|
public static String quoteIfNecessary(String id)
This is similar to quote(String)
, except that it won't quote the input string if it
can safely be used as-is. For example:
quoteIfNecessary("foo").equals("foo")
(no need to quote).
quoteIfNecessary("Foo").equals("\"Foo\"")
(identifier is mixed case so case
sensitivity is required)
quoteIfNecessary("foo bar").equals("\"foo bar\"")
(identifier contains special
characters)
quoteIfNecessary("table").equals("\"table\"")
(identifier is a reserved CQL
keyword)
id
- the "internal" form of the identifier. That is, the identifier as it would appear in
Cassandra system tables (such as system_schema.tables
, system_schema.columns
, etc.)getKeyspace(String)
.public static String quote(String id)
CQL identifiers, including keyspace, table and column ones, are case insensitive by default. Case sensitive identifiers can however be provided by enclosing the identifier in double quotes (see the CQL documentation for details). If you are using case sensitive identifiers, this method can be used to enclose such identifiers in double quotes, making them case sensitive.
Note that reserved CQL
keywords should also be quoted. You can check if a given identifier is a reserved keyword
by calling isReservedCqlKeyword(String)
.
id
- the keyspace or table identifier.id
enclosed in double-quotes, for use in methods like getReplicas(java.lang.String, com.datastax.driver.core.Token.Factory, java.nio.ByteBuffer)
,
getKeyspace(java.lang.String)
, KeyspaceMetadata.getTable(java.lang.String)
or even Cluster.connect(String)
.public static boolean isReservedCqlKeyword(String id)
The check is case-insensitive, i.e., the word "KeYsPaCe
" would be considered as a
reserved CQL keyword just as "keyspace
".
Note: The list of reserved CQL keywords is subject to change in future versions of Cassandra. As a consequence, this method is provided solely as a convenience utility and should not be considered as an authoritative source of truth for checking reserved CQL keywords.
id
- the identifier to check; should not be null
.true
if the given identifier is a known reserved CQL keyword, false
otherwise.public Set<TokenRange> getTokenRanges()
Note that this information is refreshed asynchronously by the control connection, when schema or ring topology changes. It might occasionally be stale.
QueryOptions.setMetadataEnabled(boolean)
.public Set<TokenRange> getTokenRanges(String keyspace, Host host)
Note that this information is refreshed asynchronously by the control connection, when schema or ring topology changes. It might occasionally be stale (or even empty).
keyspace
- the name of the keyspace to get token ranges for.host
- the host.host
as known by the driver. Note that
the result might be stale or empty if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
.public Set<Host> getReplicas(String keyspace, com.datastax.driver.core.Token.Factory partitioner, ByteBuffer partitionKey)
null
and then a cluster-wide partitioner will be invoked.
Note that this information is refreshed asynchronously by the control connection, when schema or ring topology changes. It might occasionally be stale (or even empty).
keyspace
- the name of the keyspace to get replicas for.partitioner
- the partitioner to use or @{code null} for cluster-wide partitioner.partitionKey
- the partition key for which to find the set of replica.partitionKey
as known by the driver. Note
that the result might be stale or empty if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
.public Set<Host> getReplicas(String keyspace, TokenRange range)
Note that it is assumed that the input range does not overlap across multiple host ranges. If the range extends over multiple hosts, it only returns the replicas for those hosts that are replicas for the last token of the range. This behavior may change in a future release, see JAVA-1355.
Also note that this information is refreshed asynchronously by the control connection, when schema or ring topology changes. It might occasionally be stale (or even empty).
keyspace
- the name of the keyspace to get replicas for.range
- the token range.range
as known by the driver. Note that the
result might be stale or empty if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
.public String getClusterName()
public String getPartitioner()
public Set<Host> getAllHosts()
public boolean checkSchemaAgreement()
This method performs a one-time check only, without any form of retry; therefore Cluster.Builder.withMaxSchemaAgreementWaitSeconds(int)
does not apply in this case.
true
if all hosts agree on the schema; false
if they don't agree, or if
the check could not be performed (for example, if the control connection is down).public KeyspaceMetadata getKeyspace(String keyspace)
keyspace
- the name of the keyspace for which metadata should be returned.null
if keyspace
is not a
known keyspace. Note that the result might be stale or null if metadata was explicitly
disabled with QueryOptions.setMetadataEnabled(boolean)
.public List<KeyspaceMetadata> getKeyspaces()
QueryOptions.setMetadataEnabled(boolean)
.public String exportSchemaAsString()
String
containing CQL queries representing the schema of this cluster.
In other words, this method returns the queries that would allow to recreate the schema of this cluster.
Note that the returned String is formatted to be human readable (for some definition of human readable at least).
It might be stale or empty if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
.
public TupleType newTupleType(DataType... types)
types
- the types for the tuple type.public TupleType newTupleType(List<DataType> types)
types
- the types for the tuple type.public Token newToken(String tokenStr)
Token
from its string representation, according to the partitioner
reported by the Cassandra nodes.tokenStr
- the string representation.IllegalStateException
- if the token factory was not initialized. This would typically
happen if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
before startup.public Token newToken(com.datastax.driver.core.Token.Factory partitioner, ByteBuffer... components)
Token
from a partition key. Partitioner can be null
and then a
cluster-wide partitioner will be invoked.partitioner
- the partitioner to use or @{code null} for cluster-wide partitioner.components
- the components of the partition key, in their serialized form (obtained with
TypeCodec.serialize(Object, ProtocolVersion)
).IllegalStateException
- if the token factory was not initialized. This would typically
happen if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
before startup.public TokenRange newTokenRange(Token start, Token end)
TokenRange
.start
- the start token.end
- the end token.IllegalStateException
- if the token factory was not initialized. This would typically
happen if metadata was explicitly disabled with QueryOptions.setMetadataEnabled(boolean)
before startup.Copyright © 2012–2024. All rights reserved.