Search in sources :

Example 21 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class SSTableExport method metadataFromSSTable.

/**
     * Construct table schema from info stored in SSTable's Stats.db
     *
     * @param desc SSTable's descriptor
     * @return Restored TableMetadata
     * @throws IOException when Stats.db cannot be read
     */
public static TableMetadata metadataFromSSTable(Descriptor desc) throws IOException {
    if (!desc.version.isCompatible())
        throw new IOException("Cannot process old and unsupported SSTable version.");
    EnumSet<MetadataType> types = EnumSet.of(MetadataType.STATS, MetadataType.HEADER);
    Map<MetadataType, MetadataComponent> sstableMetadata = desc.getMetadataSerializer().deserialize(desc, types);
    SerializationHeader.Component header = (SerializationHeader.Component) sstableMetadata.get(MetadataType.HEADER);
    IPartitioner partitioner = FBUtilities.newPartitioner(desc);
    TableMetadata.Builder builder = TableMetadata.builder("keyspace", "table").partitioner(partitioner);
    header.getStaticColumns().entrySet().stream().forEach(entry -> {
        ColumnIdentifier ident = ColumnIdentifier.getInterned(UTF8Type.instance.getString(entry.getKey()), true);
        builder.addStaticColumn(ident, entry.getValue());
    });
    header.getRegularColumns().entrySet().stream().forEach(entry -> {
        ColumnIdentifier ident = ColumnIdentifier.getInterned(UTF8Type.instance.getString(entry.getKey()), true);
        builder.addRegularColumn(ident, entry.getValue());
    });
    builder.addPartitionKeyColumn("PartitionKey", header.getKeyType());
    for (int i = 0; i < header.getClusteringTypes().size(); i++) {
        builder.addClusteringColumn("clustering" + (i > 0 ? i : ""), header.getClusteringTypes().get(i));
    }
    return builder.build();
}
Also used : MetadataComponent(org.apache.cassandra.io.sstable.metadata.MetadataComponent) TableMetadata(org.apache.cassandra.schema.TableMetadata) MetadataType(org.apache.cassandra.io.sstable.metadata.MetadataType) IOException(java.io.IOException) SerializationHeader(org.apache.cassandra.db.SerializationHeader) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) MetadataComponent(org.apache.cassandra.io.sstable.metadata.MetadataComponent)

Example 22 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class SchemaLoader method compositeIndexCFMD.

public static TableMetadata.Builder compositeIndexCFMD(String ksName, String cfName, boolean withRegularIndex, boolean withStaticIndex) throws ConfigurationException {
    // the withIndex flag exists to allow tests index creation
    // on existing columns
    TableMetadata.Builder builder = TableMetadata.builder(ksName, cfName).addPartitionKeyColumn("key", AsciiType.instance).addClusteringColumn("c1", AsciiType.instance).addRegularColumn("birthdate", LongType.instance).addRegularColumn("notbirthdate", LongType.instance).addStaticColumn("static", LongType.instance).compression(getCompressionParameters());
    Indexes.Builder indexes = Indexes.builder();
    if (withRegularIndex) {
        indexes.add(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("birthdate", true), IndexTarget.Type.VALUES)), cfName + "_birthdate_key_index", IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP));
    }
    if (withStaticIndex) {
        indexes.add(IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("static", true), IndexTarget.Type.VALUES)), cfName + "_static_index", IndexMetadata.Kind.COMPOSITES, Collections.EMPTY_MAP));
    }
    return builder.indexes(indexes.build());
}
Also used : IndexTarget(org.apache.cassandra.cql3.statements.IndexTarget) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 23 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class SchemaLoader method customIndexCFMD.

public static TableMetadata.Builder customIndexCFMD(String ksName, String cfName) {
    TableMetadata.Builder builder = TableMetadata.builder(ksName, cfName).isCompound(false).isDense(true).addPartitionKeyColumn("key", AsciiType.instance).addClusteringColumn("c1", AsciiType.instance).addRegularColumn("value", LongType.instance).compression(getCompressionParameters());
    IndexMetadata index = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(new ColumnIdentifier("value", true), IndexTarget.Type.VALUES)), cfName + "_value_index", IndexMetadata.Kind.CUSTOM, Collections.singletonMap(IndexTarget.CUSTOM_INDEX_OPTION_NAME, StubIndex.class.getName()));
    builder.indexes(Indexes.of(index));
    return builder;
}
Also used : IndexTarget(org.apache.cassandra.cql3.statements.IndexTarget) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 24 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class Util method makeSomePagingState.

public static PagingState makeSomePagingState(ProtocolVersion protocolVersion) {
    TableMetadata metadata = TableMetadata.builder("ks", "tbl").addPartitionKeyColumn("k", AsciiType.instance).addClusteringColumn("c1", AsciiType.instance).addClusteringColumn("c2", Int32Type.instance).addRegularColumn("myCol", AsciiType.instance).build();
    ByteBuffer pk = ByteBufferUtil.bytes("someKey");
    ColumnMetadata def = metadata.getColumn(new ColumnIdentifier("myCol", false));
    Clustering c = Clustering.make(ByteBufferUtil.bytes("c1"), ByteBufferUtil.bytes(42));
    Row row = BTreeRow.singleCellRow(c, BufferCell.live(def, 0, ByteBufferUtil.EMPTY_BYTE_BUFFER));
    PagingState.RowMark mark = PagingState.RowMark.create(metadata, row, protocolVersion);
    return new PagingState(pk, mark, 10, 0);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) ByteBuffer(java.nio.ByteBuffer) PagingState(org.apache.cassandra.service.pager.PagingState)

Example 25 with ColumnIdentifier

use of org.apache.cassandra.cql3.ColumnIdentifier in project cassandra by apache.

the class KeyspaceTest method testGetRowSingleColumn.

@Test
public void testGetRowSingleColumn() throws Throwable {
    String tableName = createTable("CREATE TABLE %s (a text, b int, c int, PRIMARY KEY (a, b))");
    for (int i = 0; i < 2; i++) execute("INSERT INTO %s (a, b, c) VALUES (?, ?, ?)", "0", i, i);
    final ColumnFamilyStore cfs = Keyspace.open(KEYSPACE).getColumnFamilyStore(tableName);
    for (int round = 0; round < 2; round++) {
        // slice with limit 1
        Row row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").withLimit(1).build());
        assertEquals(ByteBufferUtil.bytes(0), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).value());
        // fetch each row by name
        for (int i = 0; i < 2; i++) {
            row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").includeRow(i).build());
            assertEquals(ByteBufferUtil.bytes(i), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).value());
        }
        // fetch each row by slice
        for (int i = 0; i < 2; i++) {
            row = Util.getOnlyRow(Util.cmd(cfs, "0").columns("c").fromIncl(i).toIncl(i).build());
            assertEquals(ByteBufferUtil.bytes(i), row.getCell(cfs.metadata().getColumn(new ColumnIdentifier("c", false))).value());
        }
        if (round == 0)
            cfs.forceBlockingFlush();
    }
}
Also used : ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier) Row(org.apache.cassandra.db.rows.Row) Test(org.junit.Test)

Aggregations

ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)27 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)17 Test (org.junit.Test)10 Row (org.apache.cassandra.db.rows.Row)7 TableMetadata (org.apache.cassandra.schema.TableMetadata)7 IndexTarget (org.apache.cassandra.cql3.statements.IndexTarget)5 PartitionIterator (org.apache.cassandra.db.partitions.PartitionIterator)4 Cell (org.apache.cassandra.db.rows.Cell)4 PartitionUpdate (org.apache.cassandra.db.partitions.PartitionUpdate)3 RowIterator (org.apache.cassandra.db.rows.RowIterator)3 Iterables (com.google.common.collect.Iterables)2 ByteBuffer (java.nio.ByteBuffer)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 CellName (org.apache.cassandra.db.composites.CellName)2 IndexMetadata (org.apache.cassandra.schema.IndexMetadata)2 Iterators (com.google.common.collect.Iterators)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1