Package com.datastax.driver.core.querybuilder

Examples of com.datastax.driver.core.querybuilder.Batch


     * @param key The key value that will receive the data
     * @throws Throwable
     */
    private void testWideBatchRows(CCMBridge.CCMCluster c, int key) throws Throwable {
        // Write data
        Batch q = batch();
        for (int i = 0; i < 10000; ++i) {
            q = q.add(insertInto("wide_batch_rows").value("k", key).value("i", i));
        }
        c.session.execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM));

        // Read data
        ResultSet rs = c.session.execute(select("i").from("wide_batch_rows").where(eq("k", key)));

        // Verify data
View Full Code Here


        }
    }

    private void insertDataToCassandra(List<Object[]> existingData) throws Exception {
        List<ResultSetFuture> resultSetFutures = new ArrayList<ResultSetFuture>();
        Batch batch = QueryBuilder.batch();
        int batchSize = 0;

        //only need approximate TTL to speed up processing
        //given that each batch is processed within seconds, getting the
        //system time once per batch has minimal impact on the record retention
        long creationTimeMillis;
        long itemTTLSeconds;
        long currentTimeMillis = System.currentTimeMillis();
        long expectedTTLMillis = MigrationTable.RAW.getTTLinMilliseconds();

        for (Object[] rawDataPoint : existingData) {
            creationTimeMillis = Long.parseLong(rawDataPoint[MigrationQuery.TIMESTAMP_INDEX].toString());
            itemTTLSeconds = (expectedTTLMillis - currentTimeMillis + creationTimeMillis) / 1000l;

            if (itemTTLSeconds > 0) {
                int scheduleId = Integer.parseInt(rawDataPoint[MigrationQuery.SCHEDULE_INDEX].toString());
                Date creationTime = new Date(creationTimeMillis);

                batch.add(QueryBuilder.insertInto(MetricsTable.RAW.toString()).value("schedule_id", scheduleId)
                    .value("time", creationTime)
                    .value("value", Double.parseDouble(rawDataPoint[MigrationQuery.VALUE_INDEX].toString()))
                    .using(ttl((int) itemTTLSeconds)));
                batchSize++;
View Full Code Here

        return telemetry;
    }

    private void insertDataToCassandra(List<Object[]> existingData) throws Exception {
        List<ResultSetFuture> resultSetFutures = new ArrayList<ResultSetFuture>();
        Batch batch = QueryBuilder.batch();
        int batchSize = 0;

        //only need approximate TTL to speed up processing
        //given that each batch is processed within seconds, getting the
        //system time once per batch has minimal impact on the record retention
        long creationTimeMillis;
        long itemTTLSeconds;
        long currentTimeMillis = System.currentTimeMillis();
        long expectedTTLMillis = migrationTable.getTTLinMilliseconds();

        for (Object[] rawMeasurement : existingData) {
            creationTimeMillis = Long.parseLong(rawMeasurement[MigrationQuery.TIMESTAMP_INDEX].toString());
            itemTTLSeconds = (expectedTTLMillis - currentTimeMillis + creationTimeMillis) / 1000l;

            if (itemTTLSeconds > 0) {
                int scheduleId = Integer.parseInt(rawMeasurement[MigrationQuery.SCHEDULE_INDEX].toString());
                Date time = new Date(creationTimeMillis);

                batch.add(QueryBuilder.insertInto(MetricsTable.AGGREGATE.toString())
                    .value("schedule_id", scheduleId)
                    .value("bucket", migrationTable.getMigrationBucket().toString())
                    .value("time", time)
                    .value("avg", Double.parseDouble(rawMeasurement[MigrationQuery.VALUE_INDEX].toString()))
                    .value("min", Double.parseDouble(rawMeasurement[MigrationQuery.MIN_VALUE_INDEX].toString()))
View Full Code Here

        return new CqlBatchContext();
    }

    @Override
    public void applyBatch(BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);
        List<Object> bindArguments = ((CqlBatchContext)batchContext).getBindArguments();
        if (bindArguments.isEmpty()) {
            session.execute(batch.getQueryString());
        } else {
            session.execute(session.prepare(batch.getQueryString()).bind(bindArguments.toArray()));
        }
        ((CqlBatchContext)batchContext).reset();
    }
View Full Code Here

    @Override
    public void writeToPath(K rowKey,
                            Path path,
                            Object structuredValue,
                            BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);

        validateArgs(rowKey, path);
        Object simplifiedStructure = writeMapper.convertValue(structuredValue, Object.class);
        Map<Path,Object> pathMap = Collections.singletonMap(path, simplifiedStructure);
        Map<Path,Object> objectMap = Decomposer.get().decompose(pathMap);

        batch = batchContext == null ? batch() : batch;
        List<Object> bindArguments = batchContext == null ?
                                        new ArrayList<Object>() :
                                        ((CqlBatchContext)batchContext).getBindArguments();

        for (Map.Entry<Path,Object> entry : objectMap.entrySet()) {
            batch.add(insertStatement);

            String stringValue = StructureConverter.get().toString(entry.getValue());

            bindArguments.add(rowKey);
            bindArguments.add(entry.getKey().toString());
            bindArguments.add(stringValue);
        }

        if (batchContext == null) {
            session.execute(session.prepare(batch.getQueryString()).bind(bindArguments.toArray()));
        }
    }
View Full Code Here

        deletePath(rowKey, path, null);
    }

    @Override
    public void deletePath(K rowKey, Path path, BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);

        validateArgs(rowKey, path);

        // converting from a string and back normalizes the path, e.g. makes sure ends with the delimiter character
        String start = path.toString();
        String finish = getFinishString(start);

        // would like to just do a delete with a where clause, but unfortunately Cassandra can't do that in CQL (either)
        // with >= and <=

        // Since the path column is in the primary key, we need to just delete whole rows.

        Object[] args = {rowKey,start,finish};
        ResultSet resultSet = session.execute(readForDeleteQuery.bind(args));
        if (resultSet.isExhausted()) {
            // not found
            return;
        }

        Delete deleteStatement = delete().from(tableName);
        deleteStatement
                .where(eq(partitionKeyColumnName, rowKey))
                .and(eq(pathColumnName, bindMarker()));

        batch = batchContext == null ? batch() : batch;
        List<Object> bindArguments = batchContext == null ?
                new ArrayList<Object>() :
                ((CqlBatchContext)batchContext).getBindArguments();

        for (Row row : resultSet) {
            String pathToDelete = row.getString(0);
            batch.add(deleteStatement);
            bindArguments.add(pathToDelete);
        }

        if (batchContext == null) {
          session.execute(session.prepare(batch.getQueryString()).bind(bindArguments.toArray()));
        }
    }
View Full Code Here

        return new CqlBatchContext();
    }

    @Override
    public void applyBatch(BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);
        List<Object> bindArguments = ((CqlBatchContext)batchContext).getBindArguments();
        Query query;
        if (bindArguments.isEmpty()) {
            query = new SimpleStatement(batch.getQueryString());
        } else {
            query = session.prepare(batch.getQueryString()).bind(bindArguments.toArray());
        }
        query.setConsistencyLevel(defaultConsistencyLevel);
        session.execute(query);
        ((CqlBatchContext)batchContext).reset();
    }
View Full Code Here

    @Override
    public void writeToPath(K rowKey,
                            Path path,
                            Object structuredValue,
                            BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);

        validateArgs(rowKey, path);
        Object simplifiedStructure = writeMapper.convertValue(structuredValue, Object.class);
        Map<Path,Object> pathMap = Collections.singletonMap(path, simplifiedStructure);
        Map<Path,Object> objectMap = Decomposer.get().decompose(pathMap);

        batch = batchContext == null ? batch() : batch;
        List<Object> bindArguments = batchContext == null ?
                                        new ArrayList<Object>() :
                                        ((CqlBatchContext)batchContext).getBindArguments();
        Statement insertStatement = insertInto(tableName)
                .value(partitionKeyColumnName, bindMarker())
                .value(pathColumnName, bindMarker())
                .value(valueColumnName, bindMarker())
                .using(timestamp(getCurrentMicros()));
        insertStatement.setConsistencyLevel(defaultConsistencyLevel);


        for (Map.Entry<Path,Object> entry : objectMap.entrySet()) {
            batch.add(insertStatement);

            String stringValue = StructureConverter.get().toString(entry.getValue());

            bindArguments.add(rowKey);
            bindArguments.add(entry.getKey().toString());
            bindArguments.add(stringValue);
        }

        if (batchContext == null) {
            Query boundStatement = session.prepare(batch.getQueryString()).bind(bindArguments.toArray());
            boundStatement.setConsistencyLevel(defaultConsistencyLevel);
            session.execute(boundStatement);
        }
    }
View Full Code Here

        deletePath(rowKey, path, null);
    }

    @Override
    public void deletePath(K rowKey, Path path, BatchContext batchContext) {
        Batch batch = validateAndGetBatch(batchContext);

        validateArgs(rowKey, path);

        // converting from a string and back normalizes the path, e.g. makes sure ends with the delimiter character
        String start = path.toString();
        String finish = getFinishString(start);

        // would like to just do a delete with a where clause, but unfortunately Cassandra can't do that in CQL (either)
        // with >= and <=

        // Since the path column is in the primary key, we need to just delete whole rows.

        Object[] args = {rowKey,start,finish};
        ResultSet resultSet = session.execute(readForDeleteQuery.bind(args));
        if (resultSet.isExhausted()) {
            // not found
            return;
        }

        Delete deleteStatement = delete().from(tableName);
        deleteStatement
                .using(timestamp(getCurrentMicros()))
                .where(eq(partitionKeyColumnName, rowKey))
                .and(eq(pathColumnName, bindMarker()));

        batch = batchContext == null ? batch() : batch;
        List<Object> bindArguments = batchContext == null ?
                new ArrayList<Object>() :
                ((CqlBatchContext)batchContext).getBindArguments();

        for (Row row : resultSet) {
            String pathToDelete = row.getString(0);
            batch.add(deleteStatement);
            bindArguments.add(pathToDelete);
        }

        if (batchContext == null) {
            BoundStatement query = session.prepare(batch.getQueryString()).bind(bindArguments.toArray());
            query.setConsistencyLevel(defaultConsistencyLevel);
            session.execute(query);
        }
    }
View Full Code Here

      }
      return entities;
    }

    String tableName = getTableName(entities.get(0).getClass()).toCql();
    Batch b = insert ? createInsertBatchQuery(tableName, entities, options, cassandraConverter)
        : createUpdateBatchQuery(tableName, entities, options, cassandraConverter);
    execute(b);

    return entities;
  }
View Full Code Here

TOP

Related Classes of com.datastax.driver.core.querybuilder.Batch

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.