@Test
  public void testAlter() throws Exception {

    CreateTable desired =
        TableBuilder.create("test_keyspace", "test_table")
            .column("col1", "text")
            .column("col2", "bigint")
            .column("col3", "int")
            .column("col4", "text")
            .primaryKey("col1");

    TableMetadata existing = mock(TableMetadata.class);

    ColumnMetadata col1 = mock(ColumnMetadata.class);
    when(existing.getColumn(eq("col1"))).thenReturn(col1);
    when(col1.getType()).thenReturn(DataType.text());

    ColumnMetadata col2 = mock(ColumnMetadata.class);
    when(existing.getColumn(eq("col2"))).thenReturn(col2);
    when(col2.getType()).thenReturn(DataType.cint());

    List<AlterTable> statements = TableBuilder.alter(existing, desired);

    assertEquals(3, statements.size());
    assertEquals(
        "ALTER TABLE test_keyspace.test_table ALTER col2 TYPE bigint",
        statements.get(0).toString());
    assertEquals("ALTER TABLE test_keyspace.test_table ADD col3 int", statements.get(1).toString());
    assertEquals(
        "ALTER TABLE test_keyspace.test_table ADD col4 text", statements.get(2).toString());
  }
Exemple #2
0
    public List<String> getSchema(
        String sourceKeyspace, String destKeyspace, Collection<String> tables) {
      Cluster cluster = peer.getCluster();
      Metadata meta = cluster.getMetadata();
      KeyspaceMetadata kmeta = meta.getKeyspace(keyspace);
      Collection<TableMetadata> tmeta = kmeta.getTables();
      //	    List<String> drops = new ArrayList<>();
      List<String> created = new ArrayList<>();

      for (TableMetadata tab : tmeta) {
        if (tables.size() != 0) {
          String name = tab.getName();
          if (!tables.contains(name)) continue;
        }
        String s = tab.asCQLQuery();
        StringBuilder sb = new StringBuilder();
        sb.append("DROP TABLE IF EXISTS ")
            .append(destKeyspace)
            .append(".")
            .append(tab.getName())
            .append(";\n");

        created.add(sb.toString());
        created.add(s.replaceAll("CREATE TABLE " + sourceKeyspace, "CREATE TABLE " + destKeyspace));
      }
      return created;
    }
Exemple #3
0
 Update(TableMetadata table) {
   super(table);
   this.keyspace = table.getKeyspace().getName();
   this.table = table.getName();
   this.assignments = new Assignments(this);
   this.where = new Where(this);
   this.usings = new Options(this);
 }
  private Map<String, TableMetadata> fetchTableMetaData() {
    Map<String, TableMetadata> tableMetas = new HashMap<String, TableMetadata>();
    KeyspaceMetadata keyspaceMeta = cluster.getMetadata().getKeyspace(keyspaceName);

    Validator.validateTableTrue(
        keyspaceMeta != null, "Keyspace '%s' doest not exist or cannot be found", keyspaceName);

    for (TableMetadata tableMeta : keyspaceMeta.getTables()) {
      tableMetas.put(tableMeta.getName(), tableMeta);
    }
    return tableMetas;
  }
  public void validateForEntity(EntityMeta entityMeta, TableMetadata tableMetadata) {
    log.debug("Validate existing table {} for {}", tableMetadata.getName(), entityMeta);
    PropertyMeta idMeta = entityMeta.getIdMeta();
    if (entityMeta.isClusteredCounter()) {}

    validateTable(entityMeta, tableMetadata, idMeta);
  }
  private void validatePrimaryKeyComponents(
      TableMetadata tableMetadata, PropertyMeta idMeta, boolean partitionKey) {

    log.debug(
        "Validate existing primary key component from table {} against Achilles meta data {}",
        tableMetadata.getName(),
        idMeta);

    List<String> componentNames;
    List<Class<?>> componentClasses;
    if (partitionKey) {
      componentNames = idMeta.getPartitionComponentNames();
      componentClasses = idMeta.getPartitionComponentClasses();
    } else {
      componentNames = idMeta.getClusteringComponentNames();
      componentClasses = idMeta.getClusteringComponentClasses();
    }

    for (int i = 0; i < componentNames.size(); i++) {
      Class<?> componentClass = componentClasses.get(i);
      String componentName = componentNames.get(i);
      if (idMeta.isComponentTimeUUID(componentName)) {
        componentClass = InternalTimeUUID.class;
      }
      if (partitionKey)
        validatePartitionComponent(tableMetadata, componentName.toLowerCase(), componentClass);
      else validateClusteringComponent(tableMetadata, componentName.toLowerCase(), componentClass);
    }
  }
  private void validateClusteringComponent(
      TableMetadata tableMetaData, String columnName, Class<?> columnJavaType) {

    log.debug(
        "Validate existing clustering column {} from table {} against type {}",
        columnName,
        tableMetaData.getName(),
        columnJavaType);

    validateColumn(tableMetaData, columnName, columnJavaType, false);
    ColumnMetadata columnMetadata = tableMetaData.getColumn(columnName);
    Validator.validateBeanMappingTrue(
        hasColumnMeta(tableMetaData.getClusteringColumns(), columnMetadata),
        "Column '%s' of table '%s' should be a clustering key component",
        columnName,
        tableMetaData.getName());
  }
 @Before
 public void setUp() throws Exception {
   when(cluster.getMetadata().getKeyspace(keyspaceName)).thenReturn(keyspaceMeta);
   when(keyspaceMeta.getTables()).thenReturn(new ArrayList<TableMetadata>());
   when(keyspaceMeta.getTables()).thenReturn(asList(tableMeta));
   when(tableMeta.getName()).thenReturn("tableName");
   when(meta.config().getQualifiedTableName()).thenReturn("tableName");
   when(meta.config().isSchemaUpdateEnabled()).thenReturn(true);
 }
  protected void createTables(boolean dropTables, boolean dropUnused) {

    for (TableMetadata table :
        session.getCluster().getMetadata().getKeyspace(keyspaceName).getTables()) {
      if (dropTables) {
        if (dropUnused || mappingContext.usesTable(table)) {
          admin.dropTable(table.getName());
        }
      }
    }

    Collection<? extends CassandraPersistentEntity<?>> entities =
        converter.getMappingContext().getPersistentEntities();

    for (CassandraPersistentEntity<?> entity : entities) {
      admin.createTable(false, entity.getTableName(), entity.getType(), null /* TODO */);
    }
  }
Exemple #10
0
  private void validateColumn(
      TableMetadata tableMetaData, String columnName, Class<?> columnJavaType, boolean indexed) {

    log.debug(
        "Validate existing column {} from table {} against type {}",
        columnName,
        tableMetaData.getName(),
        columnJavaType);

    String tableName = tableMetaData.getName();
    ColumnMetadata columnMetadata = tableMetaData.getColumn(columnName);
    Name expectedType = toCQLType(columnJavaType);

    Validator.validateTableTrue(
        columnMetadata != null, "Cannot find column '%s' in the table '%s'", columnName, tableName);

    boolean columnIsIndexed = columnMetadata.getIndex() != null;

    Validator.validateTableFalse(
        (columnIsIndexed ^ indexed),
        "Column '%s' in the table '%s' is indexed (or not) whereas metadata indicates it"
            + " is (or not)",
        columnName,
        tableName);
    Name realType = columnMetadata.getType().getName();

    /*
     * See JIRA
     */
    if (realType == Name.CUSTOM) {
      realType = Name.BLOB;
    }

    Validator.validateTableTrue(
        expectedType == realType,
        "Column '%s' of table '%s' of type '%s' should be of type '%s' indeed",
        columnName,
        tableName,
        realType,
        expectedType);
  }
  private void createTableIfNecessary(RetentionTable table, KeyspaceMetadata metadata) {
    for (TableMetadata meta : metadata.getTables()) {
      logger.debug("Comparing " + meta.getName() + " with " + table.tableName());
      if (meta.getName().equalsIgnoreCase(table.tableName())) {
        return;
      }
    }

    StringBuilder query = new StringBuilder();
    query.append("CREATE TABLE ").append(table.tableName()).append(" (");
    query.append(COL_NAME).append(" text, ");
    query.append(COL_TIME).append(" bigint, ");
    query.append(COL_VALUE).append(" double, ");
    query.append("PRIMARY KEY (").append(COL_NAME).append(", ").append(COL_TIME).append(")");
    query.append(");");
    logger.debug("Creating table with query: <" + query.toString() + ">");
    try {
      session.execute(query.toString());
    } catch (AlreadyExistsException e) {
      // Some other gatherer might have already created the same table.
    }
  }
  CqlRecordWriter(Configuration conf) {
    this.conf = conf;
    this.queueSize =
        conf.getInt(ColumnFamilyOutputFormat.QUEUE_SIZE, 32 * FBUtilities.getAvailableProcessors());
    batchThreshold = conf.getLong(ColumnFamilyOutputFormat.BATCH_THRESHOLD, 32);
    this.clients = new HashMap<>();

    try {
      String keyspace = ConfigHelper.getOutputKeyspace(conf);
      try (Session client =
          CqlConfigHelper.getOutputCluster(ConfigHelper.getOutputInitialAddress(conf), conf)
              .connect(keyspace)) {
        ringCache = new NativeRingCache(conf);
        if (client != null) {
          TableMetadata tableMetadata =
              client
                  .getCluster()
                  .getMetadata()
                  .getKeyspace(client.getLoggedKeyspace())
                  .getTable(ConfigHelper.getOutputColumnFamily(conf));
          clusterColumns = tableMetadata.getClusteringColumns();
          partitionKeyColumns = tableMetadata.getPartitionKey();

          String cqlQuery = CqlConfigHelper.getOutputCql(conf).trim();
          if (cqlQuery.toLowerCase().startsWith("insert"))
            throw new UnsupportedOperationException(
                "INSERT with CqlRecordWriter is not supported, please use UPDATE/DELETE statement");
          cql = appendKeyWhereClauses(cqlQuery);
        } else {
          throw new IllegalArgumentException("Invalid configuration specified " + conf);
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  @Test
  public void should_not_add_counter_field_if_non_clustered_counter_entity() throws Exception {
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).propertyName("id").build();
    PropertyMeta counterPM = valueClass(Counter.class).type(COUNTER).propertyName("count").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(counterPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());
    when(meta.structure().isClusteredCounter()).thenReturn(false);

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verifyZeroInteractions(session);
  }
  @Test
  public void should_update_table_with_new_clustered_counter_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta counterPM = valueClass(Counter.class).type(COUNTER).cqlColumnName("count").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(counterPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());
    when(meta.structure().isClusteredCounter()).thenReturn(true);

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue()).isEqualTo("\n\tALTER TABLE tableName ADD count counter");
  }
  @Test
  public void should_update_table_with_new_map_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta mapStringPM =
        completeBean(Integer.class, String.class).type(MAP).cqlColumnName("preferences").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(mapStringPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue())
        .isEqualTo("\n\tALTER TABLE tableName ADD preferences map<int, text>");
  }
  @Test
  public void should_update_table_with_new_list_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta listStringPM =
        valueClass(String.class).type(LIST).cqlColumnName("list_string").build();

    when(meta.getAllMetasExceptId()).thenReturn(asList(listStringPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session).execute(stringCaptor.capture());
    assertThat(stringCaptor.getValue())
        .isEqualTo("\n\tALTER TABLE tableName ADD list_string list<text>");
  }
  @Test
  public void should_update_table_with_new_indexed_simple_field() throws Exception {
    // Given
    PropertyMeta idMeta = valueClass(Long.class).type(PARTITION_KEY).cqlColumnName("id").build();
    PropertyMeta longColPM = valueClass(Long.class).type(SIMPLE).cqlColumnName("longcol").build();
    longColPM.setIndexProperties(new IndexProperties("long_index", "longCol"));

    when(meta.getAllMetasExceptId()).thenReturn(asList(longColPM));
    when(meta.getIdMeta()).thenReturn(idMeta);
    when(tableMeta.getColumns()).thenReturn(Arrays.<ColumnMetadata>asList());

    // When
    updater.updateTableForEntity(session, meta, tableMeta);

    // Then
    verify(session, Mockito.times(2)).execute(stringCaptor.capture());
    final List<String> updates = stringCaptor.getAllValues();
    assertThat(updates.get(0)).isEqualTo("\n\tALTER TABLE tableName ADD longcol bigint");
    assertThat(updates.get(1)).isEqualTo("\n\tCREATE INDEX long_index ON tableName(longcol)");
  }
Exemple #18
0
  private void validateCollectionAndMapColumn(TableMetadata tableMetadata, PropertyMeta pm) {

    log.debug("Validate existing collection/map column {} from table {}");

    String columnName = pm.getPropertyName().toLowerCase();
    String tableName = tableMetadata.getName();
    ColumnMetadata columnMetadata = tableMetadata.getColumn(columnName);

    Validator.validateTableTrue(
        columnMetadata != null, "Cannot find column '%s' in the table '%s'", columnName, tableName);
    Name realType = columnMetadata.getType().getName();
    Name expectedValueType = toCQLType(pm.getValueClassForTableCreation());

    switch (pm.type()) {
      case LIST:
        Validator.validateTableTrue(
            realType == Name.LIST,
            "Column '%s' of table '%s' of type '%s' should be of type '%s' indeed",
            columnName,
            tableName,
            realType,
            Name.LIST);
        Name realListValueType = columnMetadata.getType().getTypeArguments().get(0).getName();
        Validator.validateTableTrue(
            realListValueType == expectedValueType,
            "Column '%s' of table '%s' of type 'List<%s>' should be of type 'List<%s>' indeed",
            columnName,
            tableName,
            realListValueType,
            expectedValueType);

        break;
      case SET:
        Validator.validateTableTrue(
            realType == Name.SET,
            "Column '%s' of table '%s' of type '%s' should be of type '%s' indeed",
            columnName,
            tableName,
            realType,
            Name.SET);
        Name realSetValueType = columnMetadata.getType().getTypeArguments().get(0).getName();

        Validator.validateTableTrue(
            realSetValueType == expectedValueType,
            "Column '%s' of table '%s' of type 'Set<%s>' should be of type 'Set<%s>' indeed",
            columnName,
            tableName,
            realSetValueType,
            expectedValueType);
        break;
      case MAP:
        Validator.validateTableTrue(
            realType == Name.MAP,
            "Column '%s' of table '%s' of type '%s' should be of type '%s' indeed",
            columnName,
            tableName,
            realType,
            Name.MAP);

        Name expectedMapKeyType = toCQLType(pm.getKeyClass());
        Name realMapKeyType = columnMetadata.getType().getTypeArguments().get(0).getName();
        Name realMapValueType = columnMetadata.getType().getTypeArguments().get(1).getName();
        Validator.validateTableTrue(
            realMapKeyType == expectedMapKeyType,
            "Column %s' of table '%s' of type 'Map<%s,?>' should be of type 'Map<%s,?>' indeed",
            columnName,
            tableName,
            realMapKeyType,
            expectedMapKeyType);

        Validator.validateTableTrue(
            realMapValueType == expectedValueType,
            "Column '%s' of table '%s' of type 'Map<?,%s>' should be of type 'Map<?,%s>' indeed",
            columnName,
            tableName,
            realMapValueType,
            expectedValueType);
        break;
      default:
        break;
    }
  }
Exemple #19
0
  public void validateAchillesCounter(KeyspaceMetadata keyspaceMetaData, String keyspaceName) {
    log.debug("Validate existing Achilles Counter table");
    Name textTypeName = text().getName();
    Name counterTypeName = counter().getName();

    TableMetadata tableMetaData = keyspaceMetaData.getTable(CQL_COUNTER_TABLE);
    Validator.validateTableTrue(
        tableMetaData != null,
        "Cannot find table '%s' from keyspace '%s'",
        CQL_COUNTER_TABLE,
        keyspaceName);

    ColumnMetadata fqcnColumn = tableMetaData.getColumn(CQL_COUNTER_FQCN);
    Validator.validateTableTrue(
        fqcnColumn != null,
        "Cannot find column '%s' from table '%s'",
        CQL_COUNTER_FQCN,
        CQL_COUNTER_TABLE);
    Validator.validateTableTrue(
        fqcnColumn.getType().getName() == textTypeName,
        "Column '%s' of type '%s' should be of type '%s'",
        CQL_COUNTER_FQCN,
        fqcnColumn.getType().getName(),
        textTypeName);
    Validator.validateBeanMappingTrue(
        hasColumnMeta(tableMetaData.getPartitionKey(), fqcnColumn),
        "Column '%s' of table '%s' should be a partition key component",
        CQL_COUNTER_FQCN,
        CQL_COUNTER_TABLE);

    ColumnMetadata pkColumn = tableMetaData.getColumn(CQL_COUNTER_PRIMARY_KEY);
    Validator.validateTableTrue(
        pkColumn != null,
        "Cannot find column '%s' from table '%s'",
        CQL_COUNTER_PRIMARY_KEY,
        CQL_COUNTER_TABLE);
    Validator.validateTableTrue(
        pkColumn.getType().getName() == textTypeName,
        "Column '%s' of type '%s' should be of type '%s'",
        CQL_COUNTER_PRIMARY_KEY,
        pkColumn.getType().getName(),
        textTypeName);
    Validator.validateBeanMappingTrue(
        hasColumnMeta(tableMetaData.getPartitionKey(), pkColumn),
        "Column '%s' of table '%s' should be a partition key component",
        CQL_COUNTER_PRIMARY_KEY,
        CQL_COUNTER_TABLE);

    ColumnMetadata propertyNameColumn = tableMetaData.getColumn(CQL_COUNTER_PROPERTY_NAME);
    Validator.validateTableTrue(
        propertyNameColumn != null,
        "Cannot find column '%s' from table '%s'",
        CQL_COUNTER_PROPERTY_NAME,
        CQL_COUNTER_TABLE);
    Validator.validateTableTrue(
        propertyNameColumn.getType().getName() == textTypeName,
        "Column '%s' of type '%s' should be of type '%s'",
        CQL_COUNTER_PROPERTY_NAME,
        propertyNameColumn.getType().getName(),
        textTypeName);
    Validator.validateBeanMappingTrue(
        hasColumnMeta(tableMetaData.getClusteringColumns(), propertyNameColumn),
        "Column '%s' of table '%s' should be a clustering key component",
        CQL_COUNTER_PROPERTY_NAME,
        CQL_COUNTER_TABLE);

    ColumnMetadata counterValueColumn = tableMetaData.getColumn(CQL_COUNTER_VALUE);
    Validator.validateTableTrue(
        counterValueColumn != null,
        "Cannot find column '%s' from table '%s'",
        CQL_COUNTER_VALUE,
        CQL_COUNTER_TABLE);
    Validator.validateTableTrue(
        counterValueColumn.getType().getName() == counterTypeName,
        "Column '%s' of type '%s' should be of type '%s'",
        CQL_COUNTER_VALUE,
        counterValueColumn.getType().getName(),
        counterTypeName);
  }
 protected BuiltStatement(TableMetadata tableMetadata) {
   this.partitionKey = tableMetadata.getPartitionKey();
   this.routingKey = new ByteBuffer[tableMetadata.getPartitionKey().size()];
 }
Exemple #21
0
 Insert(TableMetadata table) {
   super(table);
   this.keyspace = table.getKeyspace().getName();
   this.table = table.getName();
   this.usings = new Options(this);
 }
 @Override
 public boolean usesTable(TableMetadata table) {
   return entitySetsByTableName.containsKey(table.getName());
 }