@Override public void removeValuesUnusedLongerThan(long minimumAge, TimeUnit unit) throws BinaryStoreException { try { Date deadline = new Date(new Date().getTime() - unit.toMillis(minimumAge)); // When querying using 2nd indexes, Cassandra // (it's not CQL specific) requires that you use an '=' for at least one of // the indexed column in the where clause. This is a limitation of Cassandra. // So we have to do some tricks here ResultSet rs = session.execute( "SELECT cid from modeshape.binary where usage=0 and usage_time < " + deadline.getTime() + " allow filtering;"); Iterator<Row> rows = rs.iterator(); while (rows.hasNext()) { session.execute( "DELETE from modeshape.binary where cid = '" + rows.next().getString("cid") + "';"); } rs = session.execute( "SELECT cid from modeshape.binary where usage=1 and usage_time < " + deadline.getTime() + " allow filtering;"); rows = rs.iterator(); while (rows.hasNext()) { session.execute( "DELETE from modeshape.binary where cid = '" + rows.next().getString("cid") + "';"); } } catch (RuntimeException e) { throw new BinaryStoreException(e); } }
//////////////////////////////////// // Overridden Methods for IBackingMap //////////////////////////////////// @SuppressWarnings("unchecked") @Override public List<T> multiGet(List<List<Object>> keys) { try { List<T> values = new ArrayList<T>(); for (List<Object> rowKey : keys) { Statement statement = mapper.retrieve(rowKey); ResultSet results = session.execute(statement); // TODO: Better way to check for empty results besides accessing entire results list Iterator<Row> rowIter = results.iterator(); Row row; if (results != null && rowIter.hasNext() && (row = rowIter.next()) != null) { if (rowIter.hasNext()) { LOG.error("Found non-unique value for key [{}]", rowKey); } else { values.add((T) mapper.getValue(row)); } } else { values.add(null); } } _mreads.incrBy(values.size()); LOG.debug("Retrieving the following keys: {} with values: {}", keys, values); return values; } catch (Exception e) { checkCassandraException(e); throw new IllegalStateException("Impossible to reach this code"); } }
private void test() { ResultSet result = cassandraCQLUnit.session.execute( "select * from testCQLTable WHERE id='1690e8da-5bf8-49e8-9583-4dff8a570737'"); String val = result.iterator().next().getString("value"); assertEquals("Cql loaded string", val); }
/** * Test content for existence. * * @param key content identifier * @param alive true inside used content and false for checking within content marked as unused. * @return true if content found * @throws BinaryStoreException */ private boolean contentExists(BinaryKey key, boolean alive) throws BinaryStoreException { try { String query = "SELECT payload from modeshape.binary where cid='" + key.toString() + "'"; query = alive ? query + " and usage=1;" : query + " and usage = 0;"; ResultSet rs = session.execute(query); return rs.iterator().hasNext(); } catch (RuntimeException e) { throw new BinaryStoreException(e); } }
@Test public void testTridentTopology() throws Exception { Session session = cassandraCQLUnit.session; String[] stationIds = {"station-1", "station-2", "station-3"}; for (int i = 1; i < 4; i++) { ResultSet resultSet = session.execute( "INSERT INTO weather.station(id, name) VALUES(?, ?)", stationIds[i - 1], "Foo-Station-" + new Random().nextInt()); } ResultSet rows = cassandraCQLUnit.session.execute("SELECT * FROM weather.station"); for (Row row : rows) { System.out.println("####### row = " + row); } WeatherBatchSpout weatherBatchSpout = new WeatherBatchSpout( new Fields("weather_station_id", "temperature", "event_time"), 3, stationIds); TridentTopology topology = new TridentTopology(); Stream stream = topology.newStream("cassandra-trident-stream", weatherBatchSpout); CassandraStateFactory insertValuesStateFactory = getInsertTemperatureStateFactory(); CassandraStateFactory selectWeatherStationStateFactory = getSelectWeatherStationStateFactory(); TridentState selectState = topology.newStaticState(selectWeatherStationStateFactory); stream = stream.stateQuery( selectState, new Fields("weather_station_id"), new CassandraQuery(), new Fields("name")); stream = stream.each(new Fields("name"), new PrintFunction(), new Fields("name_x")); stream.partitionPersist( insertValuesStateFactory, new Fields("weather_station_id", "name", "event_time", "temperature"), new CassandraStateUpdater(), new Fields()); StormTopology stormTopology = topology.build(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("wordCounter", getConfig(), stormTopology); Thread.sleep(30 * 1000); rows = cassandraCQLUnit.session.execute("SELECT * FROM weather.temperature"); Assert.assertTrue(rows.iterator().hasNext()); // basic sanity check cluster.killTopology("wordCounter"); cluster.shutdown(); }
@Override public Iterable<BinaryKey> getAllBinaryKeys() throws BinaryStoreException { try { ResultSet rs = session.execute("SELECT cid from modeshape.binary WHERE usage=1;"); Iterator<Row> it = rs.iterator(); HashSet<BinaryKey> keys = new HashSet<BinaryKey>(); while (it.hasNext()) { keys.add(new BinaryKey(it.next().getString("cid"))); } return keys; } catch (RuntimeException e) { throw new BinaryStoreException(e); } }
private Row getRow(ResultSet resultSet) { Iterator<Row> iterator = resultSet.iterator(); if (!iterator.hasNext()) { return null; } Row firstRow = iterator.next(); if (singleResult && iterator.hasNext()) { throw new CassandraNotSingleResultException(resultSet); } return firstRow; }