Search in sources :

Example 31 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata 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 32 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata 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 33 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project calcite by apache.

the class CassandraSchema method getKeyFields.

/**
 * Get all primary key columns from the underlying CQL table
 *
 * @return A list of field names that are part of the partition and clustering keys
 */
Pair<List<String>, List<String>> getKeyFields(String columnFamily, boolean view) {
    AbstractTableMetadata table;
    if (view) {
        table = getKeyspace().getMaterializedView(columnFamily);
    } else {
        table = getKeyspace().getTable(columnFamily);
    }
    List<ColumnMetadata> partitionKey = table.getPartitionKey();
    List<String> pKeyFields = new ArrayList<String>();
    for (ColumnMetadata column : partitionKey) {
        pKeyFields.add(column.getName());
    }
    List<ColumnMetadata> clusteringKey = table.getClusteringColumns();
    List<String> cKeyFields = new ArrayList<String>();
    for (ColumnMetadata column : clusteringKey) {
        cKeyFields.add(column.getName());
    }
    return Pair.of((List<String>) ImmutableList.copyOf(pKeyFields), (List<String>) ImmutableList.copyOf(cKeyFields));
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) ArrayList(java.util.ArrayList)

Example 34 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project calcite by apache.

the class CassandraSchema method addMaterializedViews.

/**
 * Add all materialized views defined in the schema to this column family
 */
private void addMaterializedViews() {
    // Close the hook use to get us here
    hook.close();
    for (MaterializedViewMetadata view : getKeyspace().getMaterializedViews()) {
        String tableName = view.getBaseTable().getName();
        StringBuilder queryBuilder = new StringBuilder("SELECT ");
        // Add all the selected columns to the query
        List<String> columnNames = new ArrayList<String>();
        for (ColumnMetadata column : view.getColumns()) {
            columnNames.add("\"" + column.getName() + "\"");
        }
        queryBuilder.append(Util.toString(columnNames, "", ", ", ""));
        queryBuilder.append(" FROM \"" + tableName + "\"");
        // Get the where clause from the system schema
        String whereQuery = "SELECT where_clause from system_schema.views " + "WHERE keyspace_name='" + keyspace + "' AND view_name='" + view.getName() + "'";
        queryBuilder.append(" WHERE " + session.execute(whereQuery).one().getString(0));
        // Parse and unparse the view query to get properly quoted field names
        String query = queryBuilder.toString();
        SqlParser.ConfigBuilder configBuilder = SqlParser.configBuilder();
        configBuilder.setUnquotedCasing(Casing.UNCHANGED);
        SqlSelect parsedQuery;
        try {
            parsedQuery = (SqlSelect) SqlParser.create(query, configBuilder.build()).parseQuery();
        } catch (SqlParseException e) {
            LOGGER.warn("Could not parse query {} for CQL view {}.{}", query, keyspace, view.getName());
            continue;
        }
        StringWriter stringWriter = new StringWriter(query.length());
        PrintWriter printWriter = new PrintWriter(stringWriter);
        SqlWriter writer = new SqlPrettyWriter(CalciteSqlDialect.DEFAULT, true, printWriter);
        parsedQuery.unparse(writer, 0, 0);
        query = stringWriter.toString();
        // Add the view for this query
        String viewName = "$" + getTableNames().size();
        SchemaPlus schema = parentSchema.getSubSchema(name);
        CalciteSchema calciteSchema = CalciteSchema.from(schema);
        List<String> viewPath = calciteSchema.path(viewName);
        schema.add(viewName, MaterializedViewTable.create(calciteSchema, query, null, viewPath, view.getName(), true));
    }
}
Also used : ColumnMetadata(com.datastax.driver.core.ColumnMetadata) SqlWriter(org.apache.calcite.sql.SqlWriter) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) ArrayList(java.util.ArrayList) SqlParser(org.apache.calcite.sql.parser.SqlParser) SchemaPlus(org.apache.calcite.schema.SchemaPlus) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) SqlSelect(org.apache.calcite.sql.SqlSelect) StringWriter(java.io.StringWriter) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) SqlPrettyWriter(org.apache.calcite.sql.pretty.SqlPrettyWriter) PrintWriter(java.io.PrintWriter)

Example 35 with ColumnMetadata

use of com.datastax.driver.core.ColumnMetadata in project DataX by alibaba.

the class CassandraReaderHelper method checkConfig.

public static void checkConfig(Configuration jobConfig, Cluster cluster) {
    ensureStringExists(jobConfig, Key.HOST);
    ensureStringExists(jobConfig, Key.KEYSPACE);
    ensureStringExists(jobConfig, Key.TABLE);
    ensureExists(jobConfig, Key.COLUMN);
    // /keyspace,table是否存在
    String keyspace = jobConfig.getString(Key.KEYSPACE);
    if (cluster.getMetadata().getKeyspace(keyspace) == null) {
        throw DataXException.asDataXException(CassandraReaderErrorCode.CONF_ERROR, String.format("配置信息有错误.keyspace'%s'不存在 .", keyspace));
    }
    String table = jobConfig.getString(Key.TABLE);
    TableMetadata tableMetadata = cluster.getMetadata().getKeyspace(keyspace).getTable(table);
    if (tableMetadata == null) {
        throw DataXException.asDataXException(CassandraReaderErrorCode.CONF_ERROR, String.format("配置信息有错误.表'%s'不存在 .", table));
    }
    List<String> columns = jobConfig.getList(Key.COLUMN, String.class);
    for (String name : columns) {
        if (name == null || name.isEmpty()) {
            throw DataXException.asDataXException(CassandraReaderErrorCode.CONF_ERROR, String.format("配置信息有错误.列信息中需要包含'%s'字段 .", Key.COLUMN_NAME));
        }
        if (name.startsWith(Key.WRITE_TIME)) {
            String colName = name.substring(Key.WRITE_TIME.length(), name.length() - 1);
            ColumnMetadata col = tableMetadata.getColumn(colName);
            if (col == null) {
                throw DataXException.asDataXException(CassandraReaderErrorCode.CONF_ERROR, String.format("配置信息有错误.列'%s'不存在 .", colName));
            }
        } else {
            ColumnMetadata col = tableMetadata.getColumn(name);
            if (col == null) {
                throw DataXException.asDataXException(CassandraReaderErrorCode.CONF_ERROR, String.format("配置信息有错误.列'%s'不存在 .", name));
            }
        }
    }
}
Also used : TableMetadata(com.datastax.driver.core.TableMetadata) ColumnMetadata(com.datastax.driver.core.ColumnMetadata)

Aggregations

ColumnMetadata (com.datastax.driver.core.ColumnMetadata)37 Test (org.testng.annotations.Test)11 TableMetadata (com.datastax.driver.core.TableMetadata)8 CassandraVersion (com.datastax.driver.core.utils.CassandraVersion)8 EntityTypeMetadata (com.datastax.driver.mapping.meta.EntityTypeMetadata)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 AbstractTableMetadata (com.datastax.driver.core.AbstractTableMetadata)2 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)2 ImmutableList (com.google.common.collect.ImmutableList)2 ByteBuffer (java.nio.ByteBuffer)2 BoundStatementMapper (org.simpleflatmapper.datastax.BoundStatementMapper)2 Cluster (com.datastax.driver.core.Cluster)1 DataType (com.datastax.driver.core.DataType)1 MaterializedViewMetadata (com.datastax.driver.core.MaterializedViewMetadata)1 RegularStatement (com.datastax.driver.core.RegularStatement)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