Search in sources :

Example 1 with Batch

use of com.datastax.driver.core.querybuilder.Batch in project java-driver by datastax.

the class LargeDataTest method testWideBatchRows.

/*
     * Test a batch that writes a row of size 4,000
     * @param c The cluster object
     * @param key The key value that will receive the data
     * @throws Throwable
     */
private void testWideBatchRows(int key) throws Throwable {
    // Write data
    Batch q = batch();
    for (int i = 0; i < 4000; ++i) {
        q = q.add(insertInto("wide_batch_rows").value("k", key).value("i", i));
    }
    session().execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM));
    // Read data
    ResultSet rs = session().execute(select("i").from("wide_batch_rows").where(eq("k", key)));
    // Verify data
    int i = 0;
    for (Row row : rs) {
        assertTrue(row.getInt("i") == i++);
    }
}
Also used : Batch(com.datastax.driver.core.querybuilder.Batch)

Example 2 with Batch

use of com.datastax.driver.core.querybuilder.Batch in project newts by OpenNMS.

the class CassandraSampleRepository method insert.

@Override
public void insert(Collection<Sample> samples, boolean calculateTimeToLive) {
    Timer.Context timer = m_insertTimer.time();
    Timestamp now = Timestamp.now();
    Batch batch = unloggedBatch();
    for (Sample m : samples) {
        int ttl = m_ttl;
        if (calculateTimeToLive) {
            ttl -= (int) (now.asSeconds() - m.getTimestamp().asSeconds());
            if (ttl <= 0) {
                LOG.debug("Skipping expired sample: {}", m);
                continue;
            }
        }
        Duration resourceShard = m_contextConfigurations.getResourceShard(m.getContext());
        Insert insert = insertInto(SchemaConstants.T_SAMPLES).value(SchemaConstants.F_CONTEXT, m.getContext().getId()).value(SchemaConstants.F_PARTITION, m.getTimestamp().stepFloor(resourceShard).asSeconds()).value(SchemaConstants.F_RESOURCE, m.getResource().getId()).value(SchemaConstants.F_COLLECTED, m.getTimestamp().asMillis()).value(SchemaConstants.F_METRIC_NAME, m.getName()).value(SchemaConstants.F_VALUE, ValueType.decompose(m.getValue()));
        // for any sample that has not specified them.
        if (m.getAttributes() != null) {
            insert.value(SchemaConstants.F_ATTRIBUTES, m.getAttributes());
        }
        // Use the context specific consistency level
        insert.setConsistencyLevel(m_contextConfigurations.getWriteConsistency(m.getContext()));
        batch.add(insert.using(ttl(ttl)));
    }
    try {
        m_session.execute(batch);
        if (m_processorService != null) {
            m_processorService.submit(samples);
        }
        m_samplesInserted.mark(samples.size());
    } finally {
        timer.stop();
    }
}
Also used : Timer(com.codahale.metrics.Timer) Batch(com.datastax.driver.core.querybuilder.Batch) QueryBuilder.unloggedBatch(com.datastax.driver.core.querybuilder.QueryBuilder.unloggedBatch) Sample(org.opennms.newts.api.Sample) Duration(org.opennms.newts.api.Duration) Insert(com.datastax.driver.core.querybuilder.Insert) Timestamp(org.opennms.newts.api.Timestamp)

Example 3 with Batch

use of com.datastax.driver.core.querybuilder.Batch in project pentaho-cassandra-plugin by pentaho.

the class DriverCQLRowHandler method batchInsert.

public void batchInsert(RowMetaInterface inputMeta, Iterable<Object[]> rows, ITableMetaData tableMeta, String consistencyLevel, boolean insertFieldsNotInMetadata, LogChannelInterface log) throws Exception {
    String[] columnNames = getColumnNames(inputMeta);
    Batch batch = unloggedBatch ? QueryBuilder.unloggedBatch() : QueryBuilder.batch();
    if (!Utils.isEmpty(consistencyLevel)) {
        try {
            batch.setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
        } catch (Exception e) {
            log.logError(e.getLocalizedMessage(), e);
        }
    }
    List<Integer> toRemove = new ArrayList<>();
    if (!insertFieldsNotInMetadata) {
        for (int i = 0; i < columnNames.length; i++) {
            if (!tableMeta.columnExistsInSchema(columnNames[i])) {
                toRemove.add(i);
            }
        }
        if (toRemove.size() > 0) {
            columnNames = copyExcluding(columnNames, new String[columnNames.length - toRemove.size()], toRemove);
        }
    }
    for (Object[] row : rows) {
        Object[] values = toRemove.size() == 0 ? Arrays.copyOf(row, columnNames.length) : copyExcluding(row, new Object[columnNames.length], toRemove);
        Insert insert = QueryBuilder.insertInto(keyspace.getName(), tableMeta.getTableName());
        insert = ttlSec > 0 ? insert.using(QueryBuilder.ttl(ttlSec)).values(columnNames, values) : insert.values(columnNames, values);
        batch.add(insert);
    }
    if (batchInsertTimeout > 0) {
        try {
            getSession().executeAsync(batch).getUninterruptibly(batchInsertTimeout, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            log.logError(BaseMessages.getString(DriverCQLRowHandler.class, "DriverCQLRowHandler.Error.TimeoutReached"));
        }
    } else {
        getSession().execute(batch);
    }
}
Also used : Batch(com.datastax.driver.core.querybuilder.Batch) ArrayList(java.util.ArrayList) Insert(com.datastax.driver.core.querybuilder.Insert) KettleException(org.pentaho.di.core.exception.KettleException) TimeoutException(java.util.concurrent.TimeoutException) NotImplementedException(org.apache.commons.lang.NotImplementedException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

Batch (com.datastax.driver.core.querybuilder.Batch)3 Insert (com.datastax.driver.core.querybuilder.Insert)2 Timer (com.codahale.metrics.Timer)1 QueryBuilder.unloggedBatch (com.datastax.driver.core.querybuilder.QueryBuilder.unloggedBatch)1 ArrayList (java.util.ArrayList)1 TimeoutException (java.util.concurrent.TimeoutException)1 NotImplementedException (org.apache.commons.lang.NotImplementedException)1 Duration (org.opennms.newts.api.Duration)1 Sample (org.opennms.newts.api.Sample)1 Timestamp (org.opennms.newts.api.Timestamp)1 KettleException (org.pentaho.di.core.exception.KettleException)1