use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraIndexerStressITCase method canIndexManyResources.
@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");
}
}
use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraSampleRepository method cassandraSelect.
private Iterator<com.datastax.driver.core.Row> cassandraSelect(Context context, Resource resource, Timestamp start, Timestamp end) {
List<Future<ResultSet>> futures = Lists.newArrayList();
Duration resourceShard = m_contextConfigurations.getResourceShard(context);
Timestamp lower = start.stepFloor(resourceShard);
Timestamp upper = end.stepFloor(resourceShard);
for (Timestamp partition : new IntervalGenerator(lower, upper, resourceShard)) {
BoundStatement bindStatement = m_selectStatement.bind();
bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
bindStatement.setTimestamp("start", start.asDate());
bindStatement.setTimestamp("end", end.asDate());
// Use the context specific consistency level
bindStatement.setConsistencyLevel(m_contextConfigurations.getReadConsistency(context));
futures.add(m_session.executeAsync(bindStatement));
}
return new ConcurrentResultWrapper(futures);
}
use of com.datastax.driver.core.BoundStatement in project newts by OpenNMS.
the class CassandraSearcher method fetchResourceAttributes.
private ResultSetFuture fetchResourceAttributes(Context context, String resourceId, ConsistencyLevel readConsistency) {
BoundStatement bindStatement = m_selectAttributesStatement.bind();
bindStatement.setString(Schema.C_ATTRS_CONTEXT, context.getId());
bindStatement.setString(Schema.C_ATTRS_RESOURCE, resourceId);
bindStatement.setConsistencyLevel(readConsistency);
return m_session.executeAsync(bindStatement);
}
use of com.datastax.driver.core.BoundStatement in project pinpoint by naver.
the class CassandraDatastaxITBase method test.
@Test
public void test() throws Exception {
final int testId = 99;
final String testValue = "testValue";
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
Session myKeyspaceSession = null;
try {
myKeyspaceSession = cluster.connect(TEST_KEYSPACE);
// ===============================================
// Insert Data (PreparedStatement, BoundStatement)
PreparedStatement preparedStatement = myKeyspaceSession.prepare(CQL_INSERT);
BoundStatement boundStatement = new BoundStatement(preparedStatement);
boundStatement.bind(testId, testValue);
myKeyspaceSession.execute(boundStatement);
verifier.printCache();
// Cluster#connect(String)
Class<?> clusterClass = Class.forName("com.datastax.driver.core.Cluster");
Method connect = clusterClass.getDeclaredMethod("connect", String.class);
verifier.verifyTrace(event(CASSANDRA, connect, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
// SessionManager#prepare(String) OR AbstractSession#prepare(String)
Class<?> sessionClass;
try {
sessionClass = Class.forName("com.datastax.driver.core.AbstractSession");
} catch (ClassNotFoundException e) {
sessionClass = Class.forName("com.datastax.driver.core.SessionManager");
}
Method prepare = sessionClass.getDeclaredMethod("prepare", String.class);
verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
// SessionManager#execute(Statement) OR AbstractSession#execute(Statement)
Method execute = sessionClass.getDeclaredMethod("execute", Statement.class);
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
// ====================
// Select Data (String)
final String cqlSelect = String.format("SELECT %s, %s FROM %s WHERE %s = %d", TEST_COL_ID, TEST_COL_VALUE, TEST_TABLE, TEST_COL_ID, testId);
verifySelect(myKeyspaceSession.execute(cqlSelect), testId, testValue);
verifier.printCache();
// SessionManager#execute(String) OR AbstractSession#execute(String)
execute = sessionClass.getDeclaredMethod("execute", String.class);
String normalizedSelectSql = SQL_PARSER.normalizedSql(cqlSelect).getNormalizedSql();
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedSelectSql, String.valueOf(testId))));
// ====================
// Delete Data (String)
final String cqlDelete = String.format("DELETE FROM %s.%s WHERE %s = ?", TEST_KEYSPACE, TEST_TABLE, TEST_COL_ID);
myKeyspaceSession.execute(cqlDelete, testId);
verifier.printCache();
// SessionManager#execute(String, Object[]) OR AbstractSession#execute(String, Object[])
execute = sessionClass.getDeclaredMethod("execute", String.class, Object[].class);
String normalizedDeleteSql = SQL_PARSER.normalizedSql(cqlDelete).getNormalizedSql();
verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(normalizedDeleteSql, null)));
} finally {
closeSession(myKeyspaceSession);
}
}
use of com.datastax.driver.core.BoundStatement in project pinpoint by naver.
the class CassandraStatementExecuteQueryInterceptor method before.
@Override
public void before(Object target, Object[] args) {
if (isDebug) {
logger.beforeInterceptor(target, args);
}
Trace trace = traceContext.currentTraceObject();
if (trace == null) {
return;
}
SpanEventRecorder recorder = trace.traceBlockBegin();
try {
DatabaseInfo databaseInfo = (target instanceof DatabaseInfoAccessor) ? ((DatabaseInfoAccessor) target)._$PINPOINT$_getDatabaseInfo() : null;
if (databaseInfo == null) {
databaseInfo = UnKnownDatabaseInfo.INSTANCE;
}
recorder.recordServiceType(databaseInfo.getExecuteQueryType());
recorder.recordEndPoint(databaseInfo.getMultipleHost());
recorder.recordDestinationId(databaseInfo.getDatabaseId());
String sql;
if (args[0] instanceof BoundStatement) {
sql = ((BoundStatement) args[0]).preparedStatement().getQueryString();
} else 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) target)._$PINPOINT$_setParsingResult(parsingResult);
} else {
if (logger.isErrorEnabled()) {
logger.error("sqlParsing fail. parsingResult is null sql:{}", sql);
}
}
Map<Integer, String> bindValue = ((BindValueAccessor) target)._$PINPOINT$_getBindValue();
// Extracting bind variables from already-serialized is too risky
if (bindValue != null && !bindValue.isEmpty()) {
String bindString = toBindVariable(bindValue);
recorder.recordSqlParsingResult(parsingResult, bindString);
} else {
recorder.recordSqlParsingResult(parsingResult);
}
recorder.recordApi(descriptor);
clean(target);
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn(e.getMessage(), e);
}
}
}
Aggregations