Java Code Examples for com.datastax.driver.core.ResultSet#getAvailableWithoutFetching()

The following examples show how to use com.datastax.driver.core.ResultSet#getAvailableWithoutFetching() . 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: CassandraEntryIterator.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public CassandraEntryIterator(ResultSet results, Query query,
       BiFunction<BackendEntry, Row, BackendEntry> merger) {
    super(query);
    this.results = results;
    this.rows = results.iterator();
    this.remaining = results.getAvailableWithoutFetching();
    this.merger = merger;
    this.next = null;

    this.skipOffset();

    if (query.paging()) {
        E.checkState(this.remaining == query.limit() ||
                     results.isFullyFetched(),
                     "Unexpected fetched page size: %s", this.remaining);
    }
}
 
Example 2
Source File: TransferLogSerializationImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public void removeTransferLog(
        String queueName, String source, String dest, UUID messageId ) throws QakkaException {

    Statement query = QueryBuilder.select().all().from(TABLE_TRANSFER_LOG)
        .where(   QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ))
            .and( QueryBuilder.eq( COLUMN_DEST_REGION, dest ))
            .and( QueryBuilder.eq( COLUMN_MESSAGE_ID, messageId ));
    ResultSet rs = cassandraClient.getApplicationSession().execute( query );

    if ( rs.getAvailableWithoutFetching() == 0 ) {
        StringBuilder sb = new StringBuilder();
        sb.append( "Transfer log entry not found for queueName=" ).append( queueName );
        sb.append( " dest=" ).append( dest );
        sb.append( " messageId=" ).append( messageId );
        throw new QakkaException( sb.toString() );
    }

    Statement deleteQuery = QueryBuilder.delete().from(TABLE_TRANSFER_LOG)
            .where(   QueryBuilder.eq( COLUMN_QUEUE_NAME, queueName ))
                .and( QueryBuilder.eq( COLUMN_DEST_REGION, dest ))
            .and( QueryBuilder.eq( COLUMN_MESSAGE_ID, messageId ));
    cassandraClient.getApplicationSession().execute( deleteQuery );
}
 
Example 3
Source File: BaseModelDao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
protected List<M> list(BoundStatement stmt) {
   ResultSet rs = session().execute( stmt );
   List<M> results = new ArrayList<>(rs.getAvailableWithoutFetching());
   rs.forEach(
      (row) -> 
         Optional
            .ofNullable( toModel(row) )
            .ifPresent( results::add )
   );
   return results;
}
 
Example 4
Source File: CassandraQueryExecutor.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <T> List<T> list(ResultSet rs, Function<Row, T> transformer) {
   List<T> results = new ArrayList<T>(rs.getAvailableWithoutFetching());
   for(Row row: rs) {
      T result = transformer.apply(row);
      Preconditions.checkNotNull(result);
      results.add(result);
   }
   return results;
}
 
Example 5
Source File: CassandraQueryExecutor.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
public static <T> List<T> listOptional(ResultSet rs, Function<Row, Optional<T>> transformer) {
   List<T> results = new ArrayList<T>(rs.getAvailableWithoutFetching());
   for(Row row: rs) {
      Optional<T> result = transformer.apply(row);
      if(result.isPresent()) {
         results.add(result.get());
      }
   }
   return results;
}
 
Example 6
Source File: CassandraFactory.java    From database-transform-tool with Apache License 2.0 5 votes vote down vote up
/**
 * 描述: 数据操作(Insert|Update|Delete)
 * 时间: 2017年11月15日 上午11:27:52
 * @author yi.zhang
 * @param cql	cql语句
 * @param params	参数
 * @return	返回值
 */
public int executeUpdate(String cql, Object... params) {
	try {
		if(session!=null){
			init(servers, keyspace, username, password);
		}
		ResultSet rs = session.execute(cql, params);
		return rs.getAvailableWithoutFetching();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return -1;
}
 
Example 7
Source File: AdaptiveResultSetTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "async")
public void testSecondPageExceedsMaxFetchSize(boolean async) throws Exception {
    if (!_runTests) {
        throw new SkipException("Skipping test");
    }

    // Query for all data starting with the first row, "a".  The first row has small blobs and therefore the maximum
    // frame size should only be exceeded on the second page.

    Statement statement = QueryBuilder.select("rowid", "col", "data")
            .from(_keyspaceName, _tableName)
            .where(QueryBuilder.gte(QueryBuilder.token("rowid"), "a"))
            .setFetchSize(256);

    // Run once without using adaptive queries to verify the maximum frame size is exceeded only on the second page
    ResultSet rs = _session.execute(statement);
    // Consume the first page's results
    while (rs.getAvailableWithoutFetching() != 0) {
        rs.one();
    }

    // The next read fetches the next page's results which should exceed the maximum frame size
    try {
        rs.one();
        fail("FrameTooLongException not thrown");
    } catch (FrameTooLongException e) {
        // Ok, this is the expected exception
    }

    if (async) {
        rs = AdaptiveResultSet.executeAdaptiveQueryAsync(_session, statement, 256).get();
    } else {
        rs = AdaptiveResultSet.executeAdaptiveQuery(_session, statement, 256);
    }

    verifyResults(rs, 'a', 'k');
}
 
Example 8
Source File: TestBaselineApproach.java    From bboxdb with Apache License 2.0 5 votes vote down vote up
/**
 * Execute a range query
 * @param destTable
 * @param range
 */
public void executeRangeQuery(final String destTable, final String format, final Hyperrectangle range) {
	System.out.println("# Execute range query in range " + range);

	final Stopwatch stopwatch = Stopwatch.createStarted();
	final TupleBuilder tupleBuilder = TupleBuilderFactory.getBuilderForFormat(format);

	long readRecords = 0;
	long resultRecords = 0;

	final SimpleStatement statement = new SimpleStatement("SELECT * FROM " + destTable);
	statement.setFetchSize(2000); // Use 2000 tuples per page

	final ResultSet rs = session.execute(statement);

	for (final Row row : rs) {

		// Request next page
	    if (rs.getAvailableWithoutFetching() == 100 && !rs.isFullyFetched()) {
	        rs.fetchMoreResults(); // this is asynchronous
	    }

	    readRecords++;

	    final long id = row.getLong(0);
	    final String text = row.getString(1);

	    final Tuple tuple = tupleBuilder.buildTuple(text, Long.toString(id));

	    if(tuple.getBoundingBox().intersects(range)) {
	    	resultRecords++;
	    }
	}

	System.out.println("# Read records " + readRecords + " result records " + resultRecords);
	System.out.println("# Execution time in sec " + stopwatch.elapsed(TimeUnit.SECONDS));
}
 
Example 9
Source File: MetricService.java    From disthene-reader with MIT License 5 votes vote down vote up
public void makeArray(ResultSet resultSet, int length, Map<Long, Integer> timestampIndices) {
    if (resultSet.getAvailableWithoutFetching() > 0) {
        allNulls = false;
        values = new Double[length];
        for (Row row : resultSet) {
            values[timestampIndices.get(row.getLong("time"))] =
                    isSumMetric(path) ? CollectionUtils.unsafeSum(row.getList("data", Double.class)) : CollectionUtils.unsafeAverage(row.getList("data", Double.class));
        }
    } else {
        values = new Double[length];
        for (Map.Entry<Long, Integer> entry : timestampIndices.entrySet()) {
            values[entry.getValue()] = null;
        }
    }
}
 
Example 10
Source File: TransferLogSerializationImpl.java    From usergrid with Apache License 2.0 5 votes vote down vote up
@Override
public Result<TransferLog> getAllTransferLogs(PagingState pagingState, int fetchSize ) {

    Statement query = QueryBuilder.select().all().from(TABLE_TRANSFER_LOG);

    query.setFetchSize( fetchSize );
    if ( pagingState != null ) {
        query.setPagingState( pagingState );
    }

    ResultSet rs = cassandraClient.getApplicationSession().execute( query );
    final PagingState newPagingState = rs.getExecutionInfo().getPagingState();

    final List<TransferLog> transferLogs = new ArrayList<>();
    int numReturned = rs.getAvailableWithoutFetching();
    for ( int i=0; i<numReturned; i++ ) {
        Row row = rs.one();
        TransferLog tlog = new TransferLog(
                row.getString( COLUMN_QUEUE_NAME ),
                row.getString( COLUMN_SOURCE_REGION ),
                row.getString( COLUMN_DEST_REGION ),
                row.getUUID( COLUMN_MESSAGE_ID ),
                row.getLong( COLUMN_TRANSFER_TIME ));
        transferLogs.add( tlog );
    }

    return new Result<TransferLog>() {

        @Override
        public PagingState getPagingState() {
            return newPagingState;
        }

        @Override
        public List<TransferLog> getEntities() {
            return transferLogs;
        }
    };
}
 
Example 11
Source File: QueryCassandra.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a result set into an Avro record and writes it to the given stream.
 *
 * @param rs        The result set to convert
 * @param outStream The stream to which the Avro record will be written
 * @param timeout   The max number of timeUnits to wait for a result set fetch to complete
 * @param timeUnit  The unit of time (SECONDS, e.g.) associated with the timeout amount
 * @return The number of rows from the result set written to the stream
 * @throws IOException          If the Avro record cannot be written
 * @throws InterruptedException If a result set fetch is interrupted
 * @throws TimeoutException     If a result set fetch has taken longer than the specified timeout
 * @throws ExecutionException   If any error occurs during the result set fetch
 */
public static long convertToAvroStream(final ResultSet rs, final OutputStream outStream,
                                       long timeout, TimeUnit timeUnit)
        throws IOException, InterruptedException, TimeoutException, ExecutionException {

    final Schema schema = createSchema(rs);
    final GenericRecord rec = new GenericData.Record(schema);

    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    try (final DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter)) {
        dataFileWriter.create(schema, outStream);

        final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions();
        long nrOfRows = 0;
        if (columnDefinitions != null) {
            do {

                // Grab the ones we have
                int rowsAvailableWithoutFetching = rs.getAvailableWithoutFetching();
                if (rowsAvailableWithoutFetching == 0) {
                    // Get more
                    if (timeout <= 0 || timeUnit == null) {
                        rs.fetchMoreResults().get();
                    } else {
                        rs.fetchMoreResults().get(timeout, timeUnit);
                    }
                }

                for (Row row : rs) {

                    for (int i = 0; i < columnDefinitions.size(); i++) {
                        final DataType dataType = columnDefinitions.getType(i);

                        if (row.isNull(i)) {
                            rec.put(i, null);
                        } else {
                            rec.put(i, getCassandraObject(row, i, dataType));
                        }
                    }
                    dataFileWriter.append(rec);
                    nrOfRows += 1;

                }
            } while (!rs.isFullyFetched());
        }
        return nrOfRows;
    }
}
 
Example 12
Source File: TestBaselineApproach.java    From bboxdb with Apache License 2.0 4 votes vote down vote up
/**
 * Execute a join (nested loop join)
 * @param table1
 * @param table2
 * @param range
 */
public void executeJoin(final String table1, final String table2, final String format1,
		final String format2, final Hyperrectangle range, final double padding) {

	System.out.println("# Execute join query in range " + range);

	final Stopwatch stopwatch = Stopwatch.createStarted();

	final TupleBuilder tupleBuilder1 = TupleBuilderFactory.getBuilderForFormat(format1);
    final TupleBuilder tupleBuilder2 = TupleBuilderFactory.getBuilderForFormat(format2);

	long readRecords = 0;
	long resultRecords = 0;

	final SimpleStatement statementTable1 = new SimpleStatement("SELECT * FROM " + table1);
	statementTable1.setFetchSize(2000); // Use 2000 tuples per page

	final ResultSet rs1 = session.execute(statementTable1);

	for (final Row row1 : rs1) {

		// Request next page
	    if (rs1.getAvailableWithoutFetching() == 100 && !rs1.isFullyFetched()) {
	        rs1.fetchMoreResults(); // this is asynchronous
	    }

	    readRecords++;

	    final long id1 = row1.getLong(0);
	    final String text1 = row1.getString(1);

	    final Tuple tuple1 = tupleBuilder1.buildTuple(text1, Long.toString(id1));

	    // Tuple is outside of our query range
	    if(! tuple1.getBoundingBox().intersects(range)) {
	    	continue;
	    }

		// Perform the nested loop join
	    final SimpleStatement statementTable2 = new SimpleStatement("SELECT * FROM " + table2);
		statementTable2.setFetchSize(2000); // Use 2000 tuples per page

		final ResultSet rs2 = session.execute(statementTable2);

		for (final Row row2 : rs2) {
			// Request next page
		    if (rs1.getAvailableWithoutFetching() == 100 && !rs1.isFullyFetched()) {
		        rs1.fetchMoreResults(); // this is asynchronous
		    }

		    readRecords++;

		    final long id2 = row2.getLong(0);
		    final String text2 = row2.getString(1);

		    final Tuple tuple2 = tupleBuilder2.buildTuple(text2, Long.toString(id2));

		    if(tuple1.getBoundingBox().intersects(tuple2.getBoundingBox().enlargeByAmount(padding))) {
		    	resultRecords++;
		    }
		}
	}

	System.out.println("# Read records " + readRecords + " result records " + resultRecords);
	System.out.println("# Execution time in sec " + stopwatch.elapsed(TimeUnit.SECONDS));
}
 
Example 13
Source File: CassandraUtils.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean fetchNeeded(ResultSet resultSet) {
    return resultSet.getAvailableWithoutFetching() == cassandraConfiguration.getFetchNextPageInAdvanceRow()
        && !resultSet.isFullyFetched();
}
 
Example 14
Source File: QueryCassandra.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a result set into an Avro record and writes it to the given stream.
 *
 * @param rs        The result set to convert
 * @param outStream The stream to which the Avro record will be written
 * @param timeout   The max number of timeUnits to wait for a result set fetch to complete
 * @param timeUnit  The unit of time (SECONDS, e.g.) associated with the timeout amount
 * @return The number of rows from the result set written to the stream
 * @throws IOException          If the Avro record cannot be written
 * @throws InterruptedException If a result set fetch is interrupted
 * @throws TimeoutException     If a result set fetch has taken longer than the specified timeout
 * @throws ExecutionException   If any error occurs during the result set fetch
 */
public static long convertToAvroStream(final ResultSet rs, final OutputStream outStream,
                                       long timeout, TimeUnit timeUnit)
        throws IOException, InterruptedException, TimeoutException, ExecutionException {

    final Schema schema = createSchema(rs);
    final GenericRecord rec = new GenericData.Record(schema);

    final DatumWriter<GenericRecord> datumWriter = new GenericDatumWriter<>(schema);
    try (final DataFileWriter<GenericRecord> dataFileWriter = new DataFileWriter<>(datumWriter)) {
        dataFileWriter.create(schema, outStream);

        final ColumnDefinitions columnDefinitions = rs.getColumnDefinitions();
        long nrOfRows = 0;
        if (columnDefinitions != null) {
            do {

                // Grab the ones we have
                int rowsAvailableWithoutFetching = rs.getAvailableWithoutFetching();
                if (rowsAvailableWithoutFetching == 0) {
                    // Get more
                    if (timeout <= 0 || timeUnit == null) {
                        rs.fetchMoreResults().get();
                    } else {
                        rs.fetchMoreResults().get(timeout, timeUnit);
                    }
                }

                for (Row row : rs) {

                    for (int i = 0; i < columnDefinitions.size(); i++) {
                        final DataType dataType = columnDefinitions.getType(i);

                        if (row.isNull(i)) {
                            rec.put(i, null);
                        } else {
                            rec.put(i, getCassandraObject(row, i, dataType));
                        }
                    }
                    dataFileWriter.append(rec);
                    nrOfRows += 1;

                }
            } while (!rs.isFullyFetched());
        }
        return nrOfRows;
    }
}