com.datastax.driver.core.RegularStatement Java Examples

The following examples show how to use com.datastax.driver.core.RegularStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraUtil.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * Method to create the cassandra update query.
 *
 * @param primaryKey map representing the composite primary key.
 * @param nonPKRecord map contains the fields that has to update.
 * @param keyspaceName cassandra keyspace name.
 * @param tableName cassandra table name.
 * @return RegularStatement.
 */
public static RegularStatement createUpdateQuery(
    Map<String, Object> primaryKey,
    Map<String, Object> nonPKRecord,
    String keyspaceName,
    String tableName) {

  Update update = QueryBuilder.update(keyspaceName, tableName);
  Assignments assignments = update.with();
  Update.Where where = update.where();
  nonPKRecord
      .entrySet()
      .stream()
      .forEach(
          x -> {
            assignments.and(QueryBuilder.set(x.getKey(), x.getValue()));
          });
  primaryKey
      .entrySet()
      .stream()
      .forEach(
          x -> {
            where.and(QueryBuilder.eq(x.getKey(), x.getValue()));
          });
  return where;
}
 
Example #2
Source File: MetricCassandraCollector.java    From realtime-analytics with GNU General Public License v2.0 6 votes vote down vote up
private void runBatchInsert(List<Insert> insertRequest) {
    try {
        Batch batch;
        if (config.getLoggedBatch()) {
            batch = QueryBuilder.batch(insertRequest
                    .toArray(new RegularStatement[insertRequest.size()]));
        } else {
            batch = QueryBuilder.unloggedBatch(insertRequest
                    .toArray(new RegularStatement[insertRequest.size()]));
        }
        totalCassandraInsertRequest.addAndGet(insertRequest.size());
        ResultSetFuture future = cassandraSession.executeAsync(batch);
        CallBackListener listener = new CallBackListener(future, null);
        future.addListener(listener, pool);
        incrementBatchInsertCounter();
        pendingRequestCounter.incrementAndGet();
    } catch (Throwable ex) {
        LOGGER.error("Error publising metrics in MetricCassandraCollector:" + ex.getMessage());
        cassandraErrorCount.increment();
        registerError(ex);
    } finally {
        insertRequest.clear();
    }
}
 
Example #3
Source File: MetricCassandraCollector.java    From realtime-analytics with GNU General Public License v2.0 6 votes vote down vote up
private void runBatchUpdate(List<Update> updateRequest) {
    try {
        Batch batch;

        if (config.getLoggedBatch()) {
            batch = QueryBuilder.batch(updateRequest
                    .toArray(new RegularStatement[updateRequest.size()]));
        } else {
            batch = QueryBuilder.unloggedBatch(updateRequest
                    .toArray(new RegularStatement[updateRequest.size()]));
        }
        totalCassandraUpdateRequest.addAndGet(updateRequest.size());
        ResultSetFuture future = cassandraSession.executeAsync(batch);
        CallBackListener listener = new CallBackListener(future, null);
        future.addListener(listener, pool);
        incrementBatchUpdateCounter();
        pendingRequestCounter.incrementAndGet();
    } catch (Throwable ex) {
        LOGGER.error("Error publising metrics in MetricCassandraCollector:" + ex.getMessage());
        cassandraErrorCount.increment();
        registerError(ex);
    } finally {
        updateRequest.clear();
    }
}
 
Example #4
Source File: CassandraIndexer.java    From newts with Apache License 2.0 6 votes vote down vote up
@Override
public void delete(final Context context, final Resource resource) {
    final Timer.Context ctx = m_deleteTimer.time();

    final ConsistencyLevel writeConsistency = m_contextConfigurations.getWriteConsistency(context);

    final List<RegularStatement> statements = Lists.newArrayList();
    definitelyUnindexResource(statements, context, resource, writeConsistency);
    definitelyUnindexResourceAttributes(statements, context, resource, writeConsistency);
    definitelyRemoveMetricName(statements, context, resource, writeConsistency);

    try {
        if (!statements.isEmpty()) {
            m_session.execute(batch(statements.toArray(new RegularStatement[statements.size()])));
        }

        m_cache.delete(context, resource);
    } finally {
        ctx.stop();
    }
}
 
Example #5
Source File: IOContainerTest.java    From blueflood with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {

    // mock DatastaxIO.getSession() & Session
    PowerMockito.mockStatic( DatastaxIO.class );
    Session mockSession = mock( Session.class );
    when( DatastaxIO.getSession()).thenReturn(mockSession);
    PreparedStatement mockPreparedStatement = mock( PreparedStatement.class );
    when( mockSession.prepare( any( RegularStatement.class ) ) ).thenReturn( mockPreparedStatement );
    when( mockSession.prepare( anyString() ) ).thenReturn(mockPreparedStatement);
    when( mockPreparedStatement.setConsistencyLevel(any(ConsistencyLevel.class)) ).thenReturn( mockPreparedStatement );
}
 
Example #6
Source File: CassandraStatementExecuteQueryInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private String retrieveSql(Object args0) {
    if (args0 instanceof BoundStatement) {
        return ((BoundStatement) args0).preparedStatement().getQueryString();
    } else if (args0 instanceof RegularStatement) {
        return ((RegularStatement) args0).getQueryString();
    } else if (args0 instanceof WrappedStatementGetter) {
        return retrieveWrappedStatement((WrappedStatementGetter) args0);
    } else if (args0 instanceof BatchStatement) {
        // we could unroll all the batched statements and append ; between them if need be but it could be too long.
        return null;
    } else if (args0 instanceof String) {
        return (String) args0;
    }
    return null;
}
 
Example #7
Source File: CassandraPreparedStatementCreateInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
protected void prepareAfterTrace(Object target, Object[] args, Object result, Throwable throwable) {
    final boolean success = InterceptorUtils.isSuccess(throwable);
    if (success) {
        if (target instanceof DatabaseInfoAccessor) {
            // set databaseInfo to PreparedStatement only when
            // preparedStatement is generated successfully.
            DatabaseInfo databaseInfo = ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo();
            if (databaseInfo != null) {
                if (result instanceof DatabaseInfoAccessor) {
                    ((DatabaseInfoAccessor) result)._$PINPOINT$_setDatabaseInfo(databaseInfo);
                }
            }
        }
        if (result instanceof ParsingResultAccessor) {
            String sql;
            if (args[0] instanceof RegularStatement) {
                sql = ((RegularStatement) args[0]).getQueryString();
            } else {
                // we have string
                sql = (String) args[0];
            }
            ParsingResult parsingResult = traceContext.parseSql(sql);
            if (parsingResult != null) {
                ((ParsingResultAccessor) result)._$PINPOINT$_setParsingResult(parsingResult);
            } else {
                if (logger.isErrorEnabled()) {
                    logger.error("sqlParsing fail. parsingResult is null sql:{}", sql);
                }
            }
        }
    }
}
 
Example #8
Source File: CassandraIndexer.java    From newts with Apache License 2.0 5 votes vote down vote up
@Override
public RegularStatement toStatement() {
    LOG.trace("Inserting attribute in context: '{}' with resource id: '{}' with name: '{}' and value: '{}'",
            m_context, m_resourceId, m_field, m_value);
    return insertInto(Constants.Schema.T_ATTRS)
        .value(Constants.Schema.C_ATTRS_CONTEXT, m_context.getId())
        .value(Constants.Schema.C_ATTRS_RESOURCE, m_resourceId)
        .value(Constants.Schema.C_ATTRS_ATTR, m_field)
        .value(Constants.Schema.C_ATTRS_VALUE, m_value)
        .using(ttl(m_ttl));
}
 
Example #9
Source File: CassandraIndexer.java    From newts with Apache License 2.0 5 votes vote down vote up
@Override
public RegularStatement toStatement() {
    LOG.trace("Inserting metric in context: '{}' with resource id: '{}' with name: '{}'",
            m_context, m_resourceId, m_metric);
    return insertInto(Constants.Schema.T_METRICS)
            .value(Constants.Schema.C_METRICS_CONTEXT, m_context.getId())
            .value(Constants.Schema.C_METRICS_RESOURCE, m_resourceId)
            .value(Constants.Schema.C_METRICS_NAME, m_metric)
            .using(ttl(m_ttl));
}
 
Example #10
Source File: CassandraIndexer.java    From newts with Apache License 2.0 5 votes vote down vote up
private void definitelyRemoveMetricName(List<RegularStatement> statement, Context context, Resource resource, ConsistencyLevel writeConsistency) {
    RegularStatement delete = QueryBuilder.delete().from(Constants.Schema.T_METRICS)
            .where(QueryBuilder.eq(Constants.Schema.C_METRICS_CONTEXT, context.getId()))
            .and(QueryBuilder.eq(Constants.Schema.C_METRICS_RESOURCE, resource.getId()));
    delete.setConsistencyLevel(writeConsistency);
    statement.add(delete);
}
 
Example #11
Source File: CassandraIndexer.java    From newts with Apache License 2.0 5 votes vote down vote up
private void definitelyUnindexResourceAttributes(List<RegularStatement> statement, Context context, Resource resource, ConsistencyLevel writeConsistency) {
    if (!resource.getAttributes().isPresent()) {
        return;
    }

    for (Entry<String, String> field : resource.getAttributes().get().entrySet()) {
        // Search unindexing
        RegularStatement delete = QueryBuilder.delete().from(Constants.Schema.T_TERMS)
                .where(QueryBuilder.eq(Constants.Schema.C_TERMS_CONTEXT, context.getId()))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_FIELD, Constants.DEFAULT_TERM_FIELD))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_VALUE, field.getValue()))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_RESOURCE, resource.getId()));
        delete.setConsistencyLevel(writeConsistency);
        statement.add(delete);
        delete = QueryBuilder.delete().from(Constants.Schema.T_TERMS)
                .where(QueryBuilder.eq(Constants.Schema.C_TERMS_CONTEXT, context.getId()))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_FIELD, field.getKey()))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_VALUE, field.getValue()))
                .and(QueryBuilder.eq(Constants.Schema.C_TERMS_RESOURCE, resource.getId()));
        delete.setConsistencyLevel(writeConsistency);
        statement.add(delete);
        // Storage
        delete = QueryBuilder.delete().from(Constants.Schema.T_ATTRS)
                .where(QueryBuilder.eq(Constants.Schema.C_ATTRS_CONTEXT, context.getId()))
                .and(QueryBuilder.eq(Constants.Schema.C_ATTRS_RESOURCE, resource.getId()))
                .and(QueryBuilder.eq(Constants.Schema.C_ATTRS_ATTR, field.getKey()));
        delete.setConsistencyLevel(writeConsistency);
        statement.add(delete);
    }
}
 
Example #12
Source File: CassandraIndexer.java    From newts with Apache License 2.0 5 votes vote down vote up
private void definitelyUnindexResource(List<RegularStatement> statement, Context context, Resource resource, ConsistencyLevel writeConsistencyLevel) {
    for (String s : m_resourceIdSplitter.splitIdIntoElements(resource.getId())) {
        RegularStatement delete = QueryBuilder.delete()
            .from(Constants.Schema.T_TERMS)
            .where(QueryBuilder.eq(Constants.Schema.C_TERMS_CONTEXT, context.getId()))
            .and(QueryBuilder.eq(Constants.Schema.C_TERMS_FIELD, Constants.DEFAULT_TERM_FIELD))
            .and(QueryBuilder.eq(Constants.Schema.C_TERMS_VALUE, s))
            .and(QueryBuilder.eq(Constants.Schema.C_TERMS_RESOURCE, resource.getId()));
        delete.setConsistencyLevel(writeConsistencyLevel);
        statement.add(delete);
    }
    if (m_options.isHierarchicalIndexingEnabled()) {
        recursivelyUnindexResourceElements(statement, context, resource.getId(), writeConsistencyLevel);
    }
}
 
Example #13
Source File: Scenario.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public boolean test(Statement statement) {
    if (statement instanceof BoundStatement) {
        BoundStatement boundStatement = (BoundStatement) statement;
        return boundStatement.preparedStatement()
            .getQueryString()
            .startsWith(queryStringPrefix);
    }
    if (statement instanceof RegularStatement) {
        RegularStatement regularStatement = (RegularStatement) statement;
        return regularStatement.getQueryString()
            .startsWith(queryStringPrefix);
    }
    return false;
}
 
Example #14
Source File: CassandraIndexerTest.java    From newts with Apache License 2.0 4 votes vote down vote up
@Test
public void insertStatementsAreDeduplicatedWhenIndexingManySamples() {
    CassandraSession session = mock(CassandraSession.class);
    ArgumentCaptor<Statement> statementCaptor = ArgumentCaptor.forClass(Statement.class);
    when(session.executeAsync(statementCaptor.capture())).thenReturn(mock(ResultSetFuture.class));

    PreparedStatement statement = mock(PreparedStatement.class);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(session.prepare(any(RegularStatement.class))).thenReturn(statement);
    when(statement.bind()).thenReturn(boundStatement);
    when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);

    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder()
            .withHierarchicalIndexing(true)
            // Limit the batch size so we can accurately count the number of statements
            .withMaxBatchSize(1).build();

    MetricRegistry registry = new MetricRegistry();
    GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(2048, registry);
    CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, registry, options,
            new EscapableResourceIdSplitter(), new ContextConfigurations());

    Resource r = new Resource("snmp:1589:vmware5Cpu:2:vmware5Cpu");
    List<Sample> samples = Lists.newArrayList();
    samples.add(new Sample(Timestamp.now(), r, "CpuCostopSum", MetricType.GAUGE, new Gauge(0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuIdleSum", MetricType.GAUGE, new Gauge(19299.0)));  
    samples.add(new Sample(Timestamp.now(), r, "CpuMaxLdSum", MetricType.GAUGE, new Gauge(0)));  
    samples.add(new Sample(Timestamp.now(), r, "CpuOverlapSum", MetricType.GAUGE, new Gauge(5.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuRdySum", MetricType.GAUGE, new Gauge(41.0)));  
    samples.add(new Sample(Timestamp.now(), r, "CpuRunSum", MetricType.GAUGE, new Gauge(619.0)));  
    samples.add(new Sample(Timestamp.now(), r, "CpuSpwaitSum", MetricType.GAUGE, new Gauge(0))); 
    samples.add(new Sample(Timestamp.now(), r, "CpuSystemSum", MetricType.GAUGE, new Gauge(0))); 
    samples.add(new Sample(Timestamp.now(), r, "CpuUsagemhzAvg", MetricType.GAUGE, new Gauge(32.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuUsedSum", MetricType.GAUGE, new Gauge(299.0)));
    samples.add(new Sample(Timestamp.now(), r, "CpuWaitSum", MetricType.GAUGE, new Gauge(19343)));

    // Index the collection of samples
    indexer.update(samples);

    // Verify the number of exectuteAsync calls
    verify(session, times(20)).executeAsync(any(Statement.class));
}
 
Example #15
Source File: CassandraIndexerStressITCase.java    From newts with Apache License 2.0 4 votes vote down vote up
@Test
public void canIndexManyResources() {
    final int numResources = 20000;
    final int numSamplesPerResource = 3;

    // Setup the indexer
    ResultSetFuture future = mock(ResultSetFuture.class);
    CassandraSession session = mock(CassandraSession.class);
    when(session.executeAsync(any(Statement.class))).thenReturn(future);

    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    BoundStatement boundStatement = mock(BoundStatement.class);
    when(session.prepare(any(RegularStatement.class))).thenReturn(preparedStatement);
    when(preparedStatement.bind()).thenReturn(boundStatement);
    when(boundStatement.setString(any(String.class), any(String.class))).thenReturn(boundStatement);

    ContextConfigurations contexts = new ContextConfigurations();
    MetricRegistry metrics = new MetricRegistry();

    CassandraIndexingOptions options = new CassandraIndexingOptions.Builder()
            .withHierarchicalIndexing(true).build();

    ResourceIdSplitter resourceIdSplitter = new EscapableResourceIdSplitter();
    GuavaResourceMetadataCache cache = new GuavaResourceMetadataCache(numResources * 2, metrics);
    CassandraIndexer indexer = new CassandraIndexer(session, 0, cache, metrics, options,
            resourceIdSplitter, contexts);

    // Generate the resources and sample sets
    Resource resources[] = new Resource[numResources];
    List<List<Sample>> sampleSets = Lists.newArrayListWithCapacity(numResources);
    System.out.println("Building sample sets...");
    for (int i = 0; i < numResources; i++) {
        resources[i] = new Resource(String.format("snmp:%d:eth0-x:ifHcInOctets", i));
        List<Sample> samples = Lists.newArrayListWithCapacity(numSamplesPerResource);
        for (int j = 0; j < numSamplesPerResource; j++) {
            samples.add(new Sample(Timestamp.now(), resources[i], "y" + j, MetricType.COUNTER, new Counter(i * j)));
        }
        sampleSets.add(samples);
    };
    System.out.println("Done building sample sets.");

    // Index the resources and associated samples several times over
    for (int k = 0; k < 3; k++) {
        System.out.println("Indexing samples sets...");
        long start = System.currentTimeMillis();
        for (List<Sample> sampleSet : sampleSets) {
            indexer.update(sampleSet);
        }
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("Done indexing samples in : " + elapsed + " ms");
    }
}
 
Example #16
Source File: CassandraSessionImpl.java    From newts with Apache License 2.0 4 votes vote down vote up
public PreparedStatement prepare(RegularStatement statement) {
    try                           {  return m_session.prepare(statement);  }
    catch (DriverException excep) {  throw new CassandraException(excep);  } 
}
 
Example #17
Source File: RxSessionImpl.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<PreparedStatement> prepare(RegularStatement statement) {
    ListenableFuture<PreparedStatement> future = session.prepareAsync(statement);
    return ListenableFutureObservable.from(future, Schedulers.computation());
}
 
Example #18
Source File: RxSessionImpl.java    From hawkular-metrics with Apache License 2.0 4 votes vote down vote up
@Override
public Observable<PreparedStatement> prepare(RegularStatement statement, Scheduler scheduler) {
    ListenableFuture<PreparedStatement> future = session.prepareAsync(statement);
    return ListenableFutureObservable.from(future, scheduler);
}
 
Example #19
Source File: TestingSession.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public ListenableFuture<PreparedStatement> prepareAsync(RegularStatement statement) {
    return delegate.prepareAsync(statement);
}
 
Example #20
Source File: TestingSession.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public PreparedStatement prepare(RegularStatement statement) {
    return delegate.prepare(statement);
}
 
Example #21
Source File: CassandraSession.java    From presto with Apache License 2.0 4 votes vote down vote up
public PreparedStatement prepare(RegularStatement statement)
{
    return executeWithSession(session -> session.prepare(statement));
}
 
Example #22
Source File: RxSession.java    From hawkular-metrics with Apache License 2.0 2 votes vote down vote up
/**
 * Asynchronously prepare a query and emit the corresponding {@link PreparedStatement} on the
 * {@link Schedulers#computation()} scheduler.
 *
 * @param statement the {@link RegularStatement} to prepare
 *
 * @return an {@link Observable} emitting just one {@link PreparedStatement} item
 * @see Session#prepareAsync(RegularStatement)
 */
Observable<PreparedStatement> prepare(RegularStatement statement);
 
Example #23
Source File: RxSession.java    From hawkular-metrics with Apache License 2.0 2 votes vote down vote up
/**
 * Asynchronously prepare a query and emit the corresponding {@link PreparedStatement} on the specified
 * {@code scheduler}.
 *
 * @param statement the {@link RegularStatement} to prepare
 *
 * @return an {@link Observable} emitting just one {@link PreparedStatement} item
 * @see Session#prepareAsync(RegularStatement)
 */
Observable<PreparedStatement> prepare(RegularStatement statement, Scheduler scheduler);
 
Example #24
Source File: CassandraSession.java    From newts with Apache License 2.0 votes vote down vote up
PreparedStatement prepare(RegularStatement statement);