public void runQuery() throws IOException { MultigetSliceQuery<DataPointsRowKey, Integer, ByteBuffer> msliceQuery = HFactory.createMultigetSliceQuery( m_keyspace, ROW_KEY_SERIALIZER, IntegerSerializer.get(), ByteBufferSerializer.get()); msliceQuery.setColumnFamily(m_columnFamily); msliceQuery.setKeys(m_rowKeys); if (m_descending) msliceQuery.setRange(m_endTime, m_startTime, true, m_multiRowReadSize); else msliceQuery.setRange(m_startTime, m_endTime, false, m_multiRowReadSize); Rows<DataPointsRowKey, Integer, ByteBuffer> rows = msliceQuery.execute().get(); List<Row<DataPointsRowKey, Integer, ByteBuffer>> unfinishedRows = new ArrayList<Row<DataPointsRowKey, Integer, ByteBuffer>>(); for (Row<DataPointsRowKey, Integer, ByteBuffer> row : rows) { List<HColumn<Integer, ByteBuffer>> columns = row.getColumnSlice().getColumns(); if (!m_limit && columns.size() == m_multiRowReadSize) unfinishedRows.add(row); writeColumns(row.getKey(), columns); } // Iterate through the unfinished rows and get the rest of the data. // todo: use multiple threads to retrieve this data for (Row<DataPointsRowKey, Integer, ByteBuffer> unfinishedRow : unfinishedRows) { DataPointsRowKey key = unfinishedRow.getKey(); SliceQuery<DataPointsRowKey, Integer, ByteBuffer> sliceQuery = HFactory.createSliceQuery( m_keyspace, ROW_KEY_SERIALIZER, IntegerSerializer.get(), ByteBufferSerializer.get()); sliceQuery.setColumnFamily(m_columnFamily); sliceQuery.setKey(key); List<HColumn<Integer, ByteBuffer>> columns = unfinishedRow.getColumnSlice().getColumns(); do { Integer lastTime = columns.get(columns.size() - 1).getName(); if (m_descending) sliceQuery.setRange(lastTime - 1, m_startTime, true, m_singleRowReadSize); else sliceQuery.setRange(lastTime + 1, m_endTime, false, m_singleRowReadSize); columns = sliceQuery.execute().get().getColumns(); writeColumns(key, columns); } while (columns.size() == m_singleRowReadSize); } }
private Serializer<?> serializerForComparator(String c) { int p = c.indexOf('('); if (p >= 0) { c = c.substring(0, p); } if (LEXICALUUIDTYPE.getTypeName().equals(c) || TIMEUUIDTYPE.getTypeName().equals(c)) { return UUIDSerializer.get(); } Serializer<?> s = SERIALIZERS.getInstance(serializerToComparatorMapping.inverse().get(c)); if (s != null) { return s; } return ByteBufferSerializer.get(); }
public void save(Collection<Count> counts) { Map<String, Count> countHolder = new HashMap<String, Count>(); for (Count count : counts) { Count c = countHolder.get(count.getCounterName()); if (c != null) { c.apply(count); } else { countHolder.put(count.getCounterName(), count); } } Mutator<ByteBuffer> mutator = HFactory.createMutator(keyspace, ByteBufferSerializer.get()); for (Count count : countHolder.values()) { mutator.addCounter( count.getKeyNameBytes(), count.getTableName(), new HCounterColumnImpl( count.getColumnName(), count.getValue(), count.getColumnNameSerializer())); } try { mutator.execute(); } catch (Exception e) { // errors here happen a lot on shutdown, don't fill the logs with them String error = e.getClass().getCanonicalName(); if (counterInsertFailures.get(error) == null) { log.error("CounterStore insert failed, first instance", e); counterInsertFailures.put(error, 1); } else { int count = counterInsertFailures.get(error) + 1; counterInsertFailures.put(error, count); if (log.isDebugEnabled()) { log.debug(error + " caused CounterStore insert failure, count = " + count, e); } else { log.error(error + " caused CounterStore insert failure, count = " + count); } } } }
public CassandraWriteWork(Keyspace keyspace, CassandraWorkStatus callback) { this.callback = callback; mutator = HFactory.createMutator(keyspace, ByteBufferSerializer.get()); }
public void setByteBuffer(N columnName, ByteBuffer value, int ttl) { addInsertion(columnName, value, ByteBufferSerializer.get(), globalTtl); }
public class AbstractOnRamp implements IOnRamp { // Configurable properties protected String darkStarNode; protected int darkStarPort; protected String clusterName; protected String keyspace; protected String eventName; // Client interface vars protected Mutator<ByteBuffer> mutator; protected Cluster cluster; protected Keyspace keySpace; protected StringSerializer se = StringSerializer.get(); protected LongSerializer ls = LongSerializer.get(); protected ByteBufferSerializer bfs = ByteBufferSerializer.get(); protected BytesArraySerializer bas = BytesArraySerializer.get(); protected final ConsistencyLevelPolicy policy = new DefaultConsistencyLevel(); protected static final String timestamp_field = "_onRampSent"; protected void setProperties(BaseConfig info) { darkStarNode = info.getDarkStarNode(); darkStarPort = info.getDarkStarPort(); clusterName = info.getClusterName(); keyspace = info.getKeyspace(); eventName = info.getEventName(); } public void setProperties( String darkStarNode, int darkStarPort, String clusterName, String keyspace, String eventName) { this.darkStarNode = darkStarNode; this.darkStarPort = darkStarPort; this.clusterName = clusterName; this.keyspace = keyspace; this.eventName = eventName; } public void init() { CassandraHostConfigurator hostConfig = new CassandraHostConfigurator(darkStarNode + ":" + String.valueOf(darkStarPort)); cluster = HFactory.createCluster(clusterName, hostConfig); keySpace = HFactory.createKeyspace(keyspace, cluster, policy); mutator = HFactory.createMutator(keySpace, bfs); } public void send(EventObject event, String key) throws JSONException { ByteBuffer rowKey = se.toByteBuffer(key); event.put(timestamp_field, System.currentTimeMillis()); mutator.addInsertion(rowKey, "system", createColumn(eventName, event.toString(), se, se)); mutator.execute(); } public void send(EventObject event) throws JSONException { ByteBuffer rowKey = se.toByteBuffer(event.getString("partition_on")); event.put(timestamp_field, System.currentTimeMillis()); mutator.addInsertion( rowKey, keyspace, createColumn(event.getEventName(), event.toString(), se, se)); mutator.execute(); } }
/** * Parent class of Composite and DynamicComposite. Acts as a list of objects that get serialized * into a composite column name. Unless setAutoDeserialize(true) is called, it's going to try to * match serializers to Cassandra comparator types. * * @author edanuff */ @SuppressWarnings("rawtypes") public abstract class AbstractComposite extends AbstractList<Object> implements Comparable<AbstractComposite> { public static Logger log = LoggerFactory.getLogger(AbstractComposite.class); public enum ComponentEquality { LESS_THAN_EQUAL((byte) -1), EQUAL((byte) 0), GREATER_THAN_EQUAL((byte) 1); private final byte equality; ComponentEquality(byte equality) { this.equality = equality; } public byte toByte() { return equality; } public static ComponentEquality fromByte(byte equality) { if (equality > 0) { return GREATER_THAN_EQUAL; } if (equality < 0) { return LESS_THAN_EQUAL; } return EQUAL; } } public static final BiMap<Class<? extends Serializer>, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder<Class<? extends Serializer>, String>() .put(AsciiSerializer.class, AsciiSerializer.get().getComparatorType().getTypeName()) .put( BigIntegerSerializer.class, BigIntegerSerializer.get().getComparatorType().getTypeName()) .put( ByteBufferSerializer.class, ByteBufferSerializer.get().getComparatorType().getTypeName()) .put(LongSerializer.class, LongSerializer.get().getComparatorType().getTypeName()) .put(StringSerializer.class, StringSerializer.get().getComparatorType().getTypeName()) .put(FloatSerializer.class, FloatSerializer.get().getComparatorType().getTypeName()) .put(UUIDSerializer.class, UUIDSerializer.get().getComparatorType().getTypeName()) .build(); static final ImmutableClassToInstanceMap<Serializer> SERIALIZERS = new ImmutableClassToInstanceMap.Builder<Serializer>() .put(AsciiSerializer.class, AsciiSerializer.get()) .put(BigIntegerSerializer.class, BigIntegerSerializer.get()) .put(ByteBufferSerializer.class, ByteBufferSerializer.get()) .put(FloatSerializer.class, FloatSerializer.get()) .put(LongSerializer.class, LongSerializer.get()) .put(StringSerializer.class, StringSerializer.get()) .put(UUIDSerializer.class, UUIDSerializer.get()) .build(); public static final BiMap<Byte, String> DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder<Byte, String>() .put((byte) 'a', ASCIITYPE.getTypeName()) .put((byte) 'b', BYTESTYPE.getTypeName()) .put((byte) 'f', FLOATTYPE.getTypeName()) .put((byte) 'i', INTEGERTYPE.getTypeName()) .put((byte) 'x', LEXICALUUIDTYPE.getTypeName()) .put((byte) 'l', LONGTYPE.getTypeName()) .put((byte) 't', TIMEUUIDTYPE.getTypeName()) .put((byte) 's', UTF8TYPE.getTypeName()) .put((byte) 'u', UUIDTYPE.getTypeName()) .build(); BiMap<Class<? extends Serializer>, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING; BiMap<Byte, String> aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING; final boolean dynamic; List<Serializer<?>> serializersByPosition = null; List<String> comparatorsByPosition = null; public class Component<T> { final Serializer<T> serializer; final T value; final ByteBuffer bytes; final String comparator; final ComponentEquality equality; public Component( T value, ByteBuffer bytes, Serializer<T> serializer, String comparator, ComponentEquality equality) { this.serializer = serializer; this.value = value; this.bytes = bytes; this.comparator = comparator; this.equality = equality; } public Serializer<T> getSerializer() { return serializer; } @SuppressWarnings("unchecked") public <A> A getValue(Serializer<A> s) { if (s == null) { s = (Serializer<A>) serializer; } if ((value == null) && (bytes != null) && (s != null)) { ByteBuffer cb = bytes.duplicate(); return s.fromByteBuffer(cb); } if (value instanceof ByteBuffer) { return (A) ((ByteBuffer) value).duplicate(); } return (A) value; } public T getValue() { return getValue(serializer); } @SuppressWarnings("unchecked") public <A> ByteBuffer getBytes(Serializer<A> s) { if (bytes == null) { if (value instanceof ByteBuffer) { return ((ByteBuffer) value).duplicate(); } if (value == null) { return null; } if (s == null) { s = (Serializer<A>) serializer; } if (s != null) { return s.toByteBuffer((A) value).duplicate(); } } return bytes.duplicate(); } public ByteBuffer getBytes() { return getBytes(serializer); } public String getComparator() { return comparator; } public ComponentEquality getEquality() { return equality; } @Override public String toString() { return "Component [" + getValue() + "]"; } } List<Component<?>> components = new ArrayList<Component<?>>(); ComponentEquality equality = ComponentEquality.EQUAL; ByteBuffer serialized = null; public AbstractComposite(boolean dynamic) { this.dynamic = dynamic; } public AbstractComposite(boolean dynamic, Object... o) { this.dynamic = dynamic; this.addAll(Arrays.asList(o)); } public AbstractComposite(boolean dynamic, List<?> l) { this.dynamic = dynamic; this.addAll(l); } public List<Component<?>> getComponents() { return components; } public void setComponents(List<Component<?>> components) { serialized = null; this.components = components; } public Map<Class<? extends Serializer>, String> getSerializerToComparatorMapping() { return serializerToComparatorMapping; } public void setSerializerToComparatorMapping( Map<Class<? extends Serializer>, String> serializerToComparatorMapping) { serialized = null; this.serializerToComparatorMapping = new ImmutableBiMap.Builder<Class<? extends Serializer>, String>() .putAll(serializerToComparatorMapping) .build(); } public Map<Byte, String> getAliasesToComparatorMapping() { return aliasToComparatorMapping; } public void setAliasesToComparatorMapping(Map<Byte, String> aliasesToComparatorMapping) { serialized = null; aliasToComparatorMapping = new ImmutableBiMap.Builder<Byte, String>().putAll(aliasesToComparatorMapping).build(); } public boolean isDynamic() { return dynamic; } public List<Serializer<?>> getSerializersByPosition() { return serializersByPosition; } public void setSerializersByPosition(List<Serializer<?>> serializersByPosition) { this.serializersByPosition = serializersByPosition; } public void setSerializersByPosition(Serializer<?>... serializers) { serializersByPosition = Arrays.asList(serializers); } public void setSerializerByPosition(int index, Serializer<?> s) { if (serializersByPosition == null) { serializersByPosition = new ArrayList<Serializer<?>>(); } while (serializersByPosition.size() <= index) { serializersByPosition.add(null); } serializersByPosition.set(index, s); } public List<String> getComparatorsByPosition() { return comparatorsByPosition; } public void setComparatorsByPosition(List<String> comparatorsByPosition) { this.comparatorsByPosition = comparatorsByPosition; } public void setComparatorsByPosition(String... comparators) { comparatorsByPosition = Arrays.asList(comparators); } public void setComparatorByPosition(int index, String c) { if (comparatorsByPosition == null) { comparatorsByPosition = new ArrayList<String>(); } while (comparatorsByPosition.size() <= index) { comparatorsByPosition.add(null); } comparatorsByPosition.set(index, c); } @Override public int compareTo(AbstractComposite o) { return serialize().compareTo(o.serialize()); } private String comparatorForSerializer(Serializer<?> s) { String comparator = serializerToComparatorMapping.get(s.getClass()); if (comparator != null) { return comparator; } return BYTESTYPE.getTypeName(); } private Serializer<?> serializerForComparator(String c) { int p = c.indexOf('('); if (p >= 0) { c = c.substring(0, p); } if (LEXICALUUIDTYPE.getTypeName().equals(c) || TIMEUUIDTYPE.getTypeName().equals(c)) { return UUIDSerializer.get(); } Serializer<?> s = SERIALIZERS.getInstance(serializerToComparatorMapping.inverse().get(c)); if (s != null) { return s; } return ByteBufferSerializer.get(); } private Serializer<?> serializerForPosition(int i) { if (serializersByPosition == null) { return null; } if (i >= serializersByPosition.size()) { return null; } return serializersByPosition.get(i); } private Serializer<?> getSerializer(int i, String c) { Serializer<?> s = serializerForPosition(i); if (s != null) { return s; } return serializerForComparator(c); } private String comparatorForPosition(int i) { if (comparatorsByPosition == null) { return null; } if (i >= comparatorsByPosition.size()) { return null; } return comparatorsByPosition.get(i); } private String getComparator(int i, ByteBuffer bb) { String name = comparatorForPosition(i); if (name != null) { return name; } if (!dynamic) { if (bb.hasRemaining()) { return BYTESTYPE.getTypeName(); } else { return null; } } if (bb.hasRemaining()) { try { int header = getShortLength(bb); if ((header & 0x8000) == 0) { name = Charsets.UTF_8.newDecoder().decode(getBytes(bb, header).duplicate()).toString(); } else { byte a = (byte) (header & 0xFF); name = aliasToComparatorMapping.get(a); if (name == null) { a = (byte) Character.toLowerCase((char) a); name = aliasToComparatorMapping.get(a); if (name != null) { name += "(reversed=true)"; } } } } catch (CharacterCodingException e) { throw new RuntimeException(e); } } if ((name != null) && (name.length() == 0)) { name = null; } return name; } @Override public void clear() { serialized = null; components = new ArrayList<Component<?>>(); } @Override public int size() { return components.size(); } @SuppressWarnings("unchecked") public <T> AbstractComposite addComponent(int index, T element, ComponentEquality equality) { serialized = null; if (element instanceof Component) { components.add(index, (Component<?>) element); return this; } Serializer s = serializerForPosition(index); if (s == null) { s = SerializerTypeInferer.getSerializer(element); } String c = comparatorForPosition(index); if (c == null) { c = comparatorForSerializer(s); } components.add(index, new Component(element, null, s, c, equality)); setSerializerByPosition(index, s); return this; } public <T> AbstractComposite addComponent(T value, Serializer<T> s) { addComponent(value, s, comparatorForSerializer(s)); return this; } public <T> AbstractComposite addComponent(T value, Serializer<T> s, String comparator) { addComponent(value, s, comparator, ComponentEquality.EQUAL); return this; } public <T> AbstractComposite addComponent( T value, Serializer<T> s, String comparator, ComponentEquality equality) { addComponent(-1, value, s, comparator, equality); return this; } @SuppressWarnings("unchecked") public <T> AbstractComposite addComponent( int index, T value, Serializer<T> s, String comparator, ComponentEquality equality) { serialized = null; if (value == null) { throw new NullPointerException("Unable able to add null component"); } if (index < 0) { index = components.size(); } while (components.size() < index) { components.add(null); } components.add(index, new Component(value, null, s, comparator, equality)); return this; } private static Object mapIfNumber(Object o) { if ((o instanceof Byte) || (o instanceof Integer) || (o instanceof Short)) { return BigInteger.valueOf(((Number) o).longValue()); } return o; } @SuppressWarnings({"unchecked"}) private static Collection<?> flatten(Collection<?> c) { if (c instanceof AbstractComposite) { return ((AbstractComposite) c).getComponents(); } boolean hasCollection = false; for (Object o : c) { if (o instanceof Collection) { hasCollection = true; break; } } if (!hasCollection) { return c; } List newList = new ArrayList(); for (Object o : c) { if (o instanceof Collection) { newList.addAll(flatten((Collection) o)); } else { newList.add(o); } } return newList; } /** * For education purposes, when addAll is called, it ultimately ends up calling add(int,Object). * The call stack using JDK7 is the following. 1. addAll 2. * AbstractCollection<E>.addAll(Collection<Object>) 3. AbstractList<E>.add(E) 4. * AbstractComposite.add(int,Object) * * <p>What happens is AbstractList<E>.add(E) does this add(size(),E). Neither * java.util.AbstractList or java.util.AbstractCollection provide storage in the base class. It * expects the subclass to provide the storage. */ @Override public boolean addAll(Collection<? extends Object> c) { return super.addAll(flatten(c)); } @Override public boolean containsAll(Collection<?> c) { return super.containsAll(flatten(c)); } @Override public boolean removeAll(Collection<?> c) { return super.removeAll(flatten(c)); } @Override public boolean retainAll(Collection<?> c) { return super.retainAll(flatten(c)); } /** * Method is similar to addAll(Collection<? extends Object>) in that it ends up calling * AbstractComposite.add(int,Object). */ @Override public boolean addAll(int i, Collection<? extends Object> c) { return super.addAll(i, flatten(c)); } /** * add(int,Object) is the primary method that adds elements to the list. All other add methods end * up here. */ @Override public void add(int index, Object element) { element = mapIfNumber(element); addComponent(index, element, ComponentEquality.EQUAL); } /** * remove(int) is the primary method that removes elements from the list. All other remove methods * end up calling AbstractComposite.remove(int). */ @Override public Object remove(int index) { serialized = null; Component prev = components.remove(index); if (prev != null) { return prev.getValue(); } return null; } public <T> AbstractComposite setComponent(int index, T value, Serializer<T> s) { setComponent(index, value, s, comparatorForSerializer(s)); return this; } public <T> AbstractComposite setComponent( int index, T value, Serializer<T> s, String comparator) { setComponent(index, value, s, comparator, ComponentEquality.EQUAL); return this; } @SuppressWarnings("unchecked") public <T> AbstractComposite setComponent( int index, T value, Serializer<T> s, String comparator, ComponentEquality equality) { serialized = null; while (components.size() <= index) { components.add(null); } components.set(index, new Component(value, null, s, comparator, equality)); return this; } @SuppressWarnings("unchecked") @Override public Object set(int index, Object element) { serialized = null; if (element instanceof Component) { Component prev = components.set(index, (Component<?>) element); if (prev != null) { return prev.getValue(); } return null; } element = mapIfNumber(element); Serializer s = serializerForPosition(index); if (s == null) { s = SerializerTypeInferer.getSerializer(element); } String c = comparatorForPosition(index); if (c == null) { c = comparatorForSerializer(s); } Component prev = components.set(index, new Component(element, null, s, c, ComponentEquality.EQUAL)); if (prev != null) { return prev.getValue(); } return null; } @Override public Object get(int i) { Component c = components.get(i); if (c != null) { return c.getValue(); } return null; } public <T> T get(int i, Serializer<T> s) throws ClassCastException { T value = null; Component<?> c = components.get(i); if (c != null) { value = c.getValue(s); } return value; } public Component getComponent(int i) { if (i >= components.size()) { return null; } Component c = components.get(i); return c; } public Iterator<Component<?>> componentsIterator() { return components.iterator(); } @SuppressWarnings("unchecked") public ByteBuffer serialize() { if (serialized != null) { return serialized.duplicate(); } ByteBufferOutputStream out = new ByteBufferOutputStream(); int i = 0; for (Component c : components) { Serializer<?> s = serializerForPosition(i); ByteBuffer cb = c.getBytes(s); if (cb == null) { cb = ByteBuffer.allocate(0); } if (dynamic) { String comparator = comparatorForPosition(i); if (comparator == null) { comparator = c.getComparator(); } if (comparator == null) { comparator = BYTESTYPE.getTypeName(); } int p = comparator.indexOf("(reversed=true)"); boolean desc = false; if (p >= 0) { comparator = comparator.substring(0, p); desc = true; } if (aliasToComparatorMapping.inverse().containsKey(comparator)) { byte a = aliasToComparatorMapping.inverse().get(comparator); if (desc) { a = (byte) Character.toUpperCase((char) a); } out.writeShort((short) (0x8000 | a)); } else { out.writeShort((short) comparator.length()); out.write(comparator.getBytes(Charsets.UTF_8)); } // if (comparator.equals(BYTESTYPE.getTypeName()) && (cb.remaining() == // 0)) { // log.warn("Writing zero-length BytesType, probably an error"); // } } out.writeShort((short) cb.remaining()); out.write(cb.slice()); if ((i == (components.size() - 1)) && (equality != ComponentEquality.EQUAL)) { out.write(equality.toByte()); } else { out.write(c.getEquality().toByte()); } i++; } serialized = out.getByteBuffer(); return serialized.duplicate(); } @SuppressWarnings("unchecked") public void deserialize(ByteBuffer b) { serialized = b.duplicate(); components = new ArrayList<Component<?>>(); String comparator = null; int i = 0; while ((comparator = getComparator(i, b)) != null) { ByteBuffer data = getWithShortLength(b); if (data != null) { Serializer<?> s = getSerializer(i, comparator); ComponentEquality equality = ComponentEquality.fromByte(b.get()); components.add(new Component(null, data.slice(), s, comparator, equality)); } else { throw new RuntimeException("Missing component data in composite type"); } i++; } } protected static int getShortLength(ByteBuffer bb) { int length = (bb.get() & 0xFF) << 8; return length | (bb.get() & 0xFF); } protected static ByteBuffer getBytes(ByteBuffer bb, int length) { ByteBuffer copy = bb.duplicate(); copy.limit(copy.position() + length); bb.position(bb.position() + length); return copy; } protected static ByteBuffer getWithShortLength(ByteBuffer bb) { int length = getShortLength(bb); return getBytes(bb, length); } public ComponentEquality getEquality() { return equality; } public void setEquality(ComponentEquality equality) { serialized = null; this.equality = equality; } }
/** * Class <code>CassandraDataAccessHelper</code> Encapsulate the Cassandra DataAccessLogic used in * CassandraMessageStore */ public class CassandraDataAccessHelper { private static final String USERNAME_KEY = "username"; private static final String PASSWORD_KEY = "password"; /** Serializes used for Cassandra data operations */ private static StringSerializer stringSerializer = StringSerializer.get(); private static LongSerializer longSerializer = LongSerializer.get(); private static BytesArraySerializer bytesArraySerializer = BytesArraySerializer.get(); private static IntegerSerializer integerSerializer = IntegerSerializer.get(); private static ByteBufferSerializer byteBufferSerializer = ByteBufferSerializer.get(); /** * Create a Cassandra Cluster instance given the connection details * * @param userName userName to connect to the cassandra * @param password password to connect to cassandra * @param clusterName Cluster name * @param connectionString cassandra connection string * @return Cluster instance * @throws CassandraDataAccessException In case of and error in accessing database or data error */ public static Cluster createCluster( String userName, String password, String clusterName, String connectionString) throws CassandraDataAccessException { if (userName == null || password == null) { throw new CassandraDataAccessException( "Can't create cluster with empty userName or Password"); } if (clusterName == null) { throw new CassandraDataAccessException("Can't create cluster with empty cluster name"); } if (connectionString == null) { throw new CassandraDataAccessException("Can't create cluster with empty connection string"); } Map<String, String> credentials = new HashMap<String, String>(); credentials.put(USERNAME_KEY, userName); credentials.put(PASSWORD_KEY, password); CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(connectionString); hostConfigurator.setMaxActive(2000); Cluster cluster = HFactory.getCluster(clusterName); if (cluster == null) { cluster = HFactory.createCluster(clusterName, hostConfigurator, credentials); } return cluster; } /** * Create a Column family in a Given Cluster instance * * @param name ColumnFamily Name * @param keySpace KeySpace name * @param cluster Cluster instance * @param comparatorType Comparator * @throws CassandraDataAccessException In case of an Error accessing database or data error */ public static void createColumnFamily( String name, String keySpace, Cluster cluster, String comparatorType) throws CassandraDataAccessException { KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace); if (ksDef == null) { throw new CassandraDataAccessException( "Can't create Column family, keyspace " + keySpace + " does not exist"); } ColumnFamilyDefinition cfDef = new ThriftCfDef(keySpace, /*"Queue"*/ name, ComparatorType.getByClassName(comparatorType)); List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs(); HashSet<String> cfNames = new HashSet<String>(); for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) { cfNames.add(columnFamilyDefinition.getName()); } if (!cfNames.contains(name)) { cluster.addColumnFamily(cfDef, true); } } /** * Create a Column family for cassandra counters in a given Cluster intance * * @param name ColumnFamily Name * @param keySpace KeySpace name * @param cluster Cluster instance * @throws CassandraDataAccessException In case of an Error accessing database or data error */ public static void createCounterColumnFamily(String name, String keySpace, Cluster cluster) throws CassandraDataAccessException { KeyspaceDefinition ksDef = cluster.describeKeyspace(keySpace); if (ksDef == null) { throw new CassandraDataAccessException( "Can't create Column family, keyspace " + keySpace + " does not exist"); } ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keySpace, name, ComparatorType.COUNTERTYPE); cfDef.setComparatorType(ComparatorType.UTF8TYPE); cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName()); cfDef.setColumnType(ColumnType.STANDARD); List<ColumnFamilyDefinition> cfDefsList = ksDef.getCfDefs(); HashSet<String> cfNames = new HashSet<String>(); for (ColumnFamilyDefinition columnFamilyDefinition : cfDefsList) { cfNames.add(columnFamilyDefinition.getName()); } if (!cfNames.contains(name)) { cluster.addColumnFamily(cfDef, true); } } /** * Insert a raw-column for counting a property (row is property, column is item) * * @param cfName column family name * @param counterRowName name of row * @param queueColumn name of column * @param keyspace key space name * @throws CassandraDataAccessException */ public static void insertCounterColumn( String cfName, String counterRowName, String queueColumn, Keyspace keyspace) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); // inserting counter column mutator.insertCounter( counterRowName, cfName, HFactory.createCounterColumn(queueColumn, 0L, StringSerializer.get())); mutator.execute(); CounterQuery<String, String> counter = new ThriftCounterColumnQuery<String, String>( keyspace, stringSerializer, stringSerializer); counter.setColumnFamily(cfName).setKey(counterRowName).setName(queueColumn); } catch (Exception e) { throw new CassandraDataAccessException("Error while inserting data to:" + cfName, e); } } /** * remove allocated raw-column space for counter * * @param cfName column family name * @param counterRowName name of row * @param queueColumn name of column * @param keyspace key space name * @throws CassandraDataAccessException */ public static void removeCounterColumn( String cfName, String counterRowName, String queueColumn, Keyspace keyspace) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.deleteCounter(counterRowName, cfName, queueColumn, stringSerializer); mutator.execute(); CounterQuery<String, String> counter = new ThriftCounterColumnQuery<String, String>( keyspace, stringSerializer, stringSerializer); counter.setColumnFamily(cfName).setKey(counterRowName).setName(queueColumn); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + cfName, e); } } /** * Increment counter by given value * * @param rawID raw name * @param columnFamily column family name * @param columnName name of column * @param keyspace keyspace * @param incrementBy value to increase by * @throws CassandraDataAccessException */ public static void incrementCounter( String columnName, String columnFamily, String rawID, Keyspace keyspace, long incrementBy) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.incrementCounter(rawID, columnFamily, columnName, incrementBy); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e); } } /** * Decrement counter by 1 * * @param rawID raw name * @param columnFamily column family name * @param columnName name of column * @param keyspace keyspace * @throws CassandraDataAccessException */ public static void decrementCounter( String columnName, String columnFamily, String rawID, Keyspace keyspace, long decrementBy) throws CassandraDataAccessException { try { Mutator<String> mutator = HFactory.createMutator(keyspace, StringSerializer.get()); mutator.decrementCounter(rawID, columnFamily, columnName, decrementBy); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e); } } /** * @param keyspace name of key space * @param columnFamily column family name * @param key key value * @param cloumnName column name * @return long count value * @throws CassandraDataAccessException */ public static long getCountValue( Keyspace keyspace, String columnFamily, String cloumnName, String key) throws CassandraDataAccessException { try { CounterQuery<String, String> query = HFactory.createCounterColumnQuery(keyspace, stringSerializer, stringSerializer); query.setColumnFamily(columnFamily).setKey(key).setName(cloumnName); HCounterColumn<String> counter = query.execute().get(); return counter.getValue(); } catch (Exception e) { throw new CassandraDataAccessException("Error while accessing:" + columnFamily, e); } } public static Keyspace createKeySpace(Cluster cluster, String keySpace) { Keyspace keyspace; // Define the keySpace KeyspaceDefinition definition = new ThriftKsDef(keySpace); KeyspaceDefinition def = cluster.describeKeyspace(keySpace); if (def == null) { // Adding keySpace to the cluster cluster.addKeyspace(definition, true); } keyspace = HFactory.createKeyspace(keySpace, cluster); CassandraConsistencyLevelPolicy policy = new CassandraConsistencyLevelPolicy(); keyspace.setConsistencyLevelPolicy(policy); return keyspace; } /** * Get List of Strings in a Given ROW in a Cassandra Column Family Here we assume that the columns * in a given row have string data and key and value in the given column in that row have same * values. * * @param columnFamilyName Name of the column Family * @param rowName Row name * @param keyspace keySpace * @return List of string in that given row. * @throws CassandraDataAccessException In case of database access error or data error */ public static List<String> getRowList(String columnFamilyName, String rowName, Keyspace keyspace) throws CassandraDataAccessException { ArrayList<String> rowList = new ArrayList<String>(); if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName); } try { SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, 10000); QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute(); ColumnSlice<String, String> columnSlice = result.get(); for (HColumn<String, String> column : columnSlice.getColumns()) { rowList.add(column.getName()); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while accessing data from :" + columnFamilyName, e); } return rowList; } public static List<String> getDestinationQueueNamesFromCounterColumns( String columnFamilyName, String rowName, Keyspace keyspace) throws CassandraDataAccessException { ArrayList<String> rowList = new ArrayList<String>(); if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName); } try { SliceCounterQuery<String, String> sliceQuery = HFactory.createCounterSliceQuery(keyspace, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, Integer.MAX_VALUE); QueryResult<CounterSlice<String>> result = sliceQuery.execute(); CounterSlice<String> columnSlice = result.get(); for (HCounterColumn<String> column : columnSlice.getColumns()) { rowList.add(column.getName()); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while accessing data from :" + columnFamilyName, e); } return rowList; } /** * Get the value of a given column in a Given ROW in a Cassandra Column Family Here we assume that * the columns in a given row have string data and key and value in the given column in that row * have same values. * * @param columnFamilyName Name of the column Family * @param rowName Row name * @param keyspace keySpace * @param columnName Name of the column * @return vlaue of a given column in that given row. * @throws CassandraDataAccessException In case of database access error or data error */ public static String getColumnValue( String columnFamilyName, String rowName, Keyspace keyspace, String columnName) throws CassandraDataAccessException { ArrayList<String> rowList = new ArrayList<String>(); String value = null; if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily =" + columnFamilyName + " and rowName=" + rowName); } try { SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, 10000); QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute(); ColumnSlice<String, String> columnSlice = result.get(); for (HColumn<String, String> column : columnSlice.getColumns()) { if (column.getName().equalsIgnoreCase(columnName)) { value = column.getValue(); break; } } } catch (Exception e) { throw new CassandraDataAccessException( "Error while accessing data from :" + columnFamilyName, e); } return value; } /** * Get set of messages in a column family * * @param queueName QueueName * @param columnFamilyName ColumnFamilyName * @param keyspace Cassandra KeySpace * @param lastProcessedId Last processed Message id to use as a off set * @param count max message count limit * @return ColumnSlice which contain the messages * @throws CassandraDataAccessException */ public static ColumnSlice<Long, byte[]> getMessagesFromQueue( String queueName, String columnFamilyName, Keyspace keyspace, long lastProcessedId, int count) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || queueName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamilyName + " and queueName=" + queueName); } try { SliceQuery<String, Long, byte[]> sliceQuery = HFactory.createSliceQuery( keyspace, stringSerializer, longSerializer, bytesArraySerializer); sliceQuery.setKey(queueName); sliceQuery.setRange(lastProcessedId + 1, Long.MAX_VALUE, false, count); sliceQuery.setColumnFamily(columnFamilyName); QueryResult<ColumnSlice<Long, byte[]>> result = sliceQuery.execute(); ColumnSlice<Long, byte[]> columnSlice = result.get(); return columnSlice; } catch (Exception e) { throw new CassandraDataAccessException( "Error while getting data from " + columnFamilyName, e); } } /** * Get set of messages in a column family * * @param queueName QueueName * @param columnFamilyName ColumnFamilyName * @param keyspace Cassandra KeySpace * @param count max message count limit * @return ColumnSlice which contain the messages * @throws CassandraDataAccessException */ public static ColumnSlice<Long, byte[]> getMessagesFromQueue( String queueName, String columnFamilyName, Keyspace keyspace, int count) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || queueName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamilyName + " and queueName=" + queueName); } try { SliceQuery<String, Long, byte[]> sliceQuery = HFactory.createSliceQuery( keyspace, stringSerializer, longSerializer, bytesArraySerializer); sliceQuery.setKey(queueName); sliceQuery.setRange((long) 0, Long.MAX_VALUE, false, count); sliceQuery.setColumnFamily(columnFamilyName); QueryResult<ColumnSlice<Long, byte[]>> result = sliceQuery.execute(); ColumnSlice<Long, byte[]> columnSlice = result.get(); return columnSlice; } catch (Exception e) { throw new CassandraDataAccessException( "Error while getting data from " + columnFamilyName, e); } } /** * Get Number of <String,String> type columns in a given row in a cassandra column family * * @param rowName row Name we are querying for * @param columnFamilyName columnFamilName * @param keyspace * @param count * @return */ public static ColumnSlice<String, String> getStringTypeColumnsInARow( String rowName, String columnFamilyName, Keyspace keyspace, int count) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamilyName == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamilyName + " and rowName=" + rowName); } try { SliceQuery sliceQuery = HFactory.createSliceQuery(keyspace, stringSerializer, stringSerializer, stringSerializer); sliceQuery.setKey(rowName); sliceQuery.setColumnFamily(columnFamilyName); sliceQuery.setRange("", "", false, count); QueryResult<ColumnSlice<String, String>> result = sliceQuery.execute(); ColumnSlice<String, String> columnSlice = result.get(); return columnSlice; } catch (Exception e) { throw new CassandraDataAccessException( "Error while getting data from : " + columnFamilyName, e); } } /** * Get a HColumn<Long, byte[]> with a given key in a given row in a given column Family * * @param rowName name of the row * @param columnFamily column Family name * @param key long type key of the column we are looking for * @param keyspace cassandra keySpace instance * @return query result as a cassandra column * @throws CassandraDataAccessException in case of an Error when accessing data */ public static HColumn<Long, byte[]> getLongByteArrayColumnInARow( String rowName, String columnFamily, long key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't access Data , no keyspace provided "); } if (columnFamily == null || rowName == null) { throw new CassandraDataAccessException( "Can't access data with columnFamily = " + columnFamily + " and rowName=" + rowName); } try { ColumnQuery columnQuery = HFactory.createColumnQuery( keyspace, stringSerializer, longSerializer, bytesArraySerializer); columnQuery.setColumnFamily(columnFamily); columnQuery.setKey(rowName); columnQuery.setName(key); QueryResult<HColumn<Long, byte[]>> result = columnQuery.execute(); HColumn<Long, byte[]> column = result.get(); return column; } catch (Exception e) { throw new CassandraDataAccessException( "Error while executing quary for HColumn<Long, byte[]> with key =" + key + " in column Family = " + columnFamily, e); } } /** * Add Message to a Given Queue in Cassandra * * @param columnFamily ColumnFamily name * @param queue queue name * @param messageId Message id * @param message message in bytes * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error */ public static void addMessageToQueue( String columnFamily, String queue, String messageId, byte[] message, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } if (columnFamily == null || queue == null || messageId == null || message == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and queue=" + queue + " message id = " + messageId + " message = " + message); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addInsertion( queue.trim(), columnFamily, HFactory.createColumn(messageId, message, stringSerializer, bytesArraySerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding message to Queue", e); } } /** * Add Message to a Given Queue in Cassandra * * @param columnFamily ColumnFamily name * @param queue queue name * @param messageId Message id * @param message message in bytes * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error */ public static void addMessageToQueue( String columnFamily, String queue, long messageId, byte[] message, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } if (columnFamily == null || queue == null || message == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and queue=" + queue + " message id = " + messageId + " message = " + message); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addInsertion( queue.trim(), columnFamily, HFactory.createColumn(messageId, message, longSerializer, bytesArraySerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding message to Queue", e); } } /** * Add Message to a Given Queue in Cassandra * * @param columnFamily ColumnFamily name * @param queue queue name * @param messageId Message id * @param message message in bytes * @param mutator Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error */ public static void addMessageToQueue( String columnFamily, String queue, long messageId, byte[] message, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } if (columnFamily == null || queue == null || message == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and queue=" + queue + " message id = " + messageId + " message = " + message); } try { mutator.addInsertion( queue.trim(), columnFamily, HFactory.createColumn(messageId, message, longSerializer, bytesArraySerializer)); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException("Error while adding message to Queue", e); } } /** * Add a new Column <int,byte[]> to a given row in a given column family * * @param columnFamily column Family name * @param row row name * @param key key of the column * @param value value of the column * @param keyspace cassandra KeySpace * @throws CassandraDataAccessException */ public static void addIntegerByteArrayContentToRaw( String columnFamily, String row, int key, byte[] value, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no keySpace provided "); } if (columnFamily == null || row == null || value == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and row=" + row + " key = " + key + " value = " + value); } try { Mutator<String> messageContentMutator = HFactory.createMutator(keyspace, stringSerializer); messageContentMutator.addInsertion( row, columnFamily, HFactory.createColumn(key, value, integerSerializer, bytesArraySerializer)); messageContentMutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException( "Error while adding new Column <int,byte[]> to cassandra store", e); } } public static void addIntegerByteArrayContentToRaw( String columnFamily, String row, int key, byte[] value, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't add Data , no Mutator provided "); } if (columnFamily == null || row == null || value == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and row=" + row + " key = " + key + " value = " + value); } mutator.addInsertion( row, columnFamily, HFactory.createColumn(key, value, integerSerializer, bytesArraySerializer)); if (execute) { mutator.execute(); } } /** * Add new Column<long,long> to a given row in a given cassandra column family * * @param columnFamily column family name * @param row row name * @param key long key value of the column * @param value long value of the column * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException */ public static void addLongContentToRow( String columnFamily, String row, long key, long value, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no keySpace provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.insert( row, columnFamily, HFactory.createColumn(key, value, longSerializer, longSerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding long content to a row", e); } } /** * Add a new Column <long,byte[]> to a given row in a given column family * * @param columnFamily column family name * @param row row name * @param key long key value * @param value value as a byte array * @param keyspace CassandraKeySpace * @throws CassandraDataAccessException */ public static void addLongByteArrayColumnToRow( String columnFamily, String row, long key, byte[] value, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no keySpace provided "); } if (columnFamily == null || row == null || value == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and row=" + row + " key = " + key + " value = " + value); } try { Mutator<String> messageContentMutator = HFactory.createMutator(keyspace, stringSerializer); messageContentMutator.addInsertion( row, columnFamily, HFactory.createColumn(key, value, longSerializer, bytesArraySerializer)); messageContentMutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException( "Error while adding new Column <int,byte[]> to cassandra store", e); } } /** * Add a Mapping to a Given Row in cassandra column family. Mappings are used as search indexes * * @param columnFamily columnFamilyName * @param row row name * @param cKey key name for the adding column * @param cValue value for the adding column * @param keyspace Cassandra KeySpace * @throws CassandraDataAccessException In case of database access error or data error */ public static void addMappingToRaw( String columnFamily, String row, String cKey, String cValue, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't add Data , no KeySpace provided "); } if (columnFamily == null || row == null || cKey == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and rowName=" + row + " key = " + cKey); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addInsertion( row, columnFamily, HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer)); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while adding a Mapping to row ", e); } } public static void addMappingToRow( String columnFamily, String row, String cKey, String cValue, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } mutator.addInsertion( row, columnFamily, HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer)); if (execute) { mutator.execute(); } } /** * Add an Mapping Entry to a raw in a given column family * * @param columnFamily ColumnFamily name * @param row row name * @param cKey column key * @param cValue column value * @param mutator mutator * @param execute should we execute the insertion. if false it will just and the insertion to the * mutator * @throws CassandraDataAccessException In case of database access error or data error */ public static void addMappingToRaw( String columnFamily, String row, String cKey, String cValue, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't add Data , no mutator provided "); } if (columnFamily == null || row == null || cKey == null) { throw new CassandraDataAccessException( "Can't add data with columnFamily = " + columnFamily + " and rowName=" + row + " key = " + cKey); } try { mutator.addInsertion( row, columnFamily, HFactory.createColumn(cKey, cValue.trim(), stringSerializer, stringSerializer)); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException("Error while adding a Mapping to row ", e); } } /** * Delete a given string column in a raw in a column family * * @param columnFamily column family name * @param row row name * @param key key name * @param keyspace cassandra keySpace * @throws CassandraDataAccessException In case of database access error or data error */ public static void deleteStringColumnFromRaw( String columnFamily, String row, String key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't delete Data , no keyspace provided "); } if (columnFamily == null || row == null || key == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addDeletion(row, columnFamily, key, stringSerializer); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } } /** * Delete a given string column in a raw in a column family * * @param columnFamily column family name * @param row row name * @param key key name * @param keyspace cassandra keySpace * @throws CassandraDataAccessException In case of database access error or data error */ public static void deleteLongColumnFromRaw( String columnFamily, String row, long key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't delete Data , no keyspace provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addDeletion(row, columnFamily, key, longSerializer); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } } public static void deleteLongColumnFromRaw( String columnFamily, String row, long key, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't delete Data , no mutator provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { mutator.addDeletion(row, columnFamily, key, longSerializer); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } } /** * Delete a given string column in a raw in a column family * * @param columnFamily ColumnFamily Name * @param row row name * @param key string key to of the column * @param mutator Mutator reference * @param execute execute the deletion ? * @throws CassandraDataAccessException */ public static void deleteStringColumnFromRaw( String columnFamily, String row, String key, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't delete Data , no mutator provided "); } if (columnFamily == null || row == null || key == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { mutator.addDeletion(row, columnFamily, key, stringSerializer); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } } public static void deleteIntegerColumnFromRow( String columnFamily, String row, int key, Mutator<String> mutator, boolean execute) throws CassandraDataAccessException { if (mutator == null) { throw new CassandraDataAccessException("Can't delete Data , no mutator provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { mutator.addDeletion(row, columnFamily, key, integerSerializer); if (execute) { mutator.execute(); } } catch (Exception e) { throw new CassandraDataAccessException( "Error while deleting " + key + " from " + columnFamily); } } public static void deleteIntegerColumnFromRow( String columnFamily, String row, Integer key, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't delete Data , no keyspace provided "); } if (columnFamily == null || row == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + row + " key = " + key); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); mutator.addDeletion(row, columnFamily, key, integerSerializer); mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while deleting data", e); } } public static void deleteIntegerColumnsFromRow( String columnFamily, List<String> rows, Keyspace keyspace) throws CassandraDataAccessException { if (keyspace == null) { throw new CassandraDataAccessException("Can't delete Data , no keyspace provided "); } if (columnFamily == null || rows == null) { throw new CassandraDataAccessException( "Can't delete data in columnFamily = " + columnFamily + " and rowName=" + rows); } try { Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); for (String row : rows) { mutator.addDeletion(row, columnFamily, null, integerSerializer); } mutator.execute(); } catch (Exception e) { throw new CassandraDataAccessException("Error while deleting data", e); } } }
public void setByteBuffer(N subColumnName, ByteBuffer value) { subColumns.add( columnFactory.createColumn( subColumnName, value, clock, template.getSubSerializer(), ByteBufferSerializer.get())); }