Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2013 eBuddy B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ebuddy.cassandra;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.Deletion;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.SuperColumn;
import com.ebuddy.cassandra.property.PropertyValue;
import com.ebuddy.cassandra.property.PropertyValueFactory;
import me.prettyprint.cassandra.model.ConfigurableConsistencyLevel;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.BatchMutation;
import me.prettyprint.cassandra.service.KeyspaceService;
import me.prettyprint.cassandra.service.OperationType;
import me.prettyprint.cassandra.utils.StringUtils;
import me.prettyprint.hector.api.ConsistencyLevelPolicy;
import me.prettyprint.hector.api.HConsistencyLevel;
import me.prettyprint.hector.api.exceptions.HNotFoundException;
/**
* Utility methods for accessing Cassandra.
*
* @author Eric Zoerner [email protected]
* @deprecated use DAO objects and ExtendedNetworkId instead
*/
@Deprecated
public final class HectorUtils {
//private static final Logger LOG = Logger.getLogger(HectorUtils.class);
private static final ByteBuffer EMPTY_BYTES = ByteBuffer.wrap(new byte[0]);
/**
* ALL consistency level policy
*/
public static final ConsistencyLevelPolicy ALL_CONSISTENCY = new ConfigurableConsistencyLevel() {
@Override
public HConsistencyLevel get(OperationType op) {
return HConsistencyLevel.ALL;
}
@Override
public HConsistencyLevel get(OperationType op, String cfName) {
return HConsistencyLevel.ALL;
}
};
/**
* A pre-fabricated SlicePredicate that gets all keys (up to SETTINGS_LIMIT).
*/
public static final SlicePredicate ALL_COLUMNS;
static {
SliceRange sliceRange = new SliceRange(EMPTY_BYTES,
EMPTY_BYTES,
false,
Integer.MAX_VALUE);
ALL_COLUMNS = new SlicePredicate();
ALL_COLUMNS.setSlice_range(sliceRange);
}
private HectorUtils() {
}
/**
* Simple utility to find a SuperColumn by name from a list of SuperColumns.
*
* @param superColumns the list of SuperColumns
* @param superColumnName the ByteBuffer SuperColumn name to look for
* @return the found SuperColumn or null if not found
*/
public static SuperColumn findSuperColumn(List superColumns, ByteBuffer superColumnName) {
for (SuperColumn superColumn : superColumns) {
if (superColumn.bufferForName().equals(superColumnName)) {
return superColumn;
}
}
return null;
}
/**
* Write a batchMutation to Cassandra.
*
* @param batchMutation the batch mutation
* @param keyspaceName the name of the keyspace
* @param template the cassandra template
*/
public static void mutate(final BatchMutation> batchMutation,
final String keyspaceName,
CassandraTemplate template) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
keyspace.batchMutate(batchMutation);
/*
if (LOG.isDebugEnabled()) {
LOG.debug("sending batchMutate on keyspace '" + keyspaceName +
"'; batchMutation isEmpty=" + batchMutation.isEmpty());
}
*/
return null;
}
});
}
/**
* Remove a row in a column family (or super column family).
*
* @param keyspaceName the name of the keyspace
* @param columnFamilyName the name of the column family
* @param rowKey the row key
* @param template the cassandra template
*/
public static void removeRow(String keyspaceName,
final String columnFamilyName,
final String rowKey,
CassandraTemplate template) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
ColumnPath columnPath = createColumnPath(columnFamilyName, (String) null, null);
keyspace.remove(rowKey, columnPath);
return null;
}
});
}
/**
* Remove a column in a column family (or super column family).
*
* @param keyspaceName the name of the keyspace
* @param columnFamilyName the name of the column family
* @param rowKey the row key
* @param superColumnName the super column name, or null
* @param columnName the columnName, or null if deleting all columns
* @param template the cassandra template
*/
@SuppressWarnings({"UnusedDeclaration"})//NOSONAR
public static void removeColumn(String keyspaceName,
final String columnFamilyName,
final String rowKey,
final String superColumnName,
final String columnName,
CassandraTemplate template) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
ColumnPath columnPath = createColumnPath(columnFamilyName, superColumnName, columnName);
keyspace.remove(rowKey, columnPath);
return null;
}
});
}
/**
* Remove listed columns in a column family (or super column family).
*
* @param keyspaceName the name of the keyspace
* @param columnFamilyName the name of the column family
* @param rowKey the row key
* @param superColumnName the super column name, or null
* @param columnNames the column names, or null if deleting all columns
* @param template the cassandra template
*/
public static void removeColumns(String keyspaceName,
final String columnFamilyName,
final String rowKey,
final String superColumnName,
final List columnNames,
CassandraTemplate template) {
if (columnNames == null ||
columnNames.size() == 1) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
String colName = null;
if (columnNames != null) {
colName = columnNames.get(0);
}
ColumnPath columnPath = createColumnPath(columnFamilyName, superColumnName, colName);
keyspace.remove(rowKey, columnPath);
return null;
}
});
} else {
// use a BatchMutation to do multiple column deletions in one go
BatchMutation batchMutation = new BatchMutation(StringSerializer.get());
Deletion deletion = new Deletion();
deletion.setTimestamp(template.createTimestamp());
if (superColumnName != null) {
deletion.setSuper_column(bytes(superColumnName));
}
SlicePredicate predicate = new SlicePredicate();
for (String columnName : columnNames) {
predicate.addToColumn_names(bytes(columnName));
}
deletion.setPredicate(predicate);
batchMutation.addDeletion(rowKey, Collections.singletonList(columnFamilyName), deletion);
mutate(batchMutation, keyspaceName, template);
}
}
/**
* Store key-value pairs to a column family.
*
* @param rowKey the row key
* @param columnFamilyName the column family name
* @param keyValuePairs map with the key value pairs
* @param template the cassandra template
* @param keyspaceName the keyspace name
*/
public static void storeKeyValuePairs(final String rowKey,
final String columnFamilyName,
final Map> keyValuePairs,
final CassandraTemplate template,
final String keyspaceName,
final String delimiter) {
storeKeyValuePairs(rowKey, columnFamilyName, null, keyValuePairs, template, keyspaceName, delimiter);
}
/**
* Store key-value pairs to a super column family.
*
* @param rowKey the row key
* @param columnFamilyName the column family name
* @param superColumnName the super column name
* @param keyValuePairs map with the key value pairs, keys can be hierarchical using a delimiter;
* @param template the cassandra template
* @param keyspaceName the keyspace name
* @param delimiter the delimiter to use for hierarchical properties
*/
public static void storeKeyValuePairs(final String rowKey,
final String columnFamilyName,
final String superColumnName,
final Map> keyValuePairs,
final CassandraTemplate template,
final String keyspaceName,
final String delimiter) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
NestedBatchMutation batchMutation = new NestedBatchMutation(template,
keyspaceName,
rowKey,
columnFamilyName,
superColumnName);
for (Map.Entry> keyValue : keyValuePairs.entrySet()) {
PropertyValue> propValue = keyValue.getValue();
if (propValue.isStructured()) {
throw new IllegalArgumentException("storage of structured property values not yet implemented");
}
batchMutation.addInsertion(keyValue.getKey(), (String) propValue.getValue(), delimiter);
}
keyspace.batchMutate(batchMutation.getMutationMap());
return null;
}
});
}
/**
* Store one key-value pairs to a super column family.
*
* @param rowKey the row key
* @param columnFamilyName the column family name
* @param superColumnName the super column name
* @param propertyName the property name
* @param value the string value of the property
* @param template the cassandra template
* @param keyspaceName the keyspace name
*/
@SuppressWarnings({"UnusedDeclaration"})
public static void storeOneValue(final String rowKey,
final String columnFamilyName,
@Nullable final String superColumnName,
final String propertyName,
final String value,
final CassandraTemplate template,
String keyspaceName) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
ColumnPath columnPath = createColumnPath(columnFamilyName, superColumnName, propertyName);
keyspace.insert(rowKey, columnPath, bytes(value));
return null;
}
});
}
/**
* Store one key-value pairs to a super column family.
*
* @param rowKey the row key
* @param columnFamilyName the column family name
* @param superColumnName the super column name
* @param propertyName the name of the property
* @param value the string value of the property
* @param template the cassandra template
* @param keyspaceName the keyspace name
* @param consistencyLevel the consistency level for this operation
*/
public static void storeOneValue(final String rowKey,
final String columnFamilyName,
final String superColumnName,
final String propertyName,
final String value,
final CassandraTemplate template,
String keyspaceName,
ConsistencyLevelPolicy consistencyLevel) {
template.execute(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
ColumnPath columnPath = createColumnPath(columnFamilyName, superColumnName, propertyName);
keyspace.insert(rowKey, columnPath, bytes(value));
return null;
}
}, consistencyLevel, false);
}
/**
* Store one key-value pairs to a super column family.
*
* @param rowKey the row key
* @param columnFamilyName the column family name
* @param superColumnName the super column name
* @param propertyName the property name
* @param value the string value of the property
* @param template the cassandra template
* @param keyspaceName the keyspace name
* @param ttl the ttlInMinutes
*/
public static void storeOneValue(final String rowKey,
final String columnFamilyName,
@Nullable final String superColumnName,
final String propertyName,
final String value,
final CassandraTemplate template,
String keyspaceName,
final int ttlInMinutes) {
template.write(keyspaceName, new KeyspaceCallback() {
public Void execute(KeyspaceService keyspace) {
int ttlInSeconds = (int) TimeUnit.SECONDS.convert(ttlInMinutes, TimeUnit.MINUTES);
ColumnPath columnPath = createColumnPath(columnFamilyName, superColumnName, propertyName);
ColumnParent columnParent = new ColumnParent(columnPath.getColumn_family());
if (columnPath.isSetSuper_column()) {
columnParent.setSuper_column(columnPath.getSuper_column());
}
Column column = new Column(ByteBuffer.wrap(columnPath.getColumn()));
column.setTtl(ttlInSeconds);
ByteBuffer key = StringSerializer.get().toByteBuffer(rowKey);
keyspace.insert(key, columnParent, column);
return null;
}
});
}
/**
* Get all the key value pairs from a column family for a given row.
*
* @param rowKey the row key
* @param columnFamilyName the name of the column family
* @param template the cassandra template
* @param keyspaceName the keyspace name
* @return a map of the key value pairs
*/
public static Map> getAllKeyValuePairs(final String rowKey,
final String columnFamilyName,
CassandraTemplate template,
String keyspaceName) {
return getAllKeyValuePairs(rowKey, columnFamilyName, null, template, keyspaceName);
}
/**
* Get all the key value pairs from a super column for a given row.
*
* @param rowKey the row key
* @param columnFamilyName the name of the column family
* @param superColumnName the name of the super column
* @param template the cassandra template
* @param keyspaceName the name of the keyspace
* @return a map containing the key value pairs
*/
public static Map> getAllKeyValuePairs(final String rowKey,
final String columnFamilyName,
final String superColumnName,
CassandraTemplate template,
String keyspaceName) {
return template.read(keyspaceName, new KeyspaceCallback