com.datastax.driver.core.KeyspaceMetadata Java Examples

The following examples show how to use com.datastax.driver.core.KeyspaceMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraIntervalCollectionPersistor.java    From brein-time-utilities with Apache License 2.0 6 votes vote down vote up
protected void createColumnFamily() {
    final String ks = getKeySpace();
    final String cf = getColumnFamily();

    final KeyspaceMetadata keySpaceMeta = this.cluster.getMetadata().getKeyspace(ks);
    final TableMetadata tableMetadata = keySpaceMeta.getTable(cf);

    // check if the table exists
    if (tableMetadata != null) {
        return;
    }

    final String stmt = String.format("CREATE TABLE %s (\n" +
            "  " + KEY_COLUMN + " text,\n" +
            "  " + COLL_COLUMN + " blob,\n" +
            "  PRIMARY KEY (" + KEY_COLUMN + ")\n" +
            ");", cf);

    getSession().execute(stmt);
}
 
Example #2
Source File: CassandraSession.java    From presto with Apache License 2.0 6 votes vote down vote up
private KeyspaceMetadata getKeyspaceByCaseInsensitiveName(String caseInsensitiveSchemaName)
        throws SchemaNotFoundException
{
    List<KeyspaceMetadata> keyspaces = executeWithSession(session -> session.getCluster().getMetadata().getKeyspaces());
    KeyspaceMetadata result = null;
    // Ensure that the error message is deterministic
    List<KeyspaceMetadata> sortedKeyspaces = Ordering.from(comparing(KeyspaceMetadata::getName)).immutableSortedCopy(keyspaces);
    for (KeyspaceMetadata keyspace : sortedKeyspaces) {
        if (keyspace.getName().equalsIgnoreCase(caseInsensitiveSchemaName)) {
            if (result != null) {
                throw new PrestoException(
                        NOT_SUPPORTED,
                        format("More than one keyspace has been found for the case insensitive schema name: %s -> (%s, %s)",
                                caseInsensitiveSchemaName, result.getName(), keyspace.getName()));
            }
            result = keyspace;
        }
    }
    if (result == null) {
        throw new SchemaNotFoundException(caseInsensitiveSchemaName);
    }
    return result;
}
 
Example #3
Source File: CassandraSession.java    From presto with Apache License 2.0 6 votes vote down vote up
private static AbstractTableMetadata getTableMetadata(KeyspaceMetadata keyspace, String caseInsensitiveTableName)
{
    List<AbstractTableMetadata> tables = Stream.concat(
            keyspace.getTables().stream(),
            keyspace.getMaterializedViews().stream())
            .filter(table -> table.getName().equalsIgnoreCase(caseInsensitiveTableName))
            .collect(toImmutableList());
    if (tables.size() == 0) {
        throw new TableNotFoundException(new SchemaTableName(keyspace.getName(), caseInsensitiveTableName));
    }
    if (tables.size() == 1) {
        return tables.get(0);
    }
    String tableNames = tables.stream()
            .map(AbstractTableMetadata::getName)
            .sorted()
            .collect(joining(", "));
    throw new PrestoException(
            NOT_SUPPORTED,
            format("More than one table has been found for the case insensitive table name: %s -> (%s)",
                    caseInsensitiveTableName, tableNames));
}
 
Example #4
Source File: TestCassandraTable.java    From ingestion with Apache License 2.0 6 votes vote down vote up
private void mockTableMetadata() {
  final ColumnMetadata idColumn = mock(ColumnMetadata.class);
  when(idColumn.getName()).thenReturn("id");
  when(idColumn.getType()).thenReturn(DataType.cint());

  final ColumnMetadata textColumn = mock(ColumnMetadata.class);
  when(textColumn.getName()).thenReturn("text_col");
  when(textColumn.getType()).thenReturn(DataType.text());

  final KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class);
  when(keyspaceMetadata.getName()).thenReturn("my_keyspace");

  when(tableMetadata.getName()).thenReturn("my_table");
  when(tableMetadata.getColumns()).thenReturn(ImmutableList.of(idColumn, textColumn));
  when(tableMetadata.getKeyspace()).thenReturn(keyspaceMetadata);
  when(tableMetadata.getPrimaryKey()).thenReturn(ImmutableList.of(idColumn));
}
 
Example #5
Source File: ClusterManagerTest.java    From scalardb with Apache License 2.0 6 votes vote down vote up
@Test
public void getMetadata_ExistingKeyspaceAndTableGiven_ShouldReturnMetadata() {
  // Arrange
  manager.getSession();
  Metadata metadata = mock(Metadata.class);
  KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class);
  TableMetadata tableMetadata = mock(TableMetadata.class);
  when(cluster.getMetadata()).thenReturn(metadata);
  when(metadata.getKeyspace(anyString())).thenReturn(keyspaceMetadata);
  when(keyspaceMetadata.getTable(anyString())).thenReturn(tableMetadata);

  // Act
  TableMetadata actual = manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);

  // Assert
  assertThat(actual).isEqualTo(tableMetadata);
}
 
Example #6
Source File: ClusterManagerTest.java    From scalardb with Apache License 2.0 6 votes vote down vote up
@Test
public void getMetadata_TableNotExists_ShouldThrowStorageRuntimeException() {
  // Arrange
  manager.getSession();
  Metadata metadata = mock(Metadata.class);
  KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class);
  when(cluster.getMetadata()).thenReturn(metadata);
  when(metadata.getKeyspace(anyString())).thenReturn(keyspaceMetadata);
  when(keyspaceMetadata.getTable(anyString())).thenReturn(null);

  // Act
  assertThatThrownBy(
          () -> {
            manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);
          })
      .isInstanceOf(StorageRuntimeException.class);
}
 
Example #7
Source File: CassandraConnectorDatabaseService.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public DatabaseInfo get(
    @Nonnull @NonNull final ConnectorRequestContext context,
    @Nonnull @NonNull final QualifiedName name
) {
    final String keyspace = name.getDatabaseName();
    log.debug("Attempting to get keyspace metadata for keyspace {} for request {}", keyspace, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }

        log.debug("Successfully found the keyspace metadata for {} for request {}", name, context);
        return DatabaseInfo.builder().name(name).build();
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
 
Example #8
Source File: CassandraConnectorTableService.java    From metacat with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public TableInfo get(@Nonnull @NonNull final ConnectorRequestContext context,
                     @Nonnull @NonNull final QualifiedName name) {
    final String keyspace = name.getDatabaseName();
    final String table = name.getTableName();
    log.debug("Attempting to get metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }
        final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
        if (tableMetadata == null) {
            throw new TableNotFoundException(name);
        }

        final TableInfo tableInfo = this.getTableInfo(name, tableMetadata);
        log.debug("Successfully got metadata for Cassandra table {}.{} for request {}", keyspace, table, context);
        return tableInfo;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
 
Example #9
Source File: MetadataResultSets.java    From cassandra-jdbc-wrapper with Apache License 2.0 6 votes vote down vote up
public  CassandraMetadataResultSet makeSchemas(CassandraStatement statement, String schemaPattern) throws SQLException
{
	
	// TABLE_SCHEM String => schema name
    // TABLE_CATALOG String => catalog name (may be null)
	
	final ArrayList<MetadataRow> schemas = Lists.newArrayList();
	List<KeyspaceMetadata> keyspaces = statement.connection.getClusterMetadata().getKeyspaces();
	
	for(KeyspaceMetadata keyspace:keyspaces){
		if ("%".equals(schemaPattern)) schemaPattern = null;
		if((schemaPattern==null?keyspace.getName():schemaPattern).equals(keyspace.getName())){
			MetadataRow row = new MetadataRow().addEntry("TABLE_SCHEM", keyspace.getName()).addEntry("TABLE_CATALOG", statement.connection.getCatalog());
			schemas.add(row);
		}
		
	}
   
    CassandraMetadataResultSet result = new CassandraMetadataResultSet(statement,new MetadataResultSet().setRows(schemas));
    return result;
	    	
}
 
Example #10
Source File: DescribeTablesExecutor.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 * Execute DESCRIBE TABLES .
 * @param metaData
 * @return table
 */
@Override
public Table execute(Metadata metaData) {
    FunctionalList<KeyspaceMetadata,RowData> functional = new FunctionalList<>( metaData.getKeyspaces());
    List<RowData> rows = functional.map(new KeyspaceTablestoRowData());
    return new Table(new ListUtils<String>().buildList(), rows);
}
 
Example #11
Source File: CassandraOperations.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public boolean indexExists(final String indexName) throws IOException {
  final String tableName = getCassandraSafeName(indexName);
  Boolean tableExists = state.tableExistsCache.get(tableName);
  if (tableExists == null) {
    final KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(gwNamespace);
    if (keyspace != null) {
      tableExists = keyspace.getTable(tableName) != null;
    } else {
      tableExists = false;
    }
    state.tableExistsCache.put(tableName, tableExists);
  }
  return tableExists;
}
 
Example #12
Source File: CassandraStorage.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
protected void createSchema(Session session, Cluster cluster) throws Exception {
    if (!createSchemaOnStartup)
        return;

    final KeyspaceMetadata metaData = cluster.getMetadata().getKeyspace(session.getLoggedKeyspace());
    if (metaData.getTable("COP_WORKFLOW_INSTANCE") != null) {
        logger.info("skipping schema creation");
        return;
    }

    logger.info("Creating tables...");
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(CassandraStorage.class.getResourceAsStream("copper-schema.cql")))) {
        StringBuilder cql = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            line = line.trim();
            if (line.isEmpty())
                continue;
            if (line.startsWith("--"))
                continue;
            if (line.endsWith(";")) {
                if (line.length() > 1)
                    cql.append(line.substring(0, line.length() - 1));
                String cqlCmd = cql.toString();
                cql = new StringBuilder();
                logger.info("Executing CQL {}", cqlCmd);
                session.execute(cqlCmd);
            }
            else {
                cql.append(line).append(" ");
            }
        }
    }

}
 
Example #13
Source File: KeyspacestoRowDataFuntion.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 * Transform KeyspaceMetadata to RowData
 * @param objetcToTransform input object
 * @return
 */
@Override
public RowData transform(KeyspaceMetadata keyspaceMetadata) {
    List<CellData> cells = new ArrayList<>();
    cells.add(new CellData(keyspaceMetadata.getName()));
    return new RowData(cells);
}
 
Example #14
Source File: KeyspaceTablestoRowData.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param keyspaceMetadata
 * @return ransform KeySpacetables to CellData with tableName
 */
@Override
public RowData transform(KeyspaceMetadata keyspaceMetadata) {

    List<TableMetadata> tables = new ArrayList<>(keyspaceMetadata.getTables());
    FunctionalList<TableMetadata,CellData> functional = new FunctionalList<>(tables);
    List<CellData> cells = new ListUtils<CellData>().buildList(new CellData(buildTable(keyspaceMetadata.getName(),functional)));
    return new RowData(cells);
}
 
Example #15
Source File: GCGraceSecondsManager.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@Override
public void onKeyspaceChanged(KeyspaceMetadata current, KeyspaceMetadata previous) {
    if (!current.getName().equals(keyspace)) {
        return;
    }
    String oldReplicationFactor = previous.getReplication().get("replication_factor");
    String newReplicationFactor = current.getReplication().get("replication_factor");
    if (!oldReplicationFactor.equals(newReplicationFactor)) {
        logger.info("replication_factor of " + keyspace + " has changed from " + oldReplicationFactor +
                " to " + newReplicationFactor);
        maybeUpdateGCGraceSeconds();
    }
}
 
Example #16
Source File: DescribeKeyspacesExecutor.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 * Execute Describe Keysapces
 * @param metaData
 * @return  table
 */
@Override
public Table execute(Metadata metaData) {
    FunctionalList<KeyspaceMetadata,RowData> functional = new FunctionalList<>(metaData.getKeyspaces());
    List<RowData> rowDatas = functional.map(new KeyspacestoRowDataFuntion());
    return new Table(new ListUtils<String>().buildList(), rowDatas);
}
 
Example #17
Source File: DescribeKeySpaceAnyExecutor.java    From Explorer with Apache License 2.0 5 votes vote down vote up
/**
 *  Execute when shCQL is DESCRIBE KEYSPACE anyvalue
 *  @param metaData
 * @return  table
 */
@Override
public Table execute(Metadata metaData) {
    KeyspaceMetadata keySpaceMetada = metaData.getKeyspace(param);
    FunctionalList<TableMetadata,RowData> functionalList = new FunctionalList<>(new ArrayList<>(keySpaceMetada.getTables()));
    List<RowData> rows = functionalList.map(new TableMetadataToRowDataFunction());
    rows.add(0,buildFirst(keySpaceMetada.toString()));
    return new Table(new ListUtils<String>().buildList(),rows);
}
 
Example #18
Source File: CassandraUtils.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public static TableMetadata getTableMetadata(final Session session, final String keyspace, final String table) {
  Preconditions.checkNotNull(session);
  Preconditions.checkNotNull(keyspace);
  Preconditions.checkNotNull(table);
  final KeyspaceMetadata keyspaceMetadata = session.getCluster().getMetadata().getKeyspace(keyspace);
  if (keyspaceMetadata == null) {
    throw new IllegalStateException(String.format("Keyspace %s does not exist", keyspace));
  }
  final TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
  if (tableMetadata == null) {
    throw new IllegalStateException(String.format("Table %s.%s does not exist", keyspace, table));
  }
  return tableMetadata;
}
 
Example #19
Source File: CQLService.java    From Doradus with Apache License 2.0 5 votes vote down vote up
public List<String> getDoradusKeyspaces() {
    List<String> keyspaces = new ArrayList<>();
    List<KeyspaceMetadata> keyspaceList = m_cluster.getMetadata().getKeyspaces();
    for (KeyspaceMetadata ksMetadata : keyspaceList) {
        if (ksMetadata.getTable(APPS_CQL_NAME) != null) {
            keyspaces.add(ksMetadata.getName());
        }
    }
    return keyspaces;
}
 
Example #20
Source File: CQLMetadataCache.java    From Doradus with Apache License 2.0 5 votes vote down vote up
/**
 * Return true if column values for the given namespace/store name are binary.
 * 
 * @param namespace Namespace (Keyspace) name.
 * @param storeName Store (ColumnFamily) name.
 * @return          True if the given table's column values are binary.
 */
public boolean columnValueIsBinary(String namespace, String storeName) {
    Boolean cachedValue = getCachedValueIsBinary(namespace, storeName);
    if(cachedValue != null) return cachedValue.booleanValue();
    
    String cqlKeyspace = CQLService.storeToCQLName(namespace);
    String tableName = CQLService.storeToCQLName(storeName);
    KeyspaceMetadata ksMetadata = m_cluster.getMetadata().getKeyspace(cqlKeyspace);
    TableMetadata tableMetadata = ksMetadata.getTable(tableName);
    ColumnMetadata colMetadata = tableMetadata.getColumn("value");
    boolean isBinary = colMetadata.getType().equals(DataType.blob());
    
    putCachedValueIsBinary(namespace, storeName, isBinary);
    return isBinary;
}
 
Example #21
Source File: CassandraPersistWriter.java    From streams with Apache License 2.0 5 votes vote down vote up
private void createKeyspaceAndTable() {
  Metadata metadata = client.cluster().getMetadata();
  if (Objects.isNull(metadata.getKeyspace(config.getKeyspace()))) {
    LOGGER.info("Keyspace {} does not exist. Creating Keyspace", config.getKeyspace());
    Map<String, Object> replication = new HashMap<>();
    replication.put("class", "SimpleStrategy");
    replication.put("replication_factor", 1);

    String createKeyspaceStmt = SchemaBuilder.createKeyspace(config.getKeyspace()).with()
        .replication(replication).getQueryString();
    client.cluster().connect().execute(createKeyspaceStmt);
  }

  session = client.cluster().connect(config.getKeyspace());

  KeyspaceMetadata ks = metadata.getKeyspace(config.getKeyspace());
  TableMetadata tableMetadata = ks.getTable(config.getTable());

  if (Objects.isNull(tableMetadata)) {
    LOGGER.info("Table {} does not exist in Keyspace {}. Creating Table", config.getTable(), config.getKeyspace());
    String createTableStmt = SchemaBuilder.createTable(config.getTable())
                              .addPartitionKey(config.getPartitionKeyColumn(), DataType.varchar())
                              .addColumn(config.getColumn(), DataType.blob()).getQueryString();

    session.execute(createTableStmt);
  }
}
 
Example #22
Source File: CassandraDeepJobConfig.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches table metadata from the underlying datastore, using DataStax java driver.
 *
 * @return the table metadata as returned by the driver.
 */
public TableMetadata fetchTableMetadata() {

    Metadata metadata = getSession().getCluster().getMetadata();
    KeyspaceMetadata ksMetadata = metadata.getKeyspace(quote(this.catalog));

    if (ksMetadata != null) {
        return ksMetadata.getTable(quote(this.table));
    } else {
        return null;
    }
}
 
Example #23
Source File: CassandraSession.java    From presto with Apache License 2.0 5 votes vote down vote up
public List<String> getCaseSensitiveSchemaNames()
{
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    List<KeyspaceMetadata> keyspaces = executeWithSession(session -> session.getCluster().getMetadata().getKeyspaces());
    for (KeyspaceMetadata meta : keyspaces) {
        builder.add(meta.getName());
    }
    return builder.build();
}
 
Example #24
Source File: Session.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public Collection<TableMetadata> getTables() {
    KeyspaceMetadata keyspace = getKeyspace();
    if (keyspace == null) {
        return ImmutableList.of();
    } else {
        return keyspace.getTables();
    }
}
 
Example #25
Source File: Session.java    From glowroot with Apache License 2.0 5 votes vote down vote up
public @Nullable TableMetadata getTable(String name) {
    KeyspaceMetadata keyspace = getKeyspace();
    if (keyspace == null) {
        return null;
    } else {
        return keyspace.getTable(name);
    }
}
 
Example #26
Source File: CassandraTypeTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void initializeShouldReturnAlreadyDoneWhenTypeExists() {
    KeyspaceMetadata keyspace = mock(KeyspaceMetadata.class);
    when(keyspace.getUserType(NAME)).thenReturn(mock(UserType.class));
    Session session = mock(Session.class);

    assertThat(TYPE.initialize(keyspace, session))
            .isEqualByComparingTo(ALREADY_DONE);

    verify(keyspace).getUserType(NAME);
    verify(session, never()).execute(STATEMENT);
}
 
Example #27
Source File: CassandraTypeTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void initializeShouldExecuteCreateStatementAndReturnFullWhenTypeDoesNotExist() {
    KeyspaceMetadata keyspace = mock(KeyspaceMetadata.class);
    when(keyspace.getUserType(NAME)).thenReturn(null);
    Session session = mock(Session.class);

    assertThat(TYPE.initialize(keyspace, session))
            .isEqualByComparingTo(FULL);

    verify(keyspace).getUserType(NAME);
    verify(session).execute(STATEMENT);
}
 
Example #28
Source File: CassandraTableTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void initializeShouldExecuteReturnAlreadyDoneWhenTableExists() {
    KeyspaceMetadata keyspace = mock(KeyspaceMetadata.class);
    when(keyspace.getTable(NAME)).thenReturn(mock(TableMetadata.class));
    Session session = mock(Session.class);

    assertThat(TABLE.initialize(keyspace, session))
            .isEqualByComparingTo(ALREADY_DONE);

    verify(keyspace).getTable(NAME);
    verify(session, never()).execute(STATEMENT);
}
 
Example #29
Source File: CassandraTableTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void initializeShouldExecuteCreateStatementAndReturnFullWhenTableDoesNotExist() {
    KeyspaceMetadata keyspace = mock(KeyspaceMetadata.class);
    when(keyspace.getTable(NAME)).thenReturn(null);
    Session session = mock(Session.class);

    assertThat(TABLE.initialize(keyspace, session))
            .isEqualByComparingTo(FULL);

    verify(keyspace).getTable(NAME);
    verify(session).execute(STATEMENT);
}
 
Example #30
Source File: CassandraType.java    From james-project with Apache License 2.0 5 votes vote down vote up
public InitializationStatus initialize(KeyspaceMetadata keyspaceMetadata, Session session) {
    if (keyspaceMetadata.getUserType(name) != null) {
        return InitializationStatus.ALREADY_DONE;
    }

    session.execute(createStatement);
    return InitializationStatus.FULL;
}