use of com.netflix.astyanax.MutationBatch in project druid by druid-io.
the class CassandraDataSegmentPusher method push.
@Override
public DataSegment push(final File indexFilesDir, DataSegment segment) throws IOException {
log.info("Writing [%s] to C*", indexFilesDir);
String key = JOINER.join(config.getKeyspace().isEmpty() ? null : config.getKeyspace(), DataSegmentPusherUtil.getStorageDir(segment));
// Create index
final File compressedIndexFile = File.createTempFile("druid", "index.zip");
long indexSize = CompressionUtils.zip(indexFilesDir, compressedIndexFile);
log.info("Wrote compressed file [%s] to [%s]", compressedIndexFile.getAbsolutePath(), key);
int version = SegmentUtils.getVersionFromDir(indexFilesDir);
try {
long start = System.currentTimeMillis();
ChunkedStorage.newWriter(indexStorage, key, new FileInputStream(compressedIndexFile)).withConcurrencyLevel(CONCURRENCY).call();
byte[] json = jsonMapper.writeValueAsBytes(segment);
MutationBatch mutation = this.keyspace.prepareMutationBatch();
mutation.withRow(descriptorStorage, key).putColumn("lastmodified", System.currentTimeMillis(), null).putColumn("descriptor", json, null);
mutation.execute();
log.info("Wrote index to C* in [%s] ms", System.currentTimeMillis() - start);
} catch (Exception e) {
throw new IOException(e);
}
segment = segment.withSize(indexSize).withLoadSpec(ImmutableMap.<String, Object>of("type", "c*", "key", key)).withBinaryVersion(version);
log.info("Deleting zipped index File[%s]", compressedIndexFile);
compressedIndexFile.delete();
return segment;
}
use of com.netflix.astyanax.MutationBatch in project zuul by Netflix.
the class HystrixCassandraPut method run.
@Override
protected Void run() throws Exception {
try {
MutationBatch m = keyspace.prepareMutationBatch().setConsistencyLevel(ConsistencyLevel.CL_QUORUM);
// Setting columns in a standard column
ColumnListMutation<String> cm = m.withRow(columnFamily, rowKey);
for (String key : attributes.keySet()) {
Object o = attributes.get(key);
if (o != null) {
// unfortunately the 'putColumn' method does not nicely figure out what type the Object is so we need to do it manually
if (o instanceof String) {
cm.putColumn(key, (String) o, ttlSeconds);
} else if (o instanceof Boolean) {
cm.putColumn(key, (Boolean) o, ttlSeconds);
} else if (o instanceof Integer) {
cm.putColumn(key, (Integer) o, ttlSeconds);
} else if (o instanceof Long) {
cm.putColumn(key, (Long) o, ttlSeconds);
} else if (o instanceof Double) {
cm.putColumn(key, (Double) o, ttlSeconds);
} else if (o instanceof Date) {
cm.putColumn(key, (Date) o, ttlSeconds);
} else if (o instanceof byte[]) {
cm.putColumn(key, (byte[]) o, ttlSeconds);
} else if (o instanceof ByteBuffer) {
cm.putColumn(key, (ByteBuffer) o, ttlSeconds);
} else {
throw new IllegalArgumentException("Unsupported object instance type: " + o.getClass().getSimpleName());
}
}
}
m.execute();
return null;
} catch (ConnectionException e) {
throw e;
}
}
use of com.netflix.astyanax.MutationBatch in project titan by thinkaurelius.
the class AstyanaxStoreManager method mutateMany.
@Override
public void mutateMany(Map<String, Map<StaticBuffer, KCVMutation>> batch, StoreTransaction txh) throws BackendException {
MutationBatch m = keyspaceContext.getClient().prepareMutationBatch().withAtomicBatch(atomicBatch).setConsistencyLevel(getTx(txh).getWriteConsistencyLevel().getAstyanax()).withRetryPolicy(retryPolicy.duplicate());
final MaskedTimestamp commitTime = new MaskedTimestamp(txh);
for (Map.Entry<String, Map<StaticBuffer, KCVMutation>> batchentry : batch.entrySet()) {
String storeName = batchentry.getKey();
Preconditions.checkArgument(openStores.containsKey(storeName), "Store cannot be found: " + storeName);
ColumnFamily<ByteBuffer, ByteBuffer> columnFamily = openStores.get(storeName).getColumnFamily();
Map<StaticBuffer, KCVMutation> mutations = batchentry.getValue();
for (Map.Entry<StaticBuffer, KCVMutation> ent : mutations.entrySet()) {
// The CLMs for additions and deletions are separated because
// Astyanax's operation timestamp cannot be set on a per-delete
// or per-addition basis.
KCVMutation titanMutation = ent.getValue();
ByteBuffer key = ent.getKey().asByteBuffer();
if (titanMutation.hasDeletions()) {
ColumnListMutation<ByteBuffer> dels = m.withRow(columnFamily, key);
dels.setTimestamp(commitTime.getDeletionTime(times));
for (StaticBuffer b : titanMutation.getDeletions()) dels.deleteColumn(b.as(StaticBuffer.BB_FACTORY));
}
if (titanMutation.hasAdditions()) {
ColumnListMutation<ByteBuffer> upds = m.withRow(columnFamily, key);
upds.setTimestamp(commitTime.getAdditionTime(times));
for (Entry e : titanMutation.getAdditions()) {
Integer ttl = (Integer) e.getMetaData().get(EntryMetaData.TTL);
if (null != ttl && ttl > 0) {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY), ttl);
} else {
upds.putColumn(e.getColumnAs(StaticBuffer.BB_FACTORY), e.getValueAs(StaticBuffer.BB_FACTORY));
}
}
}
}
}
try {
m.execute();
} catch (ConnectionException e) {
throw new TemporaryBackendException(e);
}
sleepAfterWrite(txh, commitTime);
}
Aggregations