private <T> T getValuesCQL( final MapScope scope, final Collection<String> keys, final ResultsBuilderCQL<T> builder ) { final List<ByteBuffer> serializedKeys = new ArrayList<>(); keys.forEach(key -> serializedKeys.add(getMapEntryPartitionKey(scope,key))); Clause in = QueryBuilder.in("key", serializedKeys ); Statement statement = QueryBuilder.select().all().from(MAP_ENTRIES_TABLE) .where(in); ResultSet resultSet = session.execute(statement); return builder.buildResultsCQL( resultSet ); }
/** * Get all entities from cassandra database. * @return <code>List</code> of entities */ public List<T> find() { LOG.debug("Get all entities from column family {}", getColumnFamilyName()); return findListByStatement( QueryBuilder.select() .all().from(getColumnFamilyName()) .setConsistencyLevel(getReadConsistencyLevel())); }
Factory(Session session, TimestampCodec timestampCodec) { this.session = session; this.timestampCodec = timestampCodec; this.preparedStatement = session.prepare( QueryBuilder.select("ts", "trace_id") .from(Tables.SERVICE_SPAN_NAME_INDEX) .where( QueryBuilder.eq( "service_span_name", QueryBuilder.bindMarker("service_span_name"))) .and(QueryBuilder.gte("ts", QueryBuilder.bindMarker("start_ts"))) .and(QueryBuilder.lte("ts", QueryBuilder.bindMarker("end_ts"))) .limit(QueryBuilder.bindMarker("limit_")) .orderBy(QueryBuilder.desc("ts"))); }
selectBuilder = QueryBuilder.select().all(); } else { selectBuilder = QueryBuilder.select(); for (String col : fields) { ((Select.Selection) selectBuilder).column(col); stmt = session.prepare(selectBuilder.from(table) .where(QueryBuilder.eq(YCSB_KEY, QueryBuilder.bindMarker())) .limit(1)); stmt.setConsistencyLevel(readConsistencyLevel); if (trace) { logger.debug("key = {}", key); ResultSet rs = session.execute(stmt.bind(key));
@Override public CassandraEndpointUser findByExternalIdAndTenantId(String externalId, String tenantId) { LOG.debug("Try to find endpoint user by external id {} and tenant id {}", externalId, tenantId); Where where = select().from(getColumnFamilyName()) .where(eq(EP_USER_EXTERNAL_ID_PROPERTY, externalId)) .and(eq(EP_USER_TENANT_ID_PROPERTY, tenantId)); LOG.trace("Try to find endpoint user by cql select {}", where); CassandraEndpointUser endpointUser = findOneByStatement(where); LOG.trace("Found {} endpoint user", endpointUser); return endpointUser; }
/** * @return cql query statement to retrieve the workflow_id for a particular task_id from the "task_lookup" table */ public String getSelectTaskFromLookupTableStatement() { return QueryBuilder.select(WORKFLOW_ID_KEY) .from(keyspace, TABLE_TASK_LOOKUP) .where(eq(TASK_ID_KEY, bindMarker())) .getQueryString(); }
@Test(groups = "short") public void should_support_per_partition_limit() throws Exception { assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2))) .contains( row(0, 0, 0), assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(6))) .hasSize(6); assertThat(session().execute(select().all().from("test_ppl").perPartitionLimit(2).limit(5))) .contains(row(0, 0, 0), row(0, 1, 1), row(1, 0, 0), row(1, 1, 1), row(2, 0, 0)); .execute(select().all().from("test_ppl").where(in("a", 2, 3)).perPartitionLimit(2))) .contains(row(2, 0, 0), row(2, 1, 1), row(3, 0, 0), row(3, 1, 1)); assertThat( .where(in("a", 2, 3)) .perPartitionLimit(bindMarker()) .limit(3) .getQueryString(), 2)) .hasSize(3); .where(in("a", 1, 2, 3)) .perPartitionLimit(bindMarker()) .limit(3) .getQueryString(), 2)) .hasSize(3);
.execute( select("a", "b", "e", count("b"), max("e")) .from(table) .where(eq("b", 2)) .groupBy("a", "b") .allowFiltering())) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6)); .execute( select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .limit(2))) .containsExactly(row(1, 2, 6, 2L, 12), row(1, 4, 12, 2L, 24)); .execute( select("a", "b", "e", count("b"), max("e")) .from(table) .groupBy("a", "b") .perPartitionLimit(1))) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6), row(4, 8, 24, 1L, 24)); .groupBy("a", "b") .perPartitionLimit(1) .limit(2))) .containsExactly(row(1, 2, 6, 2L, 12), row(2, 2, 6, 1L, 6)); assertThat(session().execute(select("a", count("a")).distinct().from(table).groupBy("a"))) .containsExactly(row(1, 1L), row(2, 1L), row(4, 1L));
select = select().all().from("foo").where(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); assertEquals(select.toString(), query); select().all().from("foo").where().and(eq("k", 4)).and(gt("c", "a")).and(lte("c", "z")); assertEquals(select.toString(), query); .from("foo") .where(in("a", InetAddress.getByName("127.0.0.1"), InetAddress.getByName("127.0.0.3"))) .limit(42); assertEquals(select.toString(), query); select = select().writeTime("a").ttl("a").from("foo").allowFiltering(); assertEquals(select.toString(), query); .limit(bindMarker("limit")); assertEquals(select.toString(), query); .where(in("k")) .where() select = select("a").from("foo").where(in("k")); assertEquals(select.toString(), query); select = select("a").from("foo").where(in("k", bindMarker())); assertEquals(select.toString(), query); select = select("a").distinct().from("foo").where(eq("k", 1));
@Test(groups = "short") public void should_handle_contains_on_list_with_index() { PreparedStatement byBuyer = session() .prepare( select("id", "description", "buyers") .from("products") .where(contains("buyers", bindMarker("buyer")))); ResultSet results = session().execute(byBuyer.bind().setInt("buyer", 4)); Row row = results.one(); assertThat(row).isNotNull(); assertThat(row.getInt("id")).isEqualTo(38471); assertThat(row.getList("buyers", Integer.class)).contains(4); }
/** * Reads columns from the delta or delta history table. The range of columns, order, and limit can be * parameterized. */ private ResultSet columnScan(DeltaPlacement placement, TableDDL tableDDL, ByteBuffer rowKey, Range<RangeTimeUUID> columnRange, boolean ascending, int limit, ConsistencyLevel consistency) { Select.Where where = selectFrom(tableDDL) .where(eq(tableDDL.getRowKeyColumnName(), rowKey)); if (columnRange.hasLowerBound()) { if (columnRange.lowerBoundType() == BoundType.CLOSED) { where = where.and(gte(tableDDL.getChangeIdColumnName(), columnRange.lowerEndpoint().getUuid())); } else { where = where.and(gt(tableDDL.getChangeIdColumnName(), columnRange.lowerEndpoint().getUuid())); } } if (columnRange.hasUpperBound()) { if (columnRange.upperBoundType() == BoundType.CLOSED) { where = where.and(lte(tableDDL.getChangeIdColumnName(), columnRange.upperEndpoint().getUuid())); } else { where = where.and(lt(tableDDL.getChangeIdColumnName(), columnRange.upperEndpoint().getUuid())); } } Statement statement = where .orderBy(ascending ? asc(tableDDL.getChangeIdColumnName()) : desc(tableDDL.getChangeIdColumnName())) .limit(limit) .setConsistencyLevel(consistency); return AdaptiveResultSet.executeAdaptiveQuery(placement.getKeyspace().getCqlSession(), statement, _driverConfig.getSingleRowFetchSize()); }
@Test(groups = "unit") public void should_handle_per_partition_limit_clause() { assertThat(select().all().from("foo").perPartitionLimit(2).toString()) .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT 2;"); assertThat(select().all().from("foo").perPartitionLimit(bindMarker()).toString()) .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT ?;"); assertThat(select().all().from("foo").perPartitionLimit(bindMarker("limit")).toString()) .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT :limit;"); assertThat(select().all().from("foo").perPartitionLimit(2).limit(bindMarker()).toString()) .isEqualTo("SELECT * FROM foo PER PARTITION LIMIT 2 LIMIT ?;"); assertThat( .where(in("a", 2, 4)) .perPartitionLimit(2) .limit(3) .toString()) .isEqualTo("SELECT * FROM foo WHERE a IN (2,4) PER PARTITION LIMIT 2 LIMIT 3;"); assertThat( .where(eq("a", bindMarker())) .perPartitionLimit(bindMarker()) .limit(3) .toString()) .isEqualTo("SELECT * FROM foo WHERE a=? PER PARTITION LIMIT ? LIMIT 3;"); assertThat( .where(eq("a", bindMarker())) .orderBy(desc("b")) .perPartitionLimit(2) .limit(3)
/** @test_category queries:builder */ @Test(groups = "unit") public void should_handle_allow_filtering() { assertThat(select().all().from("foo").allowFiltering().toString()) .isEqualTo("SELECT * FROM foo ALLOW FILTERING;"); assertThat(select().all().from("foo").where(eq("x", 42)).allowFiltering().toString()) .isEqualTo("SELECT * FROM foo WHERE x=42 ALLOW FILTERING;"); } }
select = select().all().from("foo").where(eq("k", "4 AND c=5")); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4' AND c='5")); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4' OR '1'='1")); assertEquals(select.toString(), query); select = select().all().from("foo").where(eq("k", "4; --test comment;")); assertEquals(select.toString(), query); select = select("a", "b").from("foo").where(in("a", "b", "c'); --comment")); assertEquals(select.toString(), query); select("a", "b") .from("foo") .where(in("a", Sets.newLinkedHashSet(Arrays.asList("a", "b", "c")))); assertEquals(select.toString(), query); select = select().writeTime("a) FROM bar; --").ttl("a").from("foo").allowFiltering(); assertEquals(select.toString(), query); select = select().writeTime("a").ttl("a) FROM bar; --").from("foo").allowFiltering(); assertEquals(select.toString(), query); select = select().all().from("foo").where(gt("k=1 OR k", 42)).limit(42); assertEquals(select.toString(), query);
@Override public ResponseData get(Object rkey, Object colName) throws OperationException { Session session = DataStaxClientConnection.instance.session(); TableMetadata tm = DataStaxClientConnection.instance.getKeyspaceMetadata().getTable(cfName); String partitionKey = tm.getPartitionKey().get(0).getName(); Query query = QueryBuilder.select(colName.toString()).from(cfName).where(QueryBuilder.eq(partitionKey, rkey)).limit(1000000) .setConsistencyLevel(ConsistencyLevel.valueOf(com.netflix.jmeter.properties.Properties.instance.cassandra.getReadConsistency())); ResultSetFuture rs = session.executeAsync(query); int size = 0; try { Row row = rs.getUninterruptibly(1000000, TimeUnit.MILLISECONDS).one(); size = row != null ? row.getBytesUnsafe(colName.toString()).capacity() : 0; } catch (TimeoutException e) { e.printStackTrace(); throw new OperationException(e); } return new DataStaxClientResponseData("", size, "", 0, rkey, colName, null); }
assertThat(select().all().from("foo").groupBy("c1", column("c2"), raw("c3")).toString()) .isEqualTo("SELECT * FROM foo GROUP BY c1,c2,c3;"); assertThat( select() .all() .from("foo") .groupBy("c1", column("c2"), raw("c3")) .orderBy(asc("c1")) .toString()) .isEqualTo("SELECT * FROM foo GROUP BY c1,c2,c3 ORDER BY c1 ASC;"); assertThat( select() .all() .from("foo") .where(eq("x", 42)) .groupBy("c1", column("c2"), raw("c3")) .toString()) .isEqualTo("SELECT * FROM foo WHERE x=42 GROUP BY c1,c2,c3;"); assertThat( .all() .from("foo") .where(eq("x", 42)) .groupBy("c1", column("c2"), raw("c3")) .orderBy(asc("c1")) .toString()) .isEqualTo("SELECT * FROM foo WHERE x=42 GROUP BY c1,c2,c3 ORDER BY c1 ASC;"); try { select().all().from("foo").groupBy("foo").groupBy("bar");