public List<NotificationEntry> getNotifications(String username, String startId) { try { byte[] timeUUIDAsBytes = EMPTY_BYTE_ARRAY; if (startId != null && startId.length() > 0) { UUID uuid = UUIDUtil.fromString(startId); final byte[] idAsBytes = UUIDUtil.toBytes(uuid); timeUUIDAsBytes = getTimeUUIDFromID(username, idAsBytes); if (timeUUIDAsBytes == null) { timeUUIDAsBytes = EMPTY_BYTE_ARRAY; } } Selector selector = Pelops.createSelector(TRENDOCEAN_POOL, TRENDOCEAN_KEYSPACE); SlicePredicate columnPredicate = Selector.newColumnsPredicate(timeUUIDAsBytes, EMPTY_BYTE_ARRAY, true, PAGE_SIZE); List<SuperColumn> columns = selector.getSuperColumnsFromRow(username, WALL_CF, columnPredicate, ConsistencyLevel.ONE); List<NotificationEntry> notificationList = new ArrayList<NotificationEntry>(columns.size()); for (SuperColumn superColumn : columns) { List<Column> wallEntryColumns = superColumn.getColumns(); String notificationId = null; NotificationType notificationType = null; String questionId = null; String relatedUser = null; long answerCount = -1L; long time = 0; for (Column notificationColumn : wallEntryColumns) { String notificationColumnName = toString(notificationColumn.getName()); if (notificationColumnName.equals("_id")) { notificationId = UUIDUtil.toUUID(notificationColumn.getValue()).toString(); long timeInMicroSeconds = notificationColumn.getTimestamp(); time = timeInMicroSeconds / 1000L; } else if (notificationColumnName.equals("_type")) { notificationType = NotificationType.fromId(toByte(notificationColumn)); } else if (notificationColumnName.equals("questionId")) { questionId = toString(notificationColumn); } else if (notificationColumnName.equals("relatedUser")) { relatedUser = toString(notificationColumn); } else if (notificationColumnName.equals("answerCount")) { answerCount = toLong(notificationColumn); } else { logger.warn("Invalid notification column:{}", notificationColumnName); } } NotificationEntry notificationEntry = new NotificationEntry(); notificationEntry.setId(notificationId); notificationEntry.setTime(time); notificationEntry.setNotificationType(notificationType); notificationEntry.setQuestionId(questionId); notificationList.add(notificationEntry); notificationEntry.setUsername(relatedUser); notificationEntry.setAnswerCount(answerCount); } return notificationList; } catch (NotFoundException ex) { return Collections.EMPTY_LIST; } catch (Exception ex) { logger.error("Can not get time uuid", ex); throw new TrendOceanException(ex); } }
@SuppressWarnings("unchecked") @Override public List<GatewayVerb> getVerbs() { log.debug("Getting list with all active verb"); try { List<GatewayVerb> verbs = new ArrayList<GatewayVerb>(); Selector selector = Pelops.createSelector(schemaName); LinkedHashMap<Bytes, List<Column>> rows = selector.getColumnsFromRows( "verbs", Selector.newKeyRange("", "", 10000), // 10000 mixers limit, false, ConsistencyLevel.ONE); for (Map.Entry<Bytes, List<Column>> row : rows.entrySet()) { if (row.getValue().size() > 0) { for (Column column : row.getValue()) { GatewayVerb verb = new GatewayVerb( row.getKey().toUTF8(), Bytes.toUTF8(column.getName()), Bytes.toUTF8(column.getValue())); verbs.add(verb); } } } return verbs; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return Collections.EMPTY_LIST; } }
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); } }
private GatewayCall buildCall(List<Column> columns, String id) { if (columns != null && columns.size() > 0) { GatewayCall call = new GatewayCall(); call.setCallId(id); for (Column column : columns) { String name = Bytes.toUTF8(column.getName()); if (name.equals("node")) { call.setNodeJid(Bytes.toUTF8(column.getValue())); } if (name.equals("jid")) { call.setClientJid(Bytes.toUTF8(column.getValue())); } } return call; } return null; }
@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 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); } } }
private void populateApplicationData(Application application, List<Column> columns) { for (Column column : columns) { String name = Bytes.toUTF8(column.getName()); if (name.equals("appId")) { application.setAppId(Bytes.toUTF8((column.getValue()))); } if (name.equals("platformId")) { application.setPlatform(Bytes.toUTF8((column.getValue()))); } if (name.equals("name")) { application.setName(Bytes.toUTF8((column.getValue()))); } if (name.equals("accountId")) { application.setAccountId(Bytes.toUTF8((column.getValue()))); } if (name.equals("permissions")) { application.setPermissions(Bytes.toUTF8((column.getValue()))); } } }
private RayoNode buildNode(List<Column> columns) { if (columns != null && columns.size() > 0) { RayoNode node = new RayoNode(); for (Column column : columns) { String name = Bytes.toUTF8(column.getName()); if (name.equals("ip")) { node.setIpAddress(Bytes.toUTF8(column.getValue())); } if (name.equals("weight")) { node.setWeight(Integer.parseInt(Bytes.toUTF8(column.getValue()))); } if (name.equals("priority")) { node.setPriority(Integer.parseInt(Bytes.toUTF8(column.getValue()))); } if (name.equals("consecutive-errors")) { node.setConsecutiveErrors(Integer.parseInt(Bytes.toUTF8(column.getValue()))); } if (name.equals("blacklisted")) { node.setBlackListed(Boolean.valueOf(Bytes.toUTF8(column.getValue()))); } if (name.equals("platforms")) { node.setPlatforms( new HashSet<String>( Arrays.asList(StringUtils.split(Bytes.toUTF8(column.getValue()), ",")))); } } return node; } return null; }
@Override public Application getApplicationForAddress(String address) { log.debug("Finding application for address: [%s]", address); Selector selector = Pelops.createSelector(schemaName); List<Column> columns = selector.getColumnsFromRow("addresses", address, false, ConsistencyLevel.ONE); if (columns != null && columns.size() > 0) { Column column = columns.get(0); return getApplication(Bytes.toUTF8(column.getValue())); } log.debug("No application found for address: [%s]", address); return null; }
private byte[] getTimeUUIDFromID(String username, byte[] idAsBytes) { try { Selector selector = Pelops.createSelector(TRENDOCEAN_POOL, TRENDOCEAN_KEYSPACE); Column column = selector.getColumnFromRow(username, ID_TO_TIMEUUID_CF, idAsBytes, ConsistencyLevel.ONE); byte[] timeUUIDAsBytes = column.getValue(); return timeUUIDAsBytes; } catch (NotFoundException ex) { return null; } catch (Exception ex) { logger.error("Can not get time uuid", ex); throw new TrendOceanException(ex); } }
/** 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
@SuppressWarnings("unchecked") private Collection<String> getCalls(String jid, String type) { try { Selector selector = Pelops.createSelector(schemaName); List<Column> columns = selector.getSubColumnsFromRow("jids", type, jid, false, ConsistencyLevel.ONE); List<String> calls = new ArrayList<String>(); for (Column column : columns) { calls.add(Bytes.toUTF8(column.getValue())); } return calls; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return Collections.EMPTY_LIST; } }
@Override public String getNodeForIpAddress(String ip) { try { log.debug("Finding node for IP address: [%s]", ip); Selector selector = Pelops.createSelector(schemaName); Column column = selector.getColumnFromRow("ips", ip, "node", ConsistencyLevel.ONE); if (column != null) { return Bytes.toUTF8(column.getValue()); } } catch (NotFoundException nfe) { log.debug("No node found for ip address: [%s]", ip); return null; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); } return null; }
@Override public GatewayVerb getVerb(String mixerName, String id) { log.debug("Getting the verb [%s] from mixer [%s]", id, mixerName); Selector selector = Pelops.createSelector(schemaName); try { List<Column> columns = selector.getColumnsFromRow("verbs", mixerName, false, ConsistencyLevel.ONE); for (Column column : columns) { if (Bytes.toUTF8(column.getName()).equals(id)) { return new GatewayVerb(mixerName, id, Bytes.toUTF8(column.getValue())); } } } catch (Exception e) { log.error(e.getMessage(), e); } return null; }
private GatewayMixer buildMixer(List<Column> columns, String name) { String nodeJid = null; List<String> participants = new ArrayList<String>(); if (columns != null && columns.size() > 0) { for (Column column : columns) { String columnName = Bytes.toUTF8(column.getName()); if (columnName.equals("node")) { nodeJid = Bytes.toUTF8(column.getValue()); } else { participants.add(Bytes.toUTF8(column.getName())); } } if (nodeJid != null) { GatewayMixer mixer = new GatewayMixer(name, nodeJid); mixer.addCalls(participants); return mixer; } } return null; }
@Override public Collection<String> getCalls() { log.debug("Getting list with all active calls"); try { Selector selector = Pelops.createSelector(schemaName); List<String> calls = new ArrayList<String>(); List<SuperColumn> cols = selector.getSuperColumnsFromRow("jids", "nodes", false, ConsistencyLevel.ONE); for (SuperColumn col : cols) { List<Column> columns = col.getColumns(); for (Column column : columns) { calls.add(Bytes.toUTF8(column.getValue())); } } return calls; } catch (PelopsException pe) { log.error(pe.getMessage(), pe); return null; } }
@SuppressWarnings("unchecked") @Override public List<GatewayVerb> getVerbs(String mixerName) { log.debug("Getting the list of active verbs for mixer [%s]", mixerName); List<GatewayVerb> ids = new ArrayList<GatewayVerb>(); Selector selector = Pelops.createSelector(schemaName); try { List<Column> columns = selector.getColumnsFromRow("verbs", mixerName, false, ConsistencyLevel.ONE); for (Column column : columns) { GatewayVerb verb = new GatewayVerb( mixerName, Bytes.toUTF8(column.getName()), Bytes.toUTF8(column.getValue())); ids.add(verb); } } catch (Exception e) { log.error(e.getMessage(), e); return Collections.EMPTY_LIST; } return ids; }
@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 } } }
@Override public Date getDateValue() { return DateSerializer.get().fromBytes(column.getValue()); }
@Override public ByteBuffer getByteBufferValue() { return ByteBufferSerializer.get().fromBytes(column.getValue()); }
@Override public boolean getBooleanValue() { return BooleanSerializer.get().fromBytes(column.getValue()); }
@Override public byte[] getByteArrayValue() { return BytesArraySerializer.get().fromBytes(column.getValue()); }
@Override public long getLongValue() { return LongSerializer.get().fromBytes(column.getValue()); }
@Override public int getIntegerValue() { return IntegerSerializer.get().fromBytes(column.getValue()); }
@Override public String getStringValue() { return StringSerializer.get().fromBytes(column.getValue()); }
@Override public <V> V getValue(Serializer<V> valSer) { return valSer.fromBytes(column.getValue()); }
@Override public double getDoubleValue() { return DoubleSerializer.get().fromBytes(column.getValue()); }
@Override public UUID getUUIDValue() { return UUIDSerializer.get().fromBytes(column.getValue()); }
@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) { } } } }