List of usage examples for org.apache.cassandra.utils ByteBufferUtil toInt
public static int toInt(ByteBuffer bytes)
From source file:co.cask.hydrator.plugin.test.ETLCassandraTest.java
License:Apache License
private void testCassandraRealtimeSink() throws Exception { Schema schema = Schema.recordOf("user", Schema.Field.of("name", Schema.of(Schema.Type.STRING)), Schema.Field.of("graduated", Schema.of(Schema.Type.BOOLEAN)), Schema.Field.of("id", Schema.of(Schema.Type.INT)), Schema.Field.of("score", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("time", Schema.of(Schema.Type.LONG))); List<StructuredRecord> input = ImmutableList.of(StructuredRecord.builder(schema).set("id", 1) .set("name", "Bob").set("score", 3.4).set("graduated", false).set("time", 1234567890000L).build()); ETLStage source = new ETLStage("source", co.cask.cdap.etl.mock.realtime.MockSource.getPlugin(input)); ETLStage sink = new ETLStage("Cassandra", new ETLPlugin("Cassandra", RealtimeSink.PLUGIN_TYPE, new ImmutableMap.Builder<String, String>().put(Constants.Reference.REFERENCE_NAME, "TestCass") .put(RealtimeCassandraSink.Cassandra.ADDRESSES, "localhost:9042") .put(RealtimeCassandraSink.Cassandra.KEYSPACE, "testkeyspace") .put(RealtimeCassandraSink.Cassandra.COLUMN_FAMILY, "testtablerealtime") .put(RealtimeCassandraSink.Cassandra.COLUMNS, "name, graduated, id, score, time") .put(RealtimeCassandraSink.Cassandra.COMPRESSION, "NONE") .put(RealtimeCassandraSink.Cassandra.CONSISTENCY_LEVEL, "QUORUM").build(), null));// w ww. j a v a2 s. co m final String cqlQuery = "select name,graduated,id,score,time from testtablerealtime"; ETLRealtimeConfig etlConfig = ETLRealtimeConfig.builder().addStage(source).addStage(sink) .addConnection(source.getName(), sink.getName()).build(); Id.Application appId = Id.Application.from(Id.Namespace.DEFAULT, "testESSink"); AppRequest<ETLRealtimeConfig> appRequest = new AppRequest<>(REALTIME_APP_ARTIFACT, etlConfig); ApplicationManager appManager = deployApplication(appId, appRequest); WorkerManager workerManager = appManager.getWorkerManager(ETLWorker.class.getSimpleName()); workerManager.start(); Tasks.waitFor(true, new Callable<Boolean>() { @Override public Boolean call() throws Exception { CqlResult result = client.execute_cql3_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE, ConsistencyLevel.ALL); return result.rows.size() > 0; } }, 30, TimeUnit.SECONDS, 50, TimeUnit.MILLISECONDS); workerManager.stop(); CqlResult result = client.execute_cql3_query(ByteBufferUtil.bytes(cqlQuery), Compression.NONE, ConsistencyLevel.ALL); List<Column> columns = result.getRows().get(0).getColumns(); Assert.assertEquals("Bob", ByteBufferUtil.string(columns.get(0).bufferForValue())); byte[] bytes = new byte[] { 0 }; Assert.assertEquals(ByteBuffer.wrap(bytes), columns.get(1).bufferForValue()); Assert.assertEquals(1, ByteBufferUtil.toInt(columns.get(2).bufferForValue())); Assert.assertEquals(3.4, ByteBufferUtil.toDouble(columns.get(3).bufferForValue()), 0.000001); Assert.assertEquals(1234567890000L, ByteBufferUtil.toLong(columns.get(4).bufferForValue())); }
From source file:com.impetus.client.crud.PersonCassandraTTLTest.java
License:Apache License
/** * Tests whether TTL provided while inserting records are correctly getting * applied using CQL 3 Common TTL value for entire row is used * /*from w w w.j av a 2 s . com*/ * @throws Exception */ @Test public void testTTLonCQL3_0() throws Exception { propertyMap = new HashMap(); propertyMap.put(PersistenceProperties.KUNDERA_DDL_AUTO_PREPARE, "create"); propertyMap.put(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0); emf = Persistence.createEntityManagerFactory(SEC_IDX_CASSANDRA_TEST, propertyMap); em = emf.createEntityManager(); Map<String, Integer> ttlValues = new HashMap<String, Integer>(); ttlValues.put("PERSONCASSANDRA", new Integer(5)); em.setProperty("ttl.per.request", true); em.setProperty("ttl.values", ttlValues); Object p1 = prepareData("1", 10); em.persist(p1); em.clear(); CassandraCli.client.set_keyspace("KunderaExamples"); CqlResult cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '1'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); List<CqlRow> cqlRows = cqlResult.getRows(); CqlRow cqlRow = cqlRows.get(0); boolean personNameFound = false; boolean ageFound = false; for (Column column : cqlRow.getColumns()) { String columnName = new String(column.getName(), Constants.ENCODING); if (columnName.equals("ttl(PERSON_NAME)")) { Assert.assertEquals(5, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); personNameFound = true; } else if (columnName.equals("ttl(AGE)")) { Assert.assertEquals(5, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); ageFound = true; } } Assert.assertTrue(personNameFound && ageFound); Thread.sleep(5000); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '1'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); try { cqlRow = cqlRows.get(0); Assert.fail( "PERSON_NAME and AGE column not deleted even though a TTL of 5 seconds was specified while writing to cassandra."); } catch (IndexOutOfBoundsException ioobe) { Assert.assertTrue(cqlRows.isEmpty()); } // checking for update query. Object p2 = prepareData("2", 10); em.persist(p2); em.clear(); ttlValues = new HashMap<String, Integer>(); ttlValues.put("PERSONCASSANDRA", new Integer(10)); em.setProperty("ttl.per.request", true); em.setProperty("ttl.values", ttlValues); Query q = em.createQuery("update PersonCassandra p set p.personName='''KK MISHRA''' where p.personId=2"); q.executeUpdate(); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '2'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); cqlRow = cqlRows.get(0); personNameFound = false; ageFound = false; for (Column column : cqlRow.getColumns()) { String columnName = new String(column.getName(), Constants.ENCODING); if (columnName.equals("ttl(PERSON_NAME)")) { Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); personNameFound = true; } else if (columnName.equals("ttl(AGE)")) { Assert.assertEquals(null, column.getValue()); ageFound = true; } } Assert.assertTrue(personNameFound && ageFound); Thread.sleep(10000); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '2'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); cqlRow = cqlRows.get(0); for (Column column : cqlRow.getColumns()) { String columnName = new String(column.getName(), Constants.ENCODING); if (columnName.equals("ttl(PERSON_NAME)")) { Assert.assertEquals(null, null); personNameFound = true; } else if (columnName.equals("ttl(AGE)")) { Assert.assertEquals(null, column.getValue()); ageFound = true; } if (columnName.equals("PERSON_NAME")) { Assert.fail( "PERSON_NAME column not deleted even though a TTL of 10 seconds was specified while writing to cassandra."); } } // TTL per session. ttlValues = new HashMap<String, Integer>(); ttlValues.put("PERSONCASSANDRA", new Integer(10)); em.setProperty("ttl.per.session", true); em.setProperty("ttl.values", ttlValues); Object p3 = prepareData("3", 10); em.persist(p3); em.clear(); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '3'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); cqlRow = cqlRows.get(0); personNameFound = false; ageFound = false; for (Column column : cqlRow.getColumns()) { String columnName = new String(column.getName(), Constants.ENCODING); if (columnName.equals("ttl(PERSON_NAME)")) { Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); personNameFound = true; } else if (columnName.equals("ttl(AGE)")) { Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); ageFound = true; } } Assert.assertTrue(personNameFound && ageFound); Thread.sleep(10000); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '3'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); try { cqlRow = cqlRows.get(0); Assert.fail( "PERSON_NAME and AGE column not deleted even though a TTL of 5 seconds was specified while writing to cassandra."); } catch (IndexOutOfBoundsException ioobe) { Assert.assertTrue(cqlRows.isEmpty()); } Object p4 = prepareData("4", 10); ttlValues = new HashMap<String, Integer>(); ttlValues.put("PERSONCASSANDRA", new Integer(10)); em.setProperty("ttl.per.session", true); em.setProperty("ttl.values", ttlValues); em.persist(p4); em.clear(); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '4'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); cqlRow = cqlRows.get(0); personNameFound = false; ageFound = false; for (Column column : cqlRow.getColumns()) { String columnName = new String(column.getName(), Constants.ENCODING); if (columnName.equals("ttl(PERSON_NAME)")) { Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); personNameFound = true; } else if (columnName.equals("ttl(AGE)")) { Assert.assertEquals(10, ByteBufferUtil.toInt(ByteBuffer.wrap(column.getValue()))); ageFound = true; } } Assert.assertTrue(personNameFound && ageFound); Thread.sleep(10000); cqlResult = CassandraCli.client.execute_cql3_query(ByteBuffer .wrap("Select ttl(\"PERSON_NAME\"), ttl(\"AGE\") from \"PERSONCASSANDRA\" where \"personId\" = '4'" .getBytes()), Compression.NONE, ConsistencyLevel.ONE); cqlRows = cqlResult.getRows(); try { cqlRow = cqlRows.get(0); Assert.fail( "PERSON_NAME and AGE column not deleted even though a TTL of 5 seconds was specified while writing to cassandra."); } catch (IndexOutOfBoundsException ioobe) { Assert.assertTrue(cqlRows.isEmpty()); } String deleteQuery = "DELETE from PersonCassandra"; q = em.createQuery(deleteQuery); Assert.assertEquals(1, q.executeUpdate()); }
From source file:com.protectwise.cassandra.db.compaction.example.OddPartitionKeyDeleter.java
License:Apache License
@Override public boolean shouldKeepPartition(OnDiskAtomIterator key) { ByteBuffer lastField = key.getKey().getKey(); if (lastField.hasRemaining()) { byte[] data = new byte[lastField.remaining()]; lastField.duplicate().get(data); byte lastByte = data[data.length - 1]; Map<ByteBuffer, ByteBuffer> pks = getNamedPkColumns(key); int a = ByteBufferUtil.toInt(pks.get(ByteBufferUtil.bytes("a"))); return a % 2 == 0; } else {//ww w . j av a2 s . c o m return true; } }