public static void main(String[] args) throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException, UnsupportedEncodingException { TTransport tr = new TSocket("192.168.10.2", 9160); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); String keyspace = "Keyspace1"; String cf = "Standard2"; String key = "studentA"; // Insert long timestamp = System.currentTimeMillis(); ColumnPath path = new ColumnPath(cf); path.setColumn("age".getBytes("UTF-8")); client.insert(keyspace, key, path, "18".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE); path.setColumn("height".getBytes("UTF-8")); client.insert(keyspace, key, path, "172cm".getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE); // Read path.setColumn("height".getBytes("UTF-8")); ColumnOrSuperColumn cc = client.get(keyspace, key, path, ConsistencyLevel.ONE); Column c = cc.getColumn(); String v = new String(c.value, "UTF-8"); System.out.println("Read studentA height:" + v); tr.close(); }
@Test public void testBatchMutateBatchMutation() throws HectorException { BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get()); List<String> columnFamilies = Arrays.asList("Standard1"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Column col = new Column( StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); } } keyspace.batchMutate(batchMutation); // get value for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testBatchMutateColumn_" + j)); Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp); assertNotNull(col); String value = string(col.getValue()); assertEquals("testBatchMutateColumn_value_" + j, value); } } batchMutation = new BatchMutation<String>(StringSerializer.get()); // batch_mutate delete by key for (int i = 0; i < 10; i++) { SlicePredicate slicePredicate = new SlicePredicate(); for (int j = 0; j < 10; j++) { slicePredicate.addToColumn_names( StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); } Deletion deletion = new Deletion(connectionManager.createClock()); deletion.setPredicate(slicePredicate); batchMutation.addDeletion("testBatchMutateColumn_" + i, columnFamilies, deletion); } keyspace.batchMutate(batchMutation); // make sure the values are gone for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testBatchMutateColumn_" + j)); try { keyspace.getColumn("testBatchMutateColumn_" + i, cp); fail(); } catch (HNotFoundException e) { // good, we want this to throw. } } } }
@Test public void testInsertAndGetAndRemoveCounter() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { StringSerializer ss = StringSerializer.get(); // insert value ColumnParent cp = new ColumnParent("Counter1"); // cp.setColumn(bytes("testInsertAndGetAndRemoveCounter")); // Insert 3 counters for the same key keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", 5L)); keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("A", -1L)); keyspace.addCounter("testInsertAndGetAndRemoveCounter_key1", cp, createCounterColumn("B", 10L)); // Total for counter A is (5 - 1) = 4. // Total for counter B is 10. ColumnPath cph = new ColumnPath("Counter1"); cph.setColumn(ss.toByteBuffer("A")); Counter counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(4, counter.getColumn().value); cph.setColumn(ss.toByteBuffer("B")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); assertEquals(10, counter.getColumn().value); // Reuse the ColumnPath associated to Conter B and remove only B. keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); // Fetch it and it should not exist try { keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); } catch (HNotFoundException e) { // good } // Fetch Counter A again to verify I did not delete it cph.setColumn(ss.toByteBuffer("A")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); assertNotNull(counter); // Delete the whole row cph.column = null; keyspace.removeCounter("testInsertAndGetAndRemoveCounter_key1", cph); // Fetch A again to verify it is not there. try { cph.setColumn(ss.toByteBuffer("A")); counter = keyspace.getCounter("testInsertAndGetAndRemoveCounter_key1", cph); } catch (HNotFoundException e) { // good } }
@Test public void testBatchUpdateInsertAndDelOnSame() throws HectorException { ColumnPath sta1 = new ColumnPath("Standard1"); sta1.setColumn(bytes("deleteThroughInserBatch_col")); keyspace.insert( "deleteThroughInserBatch_key", sta1, StringSerializer.get().toByteBuffer("deleteThroughInserBatch_val")); Column found = keyspace.getColumn("deleteThroughInserBatch_key", sta1); assertNotNull(found); BatchMutation<String> batchMutation = new BatchMutation<String>(StringSerializer.get()); List<String> columnFamilies = Arrays.asList("Standard1"); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Column col = new Column( StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); batchMutation.addInsertion("testBatchMutateColumn_" + i, columnFamilies, col); } } SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.addToColumn_names( StringSerializer.get().toByteBuffer("deleteThroughInserBatch_col")); Deletion deletion = new Deletion(connectionManager.createClock()); deletion.setPredicate(slicePredicate); batchMutation.addDeletion("deleteThroughInserBatch_key", columnFamilies, deletion); keyspace.batchMutate(batchMutation); try { keyspace.getColumn("deleteThroughInserBatch_key", sta1); fail("Should not have found a value here"); } catch (Exception e) { } // get value for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testBatchMutateColumn_" + j)); Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp); assertNotNull(col); String value = string(col.getValue()); assertEquals("testBatchMutateColumn_value_" + j, value); } } }
@Test public void testMultigetColumn() throws HectorException { // insert value ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testMultigetColumn")); ArrayList<String> keys = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { keyspace.insert( "testMultigetColumn_" + i, cp, StringSerializer.get().toByteBuffer("testMultigetColumn_value_" + i)); keys.add("testMultigetColumn_" + i); } // get value /* Map<String, Column> ms = keyspace.multigetColumn(keys, cp); for (int i = 0; i < 100; i++) { Column cl = ms.get(keys.get(i)); assertNotNull(cl); assertEquals("testMultigetColumn_value_" + i, string(cl.getValue())); } */ // remove value for (int i = 0; i < 100; i++) { keyspace.remove("testMultigetColumn_" + i, cp); } }
@Test public void testGetSlice() throws HectorException { // insert value ArrayList<String> columnnames = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetSlice_" + i)); keyspace.insert( "testGetSlice", cp, StringSerializer.get().toByteBuffer("testGetSlice_Value_" + i)); columnnames.add("testGetSlice_" + i); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); List<Column> cols = keyspace.getSlice("testGetSlice", clp, sp); assertNotNull(cols); assertEquals(100, cols.size()); Collections.sort(columnnames); ArrayList<String> gotlist = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { gotlist.add(string(cols.get(i).getName())); } assertEquals(columnnames, gotlist); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetSlice_", cp); keyspace.remove("testGetSlice", cp); }
@Test public void testMultigetSlice() throws HectorException { // insert value ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testMultigetSlice")); ArrayList<String> keys = new ArrayList<String>(100); for (int i = 0; i < 100; i++) { keyspace.insert( "testMultigetSlice_" + i, cp, StringSerializer.get().toByteBuffer("testMultigetSlice_value_" + i)); keys.add("testMultigetSlice_" + i); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); Map<String, List<Column>> ms = se.fromBytesMap(keyspace.multigetSlice(se.toBytesList(keys), clp, sp)); for (int i = 0; i < 100; i++) { List<Column> cl = ms.get(keys.get(i)); assertNotNull(cl); assertEquals(1, cl.size()); assertTrue(string(cl.get(0).getValue()).startsWith("testMultigetSlice_")); } // remove value for (int i = 0; i < 100; i++) { keyspace.remove("testMultigetSlice_" + i, cp); } }
public static void main(String[] args) throws HectorException { CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get(); CassandraClient client = pool.borrowClient("tush", 9160); // A load balanced version would look like this: // CassandraClient client = pool.borrowClient(new String[] {"cas1:9160", "cas2:9160", // "cas3:9160"}); try { Keyspace keyspace = client.getKeyspace("Keyspace1"); ColumnPath columnPath = new ColumnPath("Standard1"); ColumnParent columnParent = new ColumnParent("Standard1"); columnPath.setColumn(bytes("column-name")); // insert keyspace.insert( bytes("key"), columnParent, new Column(bytes("column-name"), bytes("value"), keyspace.createClock())); // read Column col = keyspace.getColumn(bytes("key"), columnPath); System.out.println("Read from cassandra: " + string(col.getValue())); // This line makes sure that even if the client had failures and recovered, a correct // releaseClient is called, on the up to date client. client = keyspace.getClient(); } finally { // return client to pool. do it in a finally block to make sure it's executed pool.releaseClient(client); } }
@PooledConnection public void deleteColumn( String keyspace, String column_family, String key, String column, ConsistencyLevel consistency_level, boolean purgeIndex) throws InvalidRequestException, UnavailableException, TimedOutException, TException, HttpException, IOException { ColumnPath path = new ColumnPath(column_family); path.setColumn(ByteBufferUtil.bytes(column)); getConnection(keyspace) .remove( ByteBufferUtil.bytes(key), path, System.currentTimeMillis() * 1000, consistency_level); // TODO: Revisit deleting a single field because it requires a fetch // first. // Evidently it is impossible to remove just a field from a document in // SOLR // http://stackoverflow.com/questions/4802620/can-you-delete-a-field-from-a-document-in-solr-index if (config.isIndexingEnabled() && purgeIndex) { indexer.delete(column_family, key); JSONObject json = this.getSlice(keyspace, column_family, key, consistency_level); json.remove(column); indexer.index(column_family, key, json); } }
@Test public void testGetSuperSlice() throws HectorException { // insert value for (int i = 0; i < 100; i++) { ColumnPath cp = new ColumnPath("Super1"); cp.setSuper_column(bytes("SuperColumn_1")); cp.setColumn(bytes("testGetSuperSlice_" + i)); ColumnPath cp2 = new ColumnPath("Super1"); cp2.setSuper_column(bytes("SuperColumn_2")); cp2.setColumn(bytes("testGetSuperSlice_" + i)); keyspace.insert( "testGetSuperSlice", cp, StringSerializer.get().toByteBuffer("testGetSuperSlice_Value_" + i)); keyspace.insert( "testGetSuperSlice", cp2, StringSerializer.get().toByteBuffer("testGetSuperSlice_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Super1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); List<SuperColumn> cols = keyspace.getSuperSlice("testGetSuperSlice", clp, sp); assertNotNull(cols); assertEquals(2, cols.size()); ColumnPath cp = new ColumnPath("Super1"); keyspace.remove("testGetSuperSlice", cp); }
@PooledConnection public String getColumn( String keyspace, String columnFamily, String key, String column, ConsistencyLevel consistencyLevel) throws InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException, UnsupportedEncodingException { ColumnPath path = new ColumnPath(columnFamily); path.setColumn(ByteBufferUtil.bytes(column)); ColumnOrSuperColumn column_result = getConnection(keyspace).get(ByteBufferUtil.bytes(key), path, consistencyLevel); return new String(column_result.getColumn().getValue(), "UTF8"); }
@Test public void testGetRangeSlices() throws HectorException { for (int i = 0; i < 10; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetRangeSlices_" + i)); keyspace.insert( "testGetRangeSlices0", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert( "testGetRangeSlices1", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); keyspace.insert( "testGetRangeSlices2", cp, StringSerializer.get().toByteBuffer("testGetRangeSlices_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); KeyRange range = new KeyRange(); range.setStart_key("".getBytes()); range.setEnd_key("".getBytes()); Map<String, List<Column>> keySlices = se.fromBytesMap(keyspace.getRangeSlices(clp, sp, range)); assertNotNull(keySlices); assertNotNull("testGetRangeSlices1 is null", keySlices.get("testGetRangeSlices1")); assertEquals( "testGetRangeSlices_Value_0", string(keySlices.get("testGetRangeSlices1").get(0).getValue())); assertEquals(10, keySlices.get("testGetRangeSlices1").size()); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetRanageSlices0", cp); keyspace.remove("testGetRanageSlices1", cp); keyspace.remove("testGetRanageSlices2", cp); }
@Test public void testGetRangeSlice() throws HectorException { for (int i = 0; i < 10; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testGetRangeSlice_" + i)); keyspace.insert( "testGetRangeSlice0", cp, StringSerializer.get().toByteBuffer("testGetRangeSlice_Value_" + i)); keyspace.insert( "testGetRangeSlice1", cp, StringSerializer.get().toByteBuffer("testGetRangeSlice_Value_" + i)); keyspace.insert( "testGetRangeSlice2", cp, StringSerializer.get().toByteBuffer("testGetRangeSlice_Value_" + i)); } // get value ColumnParent clp = new ColumnParent("Standard1"); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); SlicePredicate sp = new SlicePredicate(); sp.setSlice_range(sr); /* @SuppressWarnings("deprecation") Map<String, List<Column>> keySlices = keyspace.getRangeSlice(clp, sp, "testGetRangeSlice0", "testGetRangeSlice3", 5); assertNotNull(keySlices); assertEquals(3, keySlices.size()); assertNotNull("testGetRangeSlice1 is null", keySlices.get("testGetRangeSlice1")); assertEquals("testGetRangeSlice_Value_0", string(keySlices.get("testGetRangeSlice1").get(0).getValue())); assertEquals(10, keySlices.get("testGetRangeSlice1").size()); */ ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetRanageSlice0", cp); keyspace.remove("testGetRanageSlice1", cp); keyspace.remove("testGetRanageSlice2", cp); }
@Test public void testGetCount() throws HectorException { // insert values for (int i = 0; i < 100; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testInsertAndGetAndRemove_" + i)); keyspace.insert( "testGetCount", cp, StringSerializer.get().toByteBuffer("testInsertAndGetAndRemove_value_" + i)); } // get value ColumnParent clp = new ColumnParent("Standard1"); // int count = keyspace.getCount("testGetCount", clp, se); // assertEquals(100, count); ColumnPath cp = new ColumnPath("Standard1"); keyspace.remove("testGetCount", cp); }
@Test public void testMultigetCount() { // insert 25 columns into 10 rows List<ByteBuffer> keys = new ArrayList<ByteBuffer>(); for (int j = 0; j < 10; j++) { for (int i = 0; i < 25; i++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(StringSerializer.get().toByteBuffer("testMultigetCount_column_" + i)); keyspace.insert( "testMultigetCount_key_" + j, cp, StringSerializer.get().toByteBuffer("testMultigetCount_value_" + i)); } if (j % 2 == 0) { keys.add(StringSerializer.get().toByteBuffer("testMultigetCount_key_" + j)); } } // get value ColumnParent clp = new ColumnParent("Standard1"); SlicePredicate slicePredicate = new SlicePredicate(); SliceRange sr = new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 150); slicePredicate.setSlice_range(sr); Map<ByteBuffer, Integer> counts = keyspace.multigetCount(keys, clp, slicePredicate); assertEquals(5, counts.size()); assertEquals(new Integer(25), counts.entrySet().iterator().next().getValue()); slicePredicate.setSlice_range( new SliceRange( StringSerializer.get().toByteBuffer(""), StringSerializer.get().toByteBuffer(""), false, 5)); counts = keyspace.multigetCount(keys, clp, slicePredicate); assertEquals(5, counts.size()); assertEquals(new Integer(5), counts.entrySet().iterator().next().getValue()); }
@Test public void testInsertAndGetAndRemove() throws IllegalArgumentException, NoSuchElementException, IllegalStateException, HNotFoundException, Exception { // insert value ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testInsertAndGetAndRemove")); for (int i = 0; i < 100; i++) { keyspace.insert( "testInsertAndGetAndRemove_" + i, cp, StringSerializer.get().toByteBuffer("testInsertAndGetAndRemove_value_" + i)); } // get value for (int i = 0; i < 100; i++) { Column col = keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp); assertNotNull(col); String value = string(col.getValue()); assertEquals("testInsertAndGetAndRemove_value_" + i, value); } // remove value for (int i = 0; i < 100; i++) { keyspace.remove("testInsertAndGetAndRemove_" + i, cp); } // get already removed value for (int i = 0; i < 100; i++) { try { keyspace.getColumn("testInsertAndGetAndRemove_" + i, cp); fail("the value should already being deleted"); } catch (HNotFoundException e) { // good } } }
@Test public void testBatchMutate() throws HectorException { Map<String, Map<String, List<Mutation>>> outerMutationMap = new HashMap<String, Map<String, List<Mutation>>>(); for (int i = 0; i < 10; i++) { Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>(); ArrayList<Mutation> mutations = new ArrayList<Mutation>(10); for (int j = 0; j < 10; j++) { Column col = new Column( StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j), StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j), connectionManager.createClock()); // list.add(col); ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); cosc.setColumn(col); Mutation mutation = new Mutation(); mutation.setColumn_or_supercolumn(cosc); mutations.add(mutation); } mutationMap.put("Standard1", mutations); outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap); } keyspace.batchMutate(se.toBytesMap(outerMutationMap)); // re-use later outerMutationMap.clear(); // get value for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testBatchMutateColumn_" + j)); Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp); assertNotNull(col); String value = string(col.getValue()); assertEquals("testBatchMutateColumn_value_" + j, value); } } // batch_mutate delete by key for (int i = 0; i < 10; i++) { ArrayList<Mutation> mutations = new ArrayList<Mutation>(10); Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>(); SlicePredicate slicePredicate = new SlicePredicate(); for (int j = 0; j < 10; j++) { slicePredicate.addToColumn_names( StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j)); } Mutation mutation = new Mutation(); Deletion deletion = new Deletion(connectionManager.createClock()); deletion.setPredicate(slicePredicate); mutation.setDeletion(deletion); mutations.add(mutation); mutationMap.put("Standard1", mutations); outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap); } keyspace.batchMutate(se.toBytesMap(outerMutationMap)); // make sure the values are gone for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { ColumnPath cp = new ColumnPath("Standard1"); cp.setColumn(bytes("testBatchMutateColumn_" + j)); try { keyspace.getColumn("testBatchMutateColumn_" + i, cp); fail(); } catch (HNotFoundException e) { } } } }