Search in sources :

Example 6 with ResultSet

use of com.datastax.driver.core.ResultSet in project camel by apache.

the class ResultSetConversionStrategiesTest method testOne.

@Test
public void testOne() {
    ResultSetConversionStrategy strategy = ResultSetConversionStrategies.fromName("ONE");
    ResultSet resultSet = mock(ResultSet.class);
    Row row = mock(Row.class);
    when(resultSet.one()).thenReturn(row);
    Object body = strategy.getBody(resultSet);
    assertTrue(body instanceof Row);
    assertSame(row, body);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 7 with ResultSet

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

the class AsyncExecutor method execAsync.

/**
     * Asynchronously executes the specified batch statement. Inputs will be passed to
     * the {@link #handler} once query succeed or failed.
     */
public SettableFuture<T> execAsync(final Statement statement, final T inputs, final AsyncResultHandler<T> handler) {
    final SettableFuture<T> settableFuture = SettableFuture.create();
    pending.incrementAndGet();
    ResultSetFuture future = session.executeAsync(statement);
    Futures.addCallback(future, new FutureCallback<ResultSet>() {

        public void release() {
            pending.decrementAndGet();
        }

        @Override
        public void onSuccess(ResultSet result) {
            release();
            settableFuture.set(inputs);
            handler.success(inputs);
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error(String.format("Failed to execute statement '%s' ", statement), t);
            release();
            settableFuture.setException(t);
            handler.failure(t, inputs);
        }
    }, executorService);
    return settableFuture;
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ResultSet(com.datastax.driver.core.ResultSet)

Example 8 with ResultSet

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

the class CassandraSampleRepository method delete.

@Override
public void delete(Context context, Resource resource) {
    /**
         * Check for ttl value > 0
         */
    if (m_ttl > 0) {
        /**
             * Delete exactly from (now - ttl) till now
             */
        final Timestamp start = Timestamp.now().minus(m_ttl, TimeUnit.SECONDS);
        final Timestamp end = Timestamp.now();
        final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
        final List<Future<ResultSet>> futures = Lists.newArrayList();
        for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
            BoundStatement bindStatement = m_deleteStatement.bind();
            bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
            bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
            bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
            futures.add(m_session.executeAsync(bindStatement));
        }
        for (final Future<ResultSet> future : futures) {
            try {
                future.get();
            } catch (final InterruptedException | ExecutionException e) {
                throw Throwables.propagate(e);
            }
        }
    } else {
        // Choose (now - one year) till now...
        Timestamp end = Timestamp.now();
        Timestamp start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
        // ... and check whether samples exist for this period of time.
        while (cassandraSelect(context, resource, start, end).hasNext()) {
            // Now delete the samples...
            final Duration resourceShard = m_contextConfigurations.getResourceShard(context);
            final List<Future<ResultSet>> futures = Lists.newArrayList();
            for (Timestamp partition : new IntervalGenerator(start.stepFloor(resourceShard), end.stepFloor(resourceShard), resourceShard)) {
                BoundStatement bindStatement = m_deleteStatement.bind();
                bindStatement.setString(SchemaConstants.F_CONTEXT, context.getId());
                bindStatement.setInt(SchemaConstants.F_PARTITION, (int) partition.asSeconds());
                bindStatement.setString(SchemaConstants.F_RESOURCE, resource.getId());
                futures.add(m_session.executeAsync(bindStatement));
            }
            for (final Future<ResultSet> future : futures) {
                try {
                    future.get();
                } catch (final InterruptedException | ExecutionException e) {
                    throw Throwables.propagate(e);
                }
            }
            // ...set end to start and start to (end - one year)
            end = start;
            start = end.minus(DELETION_INTERVAL, TimeUnit.DAYS);
        // and start over again until no more samples are found
        }
    }
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Future(java.util.concurrent.Future) Duration(org.opennms.newts.api.Duration) IntervalGenerator(org.opennms.newts.aggregate.IntervalGenerator) ExecutionException(java.util.concurrent.ExecutionException) Timestamp(org.opennms.newts.api.Timestamp) BoundStatement(com.datastax.driver.core.BoundStatement)

Example 9 with ResultSet

use of com.datastax.driver.core.ResultSet in project javaee7-samples by javaee-samples.

the class PersonSessionBean method getPersons.

public List<Person> getPersons() {
    List<Person> persons = new ArrayList<>();
    ResultSet results = session.execute(selectAllPersons.bind());
    for (Row row : results) {
        persons.add(new Person(row.getString("name"), row.getInt("age")));
    }
    return persons;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 10 with ResultSet

use of com.datastax.driver.core.ResultSet in project zipkin by openzipkin.

the class CassandraSpanStore method getSpanNames.

@Override
public ListenableFuture<List<String>> getSpanNames(String serviceName) {
    if (serviceName == null || serviceName.isEmpty())
        return EMPTY_LIST;
    serviceName = checkNotNull(serviceName, "serviceName").toLowerCase();
    int bucket = 0;
    try {
        BoundStatement bound = CassandraUtil.bindWithName(selectSpanNames, "select-span-names").setString("service_name", serviceName).setInt("bucket", bucket).setInt("limit_", 1000);
        return transform(session.executeAsync(bound), new Function<ResultSet, List<String>>() {

            @Override
            public List<String> apply(ResultSet input) {
                Set<String> spanNames = new HashSet<>();
                for (Row row : input) {
                    spanNames.add(row.getString("span_name"));
                }
                return Ordering.natural().sortedCopy(spanNames);
            }
        });
    } catch (RuntimeException ex) {
        return immediateFailedFuture(ex);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ContiguousSet(com.google.common.collect.ContiguousSet) HashSet(java.util.HashSet) ResultSet(com.datastax.driver.core.ResultSet) ResultSet(com.datastax.driver.core.ResultSet) Futures.allAsList(com.google.common.util.concurrent.Futures.allAsList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Row(com.datastax.driver.core.Row) BoundStatement(com.datastax.driver.core.BoundStatement)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)64 Row (com.datastax.driver.core.Row)35 Test (org.junit.Test)25 BoundStatement (com.datastax.driver.core.BoundStatement)11 Session (com.datastax.driver.core.Session)10 ArrayList (java.util.ArrayList)9 Cluster (com.datastax.driver.core.Cluster)8 Statement (com.datastax.driver.core.Statement)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 List (java.util.List)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 BatchStatement (com.datastax.driver.core.BatchStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 Update (com.datastax.driver.core.querybuilder.Update)3 ImmutableList (com.google.common.collect.ImmutableList)3 IgniteException (org.apache.ignite.IgniteException)3 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2