Search in sources :

Example 1 with Create

use of com.datastax.driver.core.schemabuilder.Create in project java-driver by datastax.

the class FrameLengthTest method onTestContextInitialized.

@Override
public void onTestContextInitialized() {
    logger.info("Creating table {} with {} {}-byte blob columns", tableName, colCount, bytesPerCol);
    Random random = new Random();
    // Create table
    Create create = SchemaBuilder.createTable(tableName).addPartitionKey("k", DataType.cint()).addClusteringColumn("c", DataType.cint());
    for (int i = 0; i < colCount; i++) {
        create.addColumn("col" + i, DataType.blob());
    }
    execute(create.getQueryString());
    // build prepared statement.
    Insert insert = insertInto(tableName).value("k", bindMarker()).value("c", bindMarker());
    for (int i = 0; i < colCount; i++) {
        insert = insert.value("col" + i, bindMarker());
    }
    PreparedStatement prepared = session().prepare(insert);
    // Insert rows.
    logger.info("Inserting data for {} partitions.", partitionCount);
    for (int i = 0; i < partitionCount; i++) {
        logger.info("Inserting {} rows in partition {}", rowsPerPartitionCount, i);
        for (int r = 0; r < rowsPerPartitionCount; r++) {
            BoundStatement stmt = prepared.bind();
            stmt.setInt("k", i);
            stmt.setInt("c", r);
            for (int c = 0; c < colCount; c++) {
                byte[] b = new byte[bytesPerCol];
                random.nextBytes(b);
                ByteBuffer in = ByteBuffer.wrap(b);
                stmt.setBytes("col" + c, in);
            }
            session().execute(stmt);
        }
    }
    logger.info("Done loading {}", tableName);
}
Also used : Random(java.util.Random) Create(com.datastax.driver.core.schemabuilder.Create) Insert(com.datastax.driver.core.querybuilder.Insert) ByteBuffer(java.nio.ByteBuffer)

Example 2 with Create

use of com.datastax.driver.core.schemabuilder.Create in project storm by apache.

the class MapStateTest method createTable.

protected void createTable(String keyspace, String table, Column key, Column... fields) {
    Map<String, Object> replication = new HashMap<>();
    replication.put("class", SimpleStrategy.class.getSimpleName());
    replication.put("replication_factor", 1);
    Create createTable = SchemaBuilder.createTable(keyspace, table).ifNotExists().addPartitionKey(key.name, key.type);
    for (Column field : fields) {
        createTable.addColumn(field.name, field.type);
    }
    logger.info(createTable.toString());
    session.execute(createTable);
}
Also used : HashMap(java.util.HashMap) Create(com.datastax.driver.core.schemabuilder.Create) SimpleStrategy(org.apache.cassandra.locator.SimpleStrategy)

Example 3 with Create

use of com.datastax.driver.core.schemabuilder.Create in project pentaho-cassandra-plugin by pentaho.

the class DriverKeyspace method createTable.

@Override
public boolean createTable(String tableName, RowMetaInterface rowMeta, List<Integer> keyIndexes, String createTableWithClause, LogChannelInterface log) throws Exception {
    Create createTable = SchemaBuilder.createTable(tableName);
    for (int i = 0; i < rowMeta.size(); i++) {
        if (!keyIndexes.contains(i)) {
            ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
            createTable.addColumn(valueMeta.getName(), getDataType(valueMeta));
        } else {
            ValueMetaInterface key = rowMeta.getValueMeta(i);
            createTable.addPartitionKey(key.getName(), CassandraUtils.getCassandraDataTypeFromValueMeta(key));
        }
    }
    if (!Utils.isEmpty(createTableWithClause)) {
        StringBuilder cql = new StringBuilder(createTable.toString());
        if (!createTableWithClause.toLowerCase().trim().startsWith("with")) {
            cql.append(" WITH ");
        }
        cql.append(createTableWithClause);
        getSession().execute(cql.toString());
    } else {
        getSession().execute(createTable);
    }
    return true;
}
Also used : Create(com.datastax.driver.core.schemabuilder.Create) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

Create (com.datastax.driver.core.schemabuilder.Create)3 Insert (com.datastax.driver.core.querybuilder.Insert)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 SimpleStrategy (org.apache.cassandra.locator.SimpleStrategy)1 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)1