Search in sources :

Example 36 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata in project fallout by datastax.

the class FalloutAppExtensionBase method maybeCleanDatabase.

private void maybeCleanDatabase() {
    if (getCassandraDriverManager() != null) {
        KeyspaceMetadata metadata = getSession().getCluster().getMetadata().getKeyspace(CASSANDRA_KEYSPACE);
        metadata.getTables().forEach(table -> {
            getSession().execute(String.format("TRUNCATE %s.%s;", CASSANDRA_KEYSPACE, table.getName()));
        });
        TestRunDAO.addFinishedTestRunEndStop(CASSANDRA_KEYSPACE, getCassandraDriverManager().getMappingManager());
    }
}
Also used : KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 37 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata in project scalardb by scalar-labs.

the class ClusterManagerTest method getMetadata_TableNotExists_ShouldReturnNull.

@Test
public void getMetadata_TableNotExists_ShouldReturnNull() {
    // Arrange
    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
    TableMetadata actual = manager.getMetadata(ANY_KEYSPACE_NAME, ANY_TABLE_NAME);
    // Assert
    assertThat(actual).isNull();
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableMetadata(com.datastax.driver.core.TableMetadata) Metadata(com.datastax.driver.core.Metadata) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) Test(org.junit.jupiter.api.Test)

Example 38 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata in project scalardb by scalar-labs.

the class ClusterManagerTest method getMetadata_WithReservedKeywordsExistingKeyspaceAndTableGiven_ShouldReturnMetadata.

@Test
public void getMetadata_WithReservedKeywordsExistingKeyspaceAndTableGiven_ShouldReturnMetadata() {
    // Arrange
    String keyspace = "keyspace";
    String table = "table";
    Metadata metadata = mock(Metadata.class);
    KeyspaceMetadata keyspaceMetadata = mock(KeyspaceMetadata.class);
    TableMetadata tableMetadata = mock(TableMetadata.class);
    when(cluster.getMetadata()).thenReturn(metadata);
    when(metadata.getKeyspace(quote(keyspace))).thenReturn(keyspaceMetadata);
    when(keyspaceMetadata.getTable(quote(table))).thenReturn(tableMetadata);
    // Act
    TableMetadata actual = manager.getMetadata(keyspace, table);
    // Assert
    assertThat(actual).isEqualTo(tableMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableMetadata(com.datastax.driver.core.TableMetadata) Metadata(com.datastax.driver.core.Metadata) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) Test(org.junit.jupiter.api.Test)

Example 39 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata in project metacat by Netflix.

the class CassandraConnectorTableService method list.

/**
 * {@inheritDoc}
 */
@Override
public List<TableInfo> list(@Nonnull @NonNull final ConnectorRequestContext context, @Nonnull @NonNull final QualifiedName name, @Nullable final QualifiedName prefix, @Nullable final Sort sort, @Nullable final Pageable pageable) {
    final String keyspace = name.getDatabaseName();
    log.debug("Attempting to list tables in Cassandra keyspace {} for request {}", keyspace, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            throw new DatabaseNotFoundException(name);
        }
        // TODO: Should we include views?
        final List<TableInfo> tables = Lists.newArrayList();
        for (final TableMetadata tableMetadata : keyspaceMetadata.getTables()) {
            if (prefix != null && !tableMetadata.getName().startsWith(prefix.getTableName())) {
                continue;
            }
            tables.add(this.getTableInfo(name, tableMetadata));
        }
        // Sort
        if (sort != null) {
            final Comparator<TableInfo> tableComparator = Comparator.comparing((t) -> t.getName().getTableName());
            ConnectorUtils.sort(tables, sort, tableComparator);
        }
        // Paging
        final List<TableInfo> pagedTables = ConnectorUtils.paginate(tables, pageable);
        log.debug("Listed {} tables in Cassandra keyspace {} for request {}", pagedTables.size(), keyspace, context);
        return pagedTables;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) DatabaseNotFoundException(com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException) TableInfo(com.netflix.metacat.common.server.connectors.model.TableInfo) DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 40 with KeyspaceMetadata

use of com.datastax.driver.core.KeyspaceMetadata in project metacat by Netflix.

the class CassandraConnectorTableService method exists.

/**
 * {@inheritDoc}
 */
@Override
public boolean exists(@Nonnull @NonNull final ConnectorRequestContext context, @Nonnull @NonNull final QualifiedName name) {
    final String keyspace = name.getDatabaseName();
    final String table = name.getTableName();
    log.debug("Checking if Cassandra table {}.{} exists for request {}", keyspace, table, context);
    try {
        final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace);
        if (keyspaceMetadata == null) {
            return false;
        }
        final boolean exists = keyspaceMetadata.getTable(table) != null;
        log.debug("Cassandra table {}.{} {} for request {}", keyspace, table, exists ? "exists" : "doesn't exist", context);
        return exists;
    } catch (final DriverException de) {
        log.error(de.getMessage(), de);
        throw this.getExceptionMapper().toConnectorException(de, name);
    }
}
Also used : DriverException(com.datastax.driver.core.exceptions.DriverException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Aggregations

KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)83 TableMetadata (com.datastax.driver.core.TableMetadata)40 ImmutableList (com.google.common.collect.ImmutableList)20 DriverException (com.datastax.driver.core.exceptions.DriverException)15 Cluster (com.datastax.driver.core.Cluster)14 AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)12 Map (java.util.Map)12 ArrayList (java.util.ArrayList)11 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)10 HashMap (java.util.HashMap)10 List (java.util.List)10 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)9 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)9 Metadata (com.datastax.driver.core.Metadata)9 Session (com.datastax.driver.core.Session)9 Set (java.util.Set)9 PrestoException (com.facebook.presto.spi.PrestoException)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)6 Statement (com.datastax.driver.core.Statement)6