Search in sources :

Example 11 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project presto by prestodb.

the class NativeCassandraSession method getTableMetadata.

private TableMetadata getTableMetadata(SchemaTableName schemaTableName) {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    KeyspaceMetadata keyspaceMetadata = getCheckedKeyspaceMetadata(schemaName);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(tableName);
    if (tableMetadata != null) {
        return tableMetadata;
    }
    for (TableMetadata table : keyspaceMetadata.getTables()) {
        if (table.getName().equalsIgnoreCase(tableName)) {
            return table;
        }
    }
    throw new TableNotFoundException(schemaTableName);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 12 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method testAlter.

@Test
public void testAlter() {
    SchemaSync.drop(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
    SchemaSync.sync(keyspace, session, EntityWithIndexesV2.class);
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
    assertNotNull(tableMetadata);
    assertEquals("test_entity_index", tableMetadata.getName());
    assertEquals(6, tableMetadata.getColumns().size());
    ColumnMetadata columnMetadata = tableMetadata.getColumn("uuid");
    //assertNull(columnMetadata.getIndex());
    columnMetadata = tableMetadata.getColumn("email");
    //assertNotNull(columnMetadata.getIndex());
    //assertEquals("test_entity_email_idx", columnMetadata.getIndex().getName());
    columnMetadata = tableMetadata.getColumn("timestamp");
    //assertNull(columnMetadata.getIndex());
    columnMetadata = tableMetadata.getColumn("counter2");
    //assertEquals("test_entity_counter_idx", columnMetadata.getIndex().getName());	
    columnMetadata = tableMetadata.getColumn("name");
    assertNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("ref");
    assertNotNull(columnMetadata);
    columnMetadata = tableMetadata.getColumn("pets");
    assertNotNull(columnMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) Test(org.junit.Test)

Example 13 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSyncTest method testCreate.

@Test
public void testCreate() {
    EntityTypeParser.getEntityMetadata(EntityWithIndexes.class).markUnSynced(keyspace);
    SchemaSync.sync(keyspace, session, EntityWithIndexes.class);
    EntityTypeMetadata entityMetadata = EntityTypeParser.getEntityMetadata(EntityWithIndexes.class);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(entityMetadata.getTableName());
    assertNotNull(tableMetadata);
    assertEquals("test_entity_index", tableMetadata.getName());
    assertEquals(6, tableMetadata.getColumns().size());
    ColumnMetadata columnMetadata = tableMetadata.getColumn("uuid");
    //assertNull(columnMetadata.getIndex());
    columnMetadata = tableMetadata.getColumn("email");
    //assertNotNull(columnMetadata.getIndex());
    //assertEquals("test_entity_index_email_idx", columnMetadata.getIndex().getName());
    columnMetadata = tableMetadata.getColumn("timestamp");
    //assertNotNull(columnMetadata.getIndex());
    //assertEquals("test_entity_timestamp_idx", columnMetadata.getIndex().getName());	
    columnMetadata = tableMetadata.getColumn("counter");
    //assertNull(columnMetadata.getIndex());		
    columnMetadata = tableMetadata.getColumn("name");
    assertNotNull(columnMetadata);
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) EntityTypeMetadata(com.datastax.driver.mapping.meta.EntityTypeMetadata) EntityWithIndexes(com.datastax.driver.mapping.entity.EntityWithIndexes) Test(org.junit.Test)

Example 14 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSync method alterTableStatements.

/**
     * Compare TableMetadata against Entity metadata and generate alter statements if necessary.
     * <p>
     * Cannot alter clustered and primary key columns. 
     *
     * @param class the class to generate statements for or indexed
     * @return a new {@code List<RegularStatement>}.
     */
private static <T> List<RegularStatement> alterTableStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
    boolean doNotAddCols = false;
    boolean doDropCols = true;
    if (syncOptions != null) {
        List<SyncOptionTypes> opts = syncOptions.getOptions(entityMetadata.getEntityClass());
        doNotAddCols = opts.contains(SyncOptionTypes.DoNotAddColumns);
        doDropCols = !opts.contains(SyncOptionTypes.DoNotDropColumns);
    }
    List<RegularStatement> statements = new ArrayList<RegularStatement>();
    // get EntityTypeMetadata
    String table = entityMetadata.getTableName();
    // get TableMetadata - requires connection to cassandra
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    // build statements for a new column or a columns with changed datatype.
    for (EntityFieldMetaData field : entityMetadata.getFields()) {
        String column = field.getColumnName();
        String fieldType = field.getDataType().name();
        ColumnMetadata columnMetadata = tableMetadata.getColumn(column);
        String colIndex = null;
        /*if (columnMetadata!= null && columnMetadata.getIndex() != null) {
    			colIndex = columnMetadata.getIndex().getName();
    		}*/
        String fieldIndex = null;
        if (entityMetadata.getIndex(column) != null) {
            fieldIndex = entityMetadata.getIndex(column);
        }
        if (columnMetadata == null) {
            if (doNotAddCols)
                continue;
            // if column not exists in Cassandra then build add column Statement 
            String colType = fieldType;
            if (field.isGenericType()) {
                colType = field.getGenericDef();
            }
            AlterTable statement = new AlterTable.Builder().addColumn(keyspace, table, column, colType);
            statements.add(statement);
            if (fieldIndex != null) {
                statements.add(new DropIndex(column, fieldIndex));
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            }
        } else if (colIndex != null || fieldIndex != null) {
            if (colIndex == null) {
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            } else if (fieldIndex == null) {
                statements.add(new DropIndex(column, colIndex));
            } else if (!"".equals(fieldIndex) && !fieldIndex.equals(colIndex)) {
                statements.add(new DropIndex(column, colIndex));
                statements.add(new CreateIndex(keyspace, table, column, fieldIndex));
            }
        } else if (!fieldType.equals(columnMetadata.getType().getName().name())) {
            // can't change datatype for clustered columns
            if (tableMetadata.getClusteringColumns().contains(columnMetadata)) {
                continue;
            }
            // can't change datatype for PK  columns
            if (tableMetadata.getPrimaryKey().contains(columnMetadata)) {
                continue;
            }
            // drop index if any
            /*if (columnMetadata.getIndex() != null) {
    				statements.add(new DropIndex(column, columnMetadata.getIndex().getName()));
    			}*/
            // alter column datatype
            statements.add(new AlterTable.Builder().alterColumn(keyspace, table, column, fieldType));
            // create index if any
            if (entityMetadata.getIndex(column) != null) {
                statements.add(new CreateIndex(keyspace, table, column, entityMetadata.getIndex(column)));
            }
        }
    }
    // column is in Cassandra but not in entity anymore
    if (doDropCols) {
        for (ColumnMetadata colmeta : tableMetadata.getColumns()) {
            colmeta.getName();
            boolean exists = false;
            for (EntityFieldMetaData field : entityMetadata.getFields()) {
                if (colmeta.getName().equalsIgnoreCase(field.getColumnName())) {
                    exists = true;
                    break;
                }
            }
            if (!exists) {
                AlterTable statement = new AlterTable.Builder().dropColumn(keyspace, table, colmeta.getName());
                statements.add(statement);
            }
        }
    }
    return statements;
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) EntityFieldMetaData(com.datastax.driver.mapping.meta.EntityFieldMetaData) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) RegularStatement(com.datastax.driver.core.RegularStatement) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Example 15 with TableMetadata

use of com.datastax.driver.core.TableMetadata in project cassandra-driver-mapping by valchkou.

the class SchemaSync method buildSyncStatements.

/**
     * Generate alter, drop or create statements for the given Entity
     *  
     * @param keyspace
     * @param session
     * @param entityMetadata
     * @param syncOptions
     * @return RegularStatements
     */
public static List<RegularStatement> buildSyncStatements(String keyspace, Session session, EntityTypeMetadata entityMetadata, SyncOptions syncOptions) {
    String table = entityMetadata.getTableName();
    session.execute("USE " + keyspace);
    Cluster cluster = session.getCluster();
    KeyspaceMetadata keyspaceMetadata = cluster.getMetadata().getKeyspace(keyspace);
    TableMetadata tableMetadata = keyspaceMetadata.getTable(table);
    List<RegularStatement> statements = new ArrayList<RegularStatement>();
    if (tableMetadata == null) {
        statements = createTableStatements(keyspace, entityMetadata);
    } else {
        statements = alterTableStatements(keyspace, session, entityMetadata, syncOptions);
    }
    return statements;
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) RegularStatement(com.datastax.driver.core.RegularStatement) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata)

Aggregations

TableMetadata (com.datastax.driver.core.TableMetadata)18 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)9 ColumnMetadata (com.datastax.driver.core.ColumnMetadata)8 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)8 Test (org.junit.Test)7 Cluster (com.datastax.driver.core.Cluster)3 DriverException (com.datastax.driver.core.exceptions.DriverException)3 DatabaseNotFoundException (com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException)3 ArrayList (java.util.ArrayList)3 RegularStatement (com.datastax.driver.core.RegularStatement)2 ImmutableList (com.google.common.collect.ImmutableList)2 TableInfo (com.netflix.metacat.common.server.connectors.model.TableInfo)2 com.datastax.driver.core (com.datastax.driver.core)1 EntityWithCompositeKey (com.datastax.driver.mapping.entity.EntityWithCompositeKey)1 EntityWithIndexes (com.datastax.driver.mapping.entity.EntityWithIndexes)1 EntityWithTimeUUID (com.datastax.driver.mapping.entity.EntityWithTimeUUID)1 EntityFieldMetaData (com.datastax.driver.mapping.meta.EntityFieldMetaData)1 PrestoException (com.facebook.presto.spi.PrestoException)1 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)1 QualifiedName (com.netflix.metacat.common.QualifiedName)1