@PooledConnection public void setColumn( String keyspace, String column_family, String key, JSONObject json, ConsistencyLevel consistency_level, boolean index, long timestamp) throws InvalidRequestException, UnavailableException, TimedOutException, TException, HttpException, IOException { List<Mutation> slice = new ArrayList<Mutation>(); for (Object field : json.keySet()) { String name = (String) field; String value = (String) json.get(name); Column c = new Column(); c.setName(ByteBufferUtil.bytes(name)); c.setValue(ByteBufferUtil.bytes(value)); c.setTimestamp(timestamp); Mutation m = new Mutation(); ColumnOrSuperColumn cc = new ColumnOrSuperColumn(); cc.setColumn(c); m.setColumn_or_supercolumn(cc); slice.add(m); } Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); Map<String, List<Mutation>> cfMutations = new HashMap<String, List<Mutation>>(); cfMutations.put(column_family, slice); mutationMap.put(ByteBufferUtil.bytes(key), cfMutations); getConnection(keyspace).batch_mutate(mutationMap, consistency_level); if (config.isIndexingEnabled() && index) indexer.index(column_family, key, json); }
public final List<ColumnOrSuperColumn> getTermDocFreq() { if (termBuffer.length == 0) return null; List<ColumnOrSuperColumn> termDocs = termDocFreqBuffer.get(termBuffer[termPosition]); // create proper docIds. // Make sure these ids are sorted in ascending order since lucene // requires this. int docIds[] = new int[termDocs.size()]; int idx = 0; List<ColumnOrSuperColumn> sortedTermDocs = new ArrayList<ColumnOrSuperColumn>(termDocs.size()); Map<Integer, ColumnOrSuperColumn> termDocMap = new HashMap<Integer, ColumnOrSuperColumn>(); for (ColumnOrSuperColumn col : termDocs) { int docId = indexReader.addDocument(col.getSuper_column(), currentField); termDocMap.put(docId, col); docIds[idx++] = docId; } // sort Arrays.sort(docIds); // move for (idx = 0; idx < termDocs.size(); idx++) { sortedTermDocs.add(termDocMap.get(docIds[idx])); } return sortedTermDocs; }
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(); }
/** * Insert a record in the database. Any field/value pairs in the specified values HashMap will be * written into the record with the specified record key. * * @param table The name of the table * @param key The record key of the record to insert. * @param values A HashMap of field/value pairs to insert in the record * @return Zero on success, a non-zero error code on error */ public int insert(String table, String key, HashMap<String, ByteIterator> values) { if (!_table.equals(table)) { try { client.set_keyspace(table); _table = table; } catch (Exception e) { e.printStackTrace(); e.printStackTrace(System.out); return Error; } } for (int i = 0; i < OperationRetries; i++) { if (_debug) { System.out.println("Inserting key: " + key); } try { ByteBuffer wrappedKey = ByteBuffer.wrap(key.getBytes("UTF-8")); ColumnOrSuperColumn column; for (Map.Entry<String, ByteIterator> entry : values.entrySet()) { column = new ColumnOrSuperColumn(); Column subColumn = new Column(ByteBuffer.wrap(entry.getKey().getBytes("UTF-8"))); subColumn.setValue(ByteBuffer.wrap(entry.getValue().toArray())); subColumn.setTimestamp(System.currentTimeMillis()); column.setColumn(subColumn); mutations.add(new Mutation().setColumn_or_supercolumn(column)); } mutationMap.put(column_family, mutations); record.put(wrappedKey, mutationMap); client.batch_mutate(record, ConsistencyLevel.ONE); mutations.clear(); mutationMap.clear(); record.clear(); return Ok; } catch (Exception e) { errorexception = e; } try { Thread.sleep(500); } catch (InterruptedException e) { } } errorexception.printStackTrace(); errorexception.printStackTrace(System.out); return Error; }
@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"); }
/** * Purge expired entries. Expiration entries are stored in a single key (expirationKey) within a * specific ColumnFamily (set by configuration). The entries are grouped by expiration timestamp * in SuperColumns within which each entry's key is mapped to a column */ @Override protected void purgeInternal() throws CacheLoaderException { if (trace) log.trace("purgeInternal"); Cassandra.Client cassandraClient = null; try { cassandraClient = dataSource.getConnection(); // We need to get all supercolumns from the beginning of time until // now, in SLICE_SIZE chunks SlicePredicate predicate = new SlicePredicate(); predicate.setSlice_range( new SliceRange( ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.bytes(System.currentTimeMillis()), false, SLICE_SIZE)); Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap = new HashMap<ByteBuffer, Map<String, List<Mutation>>>(); for (boolean complete = false; !complete; ) { // Get all columns List<ColumnOrSuperColumn> slice = cassandraClient.get_slice( expirationKey, expirationColumnParent, predicate, readConsistencyLevel); complete = slice.size() < SLICE_SIZE; // Delete all keys returned by the slice for (ColumnOrSuperColumn crumb : slice) { SuperColumn scol = crumb.getSuper_column(); for (Iterator<Column> i = scol.getColumnsIterator(); i.hasNext(); ) { Column col = i.next(); // Remove the entry row remove0(ByteBuffer.wrap(col.getName()), mutationMap); } // Remove the expiration supercolumn addMutation( mutationMap, expirationKey, config.expirationColumnFamily, ByteBuffer.wrap(scol.getName()), null, null); } } cassandraClient.batch_mutate(mutationMap, writeConsistencyLevel); } catch (Exception e) { throw new CacheLoaderException(e); } finally { dataSource.releaseConnection(cassandraClient); } }
private void doSlice(String keyspace, String key, String columnFamily, byte[] superColumnName) throws InvalidRequestException, UnavailableException, TimedOutException, TException, UnsupportedEncodingException, IllegalAccessException, NotFoundException, InstantiationException, ClassNotFoundException { SliceRange range = new SliceRange(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.EMPTY_BYTE_ARRAY, true, 1000000); List<ColumnOrSuperColumn> columns = thriftClient_.get_slice( keyspace, key, createColumnParent(columnFamily, superColumnName), createSlicePredicate(null, range), ConsistencyLevel.ONE); int size = columns.size(); // Print out super columns or columns. for (ColumnOrSuperColumn cosc : columns) { if (cosc.isSetSuper_column()) { SuperColumn superColumn = cosc.super_column; css_.out.printf( "=> (super_column=%s,", formatSuperColumnName(keyspace, columnFamily, superColumn)); for (Column col : superColumn.getColumns()) css_.out.printf( "\n (column=%s, value=%s, timestamp=%d)", formatSubcolumnName(keyspace, columnFamily, col), new String(col.value, "UTF-8"), col.timestamp); css_.out.println(")"); } else { Column column = cosc.column; css_.out.printf( "=> (column=%s, value=%s, timestamp=%d)\n", formatColumnName(keyspace, columnFamily, column), new String(column.value, "UTF-8"), column.timestamp); } } css_.out.println("Returned " + size + " results."); }
private static void addMutation( Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap, ByteBuffer key, String columnFamily, ByteBuffer superColumn, ByteBuffer column, ByteBuffer value) { Map<String, List<Mutation>> keyMutations = mutationMap.get(key); // If the key doesn't exist yet, create the mutation holder if (keyMutations == null) { keyMutations = new HashMap<String, List<Mutation>>(); mutationMap.put(key, keyMutations); } // If the columnfamily doesn't exist yet, create the mutation holder List<Mutation> columnFamilyMutations = keyMutations.get(columnFamily); if (columnFamilyMutations == null) { columnFamilyMutations = new ArrayList<Mutation>(); keyMutations.put(columnFamily, columnFamilyMutations); } if (value == null) { // Delete Deletion deletion = new Deletion(microTimestamp()); if (superColumn != null) { deletion.setSuper_column(superColumn); } if (column != null) { // Single column delete deletion.setPredicate( new SlicePredicate().setColumn_names(Collections.singletonList(column))); } // else Delete entire column family or supercolumn columnFamilyMutations.add(new Mutation().setDeletion(deletion)); } else { // Insert/update ColumnOrSuperColumn cosc = new ColumnOrSuperColumn(); if (superColumn != null) { List<Column> columns = new ArrayList<Column>(); columns.add(new Column(column, value, microTimestamp())); cosc.setSuper_column(new SuperColumn(superColumn, columns)); } else { cosc.setColumn(new Column(column, value, microTimestamp())); } columnFamilyMutations.add(new Mutation().setColumn_or_supercolumn(cosc)); } }
@Override public InternalCacheEntry load(Object key) throws CacheLoaderException { String hashKey = hashKey(key); Cassandra.Client cassandraClient = null; try { cassandraClient = dataSource.getConnection(); ColumnOrSuperColumn column = cassandraClient.get(ByteBufferUtil.bytes(hashKey), entryColumnPath, readConsistencyLevel); InternalCacheEntry ice = unmarshall(column.getColumn().getValue(), key); if (ice != null && ice.isExpired()) { remove(key); return null; } return ice; } catch (NotFoundException nfe) { log.debugf("Key '%s' not found", hashKey); return null; } catch (Exception e) { throw new CacheLoaderException(e); } finally { dataSource.releaseConnection(cassandraClient); } }
/** Provides retrieving of a next column. */ private void shiftColumn() { if (m_iColumns.hasNext()) { // Current list may be used ColumnOrSuperColumn cosc = m_iColumns.next(); m_sliceSize++; Column column = cosc.getColumn(); m_next = new DColumn(column.getName(), column.getValue()); } else if (m_sliceSize < CassandraDefs.MAX_COLS_BATCH_SIZE) { // All columns were read; no sense to try to get more columns m_next = null; return; } else if (m_next != null) { // Save current column name String lastName = m_next.getName(); List<ColumnOrSuperColumn> columns = getNextSlice(lastName); m_sliceSize = 0; m_iColumns = columns.iterator(); if (!m_iColumns.hasNext()) { // column was deleted? We cannot iterate correctly... m_next = null; return; } // Normally the first column is the same as was the previous one. ColumnOrSuperColumn cosc = m_iColumns.next(); m_sliceSize++; Column column = cosc.getColumn(); m_next = new DColumn(column.getName(), column.getValue()); // Most probably we've got a column already read... // Shift from this column to the next one. if (lastName.equals(m_next.getName())) { // Just shift to next column shiftColumn(); } // else the column was deleted. // We cannot guarantee that the iteration will continue correctly. } } // shiftColumn
@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) { } } } }