本文整理了Java中com.datastax.driver.core.Cluster.getMetadata()
方法的一些代码示例,展示了Cluster.getMetadata()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cluster.getMetadata()
方法的具体详情如下:
包路径:com.datastax.driver.core.Cluster
类名称:Cluster
方法名:getMetadata
[英]Returns read-only metadata on the connected cluster.
This includes the known nodes with their status as seen by the driver, as well as the schema definitions. Since this return metadata on the connected cluster, this method may trigger the creation of a connection if none has been established yet (neither init() nor connect()has been called yet).
[中]返回所连接群集上的只读元数据。
这包括驱动程序看到的已知节点及其状态,以及模式定义。由于此方法在连接的集群上返回元数据,因此如果尚未建立连接(尚未调用init()或connect()),则此方法可能会触发连接的创建。
代码示例来源:origin: prestodb/presto
private void checkSizeEstimatesTableExist()
{
KeyspaceMetadata keyspaceMetadata = executeWithSession(session -> session.getCluster().getMetadata().getKeyspace(SYSTEM));
checkState(keyspaceMetadata != null, "system keyspace metadata must not be null");
TableMetadata table = keyspaceMetadata.getTable(SIZE_ESTIMATES);
if (table == null) {
throw new PrestoException(NOT_SUPPORTED, "Cassandra versions prior to 2.1.5 are not supported");
}
}
代码示例来源:origin: kaaproject/kaa
protected UserType getUserType(String userType) {
return getSession().getCluster().getMetadata().getKeyspace(KAA).getUserType(userType);
}
代码示例来源:origin: apache/usergrid
private void createTable(TableDefinition tableDefinition, boolean forceCheckSchema) throws Exception {
boolean exists;
if(!forceCheckSchema){
exists = dataStaxCluster.getClusterSession().getCluster()
.getMetadata()
.getKeyspace(CQLUtils.quote( tableDefinition.getKeyspace() ) )
.getTable( tableDefinition.getTableName() ) != null;
}else{
exists = dataStaxCluster.getClusterSession()
.execute("select * from system.schema_columnfamilies where keyspace_name='"+tableDefinition.getKeyspace()
+"' and columnfamily_name='"+CQLUtils.unquote(tableDefinition.getTableName())+"'").one() != null;
}
if( exists ){
logger.info("Not creating table {}, it already exists.", tableDefinition.getTableName());
return;
}
String CQL = tableDefinition.getTableCQL(cassandraFig, TableDefinition.ACTION.CREATE);
if (logger.isDebugEnabled()) {
logger.debug(CQL);
}
if ( tableDefinition.getKeyspace().equals( cassandraFig.getApplicationKeyspace() )) {
dataStaxCluster.getApplicationSession().execute( CQL );
} else {
dataStaxCluster.getApplicationLocalSession().execute( CQL );
}
logger.info("Created table: {} in keyspace {}",
tableDefinition.getTableName(), tableDefinition.getKeyspace());
}
代码示例来源:origin: apache/usergrid
/**
* Execute CQL that will create the keyspace if it doesn't exist and alter it if it does.
* @throws Exception
* @param forceCheck
*/
@Override
public synchronized void createApplicationKeyspace(boolean forceCheck) throws Exception {
boolean exists;
if(!forceCheck) {
// this gets info from client's metadata
exists = getClusterSession().getCluster().getMetadata()
.getKeyspace(CQLUtils.quote(cassandraConfig.getApplicationKeyspace())) != null;
}else{
exists = getClusterSession()
.execute("select * from system.schema_keyspaces where keyspace_name = '"+cassandraConfig.getApplicationKeyspace()+"'")
.one() != null;
}
if(exists){
logger.info("Not creating keyspace {}, it already exists.", cassandraConfig.getApplicationKeyspace());
return;
}
final String createApplicationKeyspace = String.format(
"CREATE KEYSPACE IF NOT EXISTS %s WITH replication = %s",
CQLUtils.quote( cassandraConfig.getApplicationKeyspace()),
CQLUtils.getFormattedReplication( cassandraConfig.getStrategy(), cassandraConfig.getStrategyOptions())
);
getClusterSession().execute(createApplicationKeyspace);
waitForSchemaAgreement();
logger.info("Created keyspace: {}", cassandraConfig.getApplicationKeyspace());
}
代码示例来源:origin: apache/usergrid
if(!forceCheck) {
exists = getClusterSession().getCluster().getMetadata()
.getKeyspace(CQLUtils.quote(cassandraConfig.getApplicationLocalKeyspace())) != null;
}else{
exists = getClusterSession()
代码示例来源:origin: kaaproject/kaa
@SuppressWarnings("unchecked")
private UDTValue convertValue(Object value,
CassandraEntityMapper<?> mapper,
CassandraClient cassandraClient) {
String keyspace = cassandraClient.getSession().getLoggedKeyspace();
UserType userType = cassandraClient.getSession()
.getCluster()
.getMetadata()
.getKeyspace(keyspace)
.getUserType(mapper.getName());
UDTValue udtValue = userType.newValue();
for (String name : mapper.getNonKeyColumnNames()) {
Object fieldValue = mapper.getColumnValueForName(name, value, cassandraClient);
if (fieldValue != null) {
udtValue.set(name, fieldValue, (Class<Object>) fieldValue.getClass());
} else {
udtValue.setToNull(name);
}
}
return udtValue;
}
}
代码示例来源:origin: hugegraph/hugegraph
protected boolean existsKeyspace() {
return this.cluster().getMetadata().getKeyspace(this.keyspace) != null;
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private void assertNotExists(String name) {
assertNull(cluster().getMetadata().getKeyspace(name));
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private void assertExists(String fetchName, String realName) {
KeyspaceMetadata km = cluster().getMetadata().getKeyspace(fetchName);
assertNotNull(km);
assertEquals(realName, km.getName());
}
代码示例来源:origin: Impetus/Kundera
@Override
public void deleteByColumn(String schemaName, String tableName, String columnName, Object columnValue) {
Session session = factory.getConnection();
String rowKeyName = null;
CQLTranslator translator = new CQLTranslator();
try {
List<ColumnMetadata> primaryKeys = session.getCluster().getMetadata().getKeyspace("\"" + schemaName + "\"")
.getTable("\"" + tableName + "\"").getPrimaryKey();
rowKeyName = primaryKeys.get(0).getName();
} finally {
// factory.releaseConnection(session);
}
List rowKeys =
getColumnsById(schemaName, tableName, columnName, rowKeyName, columnValue, columnValue.getClass());
for (Object rowKey : rowKeys) {
if (rowKey != null) {
String deleteQuery = CQLTranslator.DELETE_QUERY;
deleteQuery = StringUtils.replace(deleteQuery, CQLTranslator.COLUMN_FAMILY,
translator.ensureCase(new StringBuilder(), tableName, false).toString());
StringBuilder deleteQueryBuilder = new StringBuilder(deleteQuery);
deleteQueryBuilder.append(CQLTranslator.ADD_WHERE_CLAUSE);
deleteQueryBuilder = translator.ensureCase(deleteQueryBuilder, rowKeyName, false);
deleteQueryBuilder.append(CQLTranslator.EQ_CLAUSE);
translator.appendValue(deleteQueryBuilder, rowKey.getClass(), rowKey, false, false);
this.execute(deleteQueryBuilder.toString(), null);
}
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Override
public Boolean call() throws Exception {
return cluster().getMetadata().getKeyspace(keyspace).getTable("user") != null;
}
};
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private MaterializedViewMetadata getMaterializedView(String name) {
return cluster().getMetadata().getKeyspace(keyspace).getMaterializedView(name);
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
private TableMetadata getTable(String name) {
return cluster().getMetadata().getKeyspace(keyspace).getTable(name);
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void remainingDeleteTests() throws Exception {
Statement query;
TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable(TABLE_TEXT);
assertNotNull(table);
String expected = String.format("DELETE k FROM %s.test_text;", keyspace);
query = delete("k").from(table);
assertEquals(query.toString(), expected);
try {
session().execute(query);
fail();
} catch (SyntaxError e) {
// Missing WHERE clause
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void should_parse_user_defined_type_when_definition_in_old_user_types() {
Metadata metadata = cluster().getMetadata();
KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(this.keyspace);
assertThat(parse("\"A\"", cluster(), keyspace, null, keyspaceMetadata.userTypes, false, false))
.isUserType(keyspace, "A");
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
/**
* Ensures that if a table is configured with change data capture enabled that {@link
* TableOptionsMetadata#isCDC()} returns true for that table.
*
* @test_category metadata
* @jira_ticket JAVA-1287
* @jira_ticket CASSANDRA-12041
*/
@Test(groups = "short")
public void should_parse_cdc_from_table_options() {
// given
// create a simple table with cdc as true.
String cql =
String.format(
"CREATE TABLE %s.cdc_table (\n"
+ " k text,\n"
+ " c int,\n"
+ " v timeuuid,\n"
+ " PRIMARY KEY (k, c)\n"
+ ") WITH cdc=true;",
keyspace);
session().execute(cql);
// when retrieving the table's metadata.
TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("cdc_table");
// then the table's options should have cdc as true.
assertThat(table.getOptions().isCDC()).isEqualTo(true);
assertThat(table.asCQLQuery(true)).contains("cdc = true");
}
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void should_escape_single_quote_table_comment() {
// given
String cql =
String.format(
"CREATE TABLE %s.single_quote (\n"
+ " c1 int PRIMARY KEY\n"
+ ") WITH comment = 'comment with single quote '' should work'",
keyspace);
// when
session().execute(cql);
TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("single_quote");
// then
assertThat(table.getOptions().getComment())
.isEqualTo("comment with single quote ' should work");
assertThat(table.asCQLQuery()).contains("comment = 'comment with single quote '' should work'");
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void should_parse_user_defined_type_when_definition_in_current_user_types() {
Metadata metadata = cluster().getMetadata();
KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(this.keyspace);
assertThat(
parse(
"frozen<\"A\">",
cluster(),
keyspace,
keyspaceMetadata.userTypes,
null,
false,
false))
.isUserType(keyspace, "A");
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void insertUdtTest() throws Exception {
UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt");
UDTValue udtValue =
udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost"));
Statement insert = insertInto("udtTest").value("k", 1).value("t", udtValue);
assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) VALUES (1,{i:2,a:'127.0.0.1'});");
session().execute(insert);
List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all();
assertEquals(rows.size(), 1);
Row r1 = rows.get(0);
assertEquals("127.0.0.1", r1.getUDTValue("t").getInet("a").getHostAddress());
}
代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core
@Test(groups = "short")
public void should_handle_UDT_with_many_fields() throws Exception {
int MAX_TEST_LENGTH = 1024;
// create the seed udt
StringBuilder sb = new StringBuilder();
for (int i = 0; i < MAX_TEST_LENGTH; ++i) {
sb.append(String.format("v_%s int", i));
if (i + 1 < MAX_TEST_LENGTH) sb.append(",");
}
session().execute(String.format("CREATE TYPE lengthy_udt (%s)", sb.toString()));
// create a table with multiple sizes of udts
session().execute("CREATE TABLE lengthy_udt_table (k int PRIMARY KEY, v frozen<lengthy_udt>)");
// hold onto the UserType for future use
UserType udtDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("lengthy_udt");
// verify inserts and reads
for (int i : Arrays.asList(0, 1, 2, 3, MAX_TEST_LENGTH)) {
// create udt
UDTValue createdUDT = udtDef.newValue();
for (int j = 0; j < i; ++j) {
createdUDT.setInt(j, j);
}
// write udt
session().execute("INSERT INTO lengthy_udt_table (k, v) VALUES (0, ?)", createdUDT);
// verify udt was written and read correctly
UDTValue r =
session().execute("SELECT v FROM lengthy_udt_table WHERE k=0").one().getUDTValue("v");
assertThat(r.toString()).isEqualTo(createdUDT.toString());
}
}
内容来源于网络,如有侵权,请联系作者删除!