List of usage examples for org.apache.cassandra.utils ByteBufferUtil EMPTY_BYTE_BUFFER
ByteBuffer EMPTY_BYTE_BUFFER
To view the source code for org.apache.cassandra.utils ByteBufferUtil EMPTY_BYTE_BUFFER.
Click Source Link
From source file:WordCountCounters.java
License:Apache License
public int run(String[] args) throws Exception { Job job = new Job(getConf(), "wordcountcounters"); job.setJarByClass(WordCountCounters.class); job.setMapperClass(SumMapper.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH_PREFIX)); job.setInputFormatClass(ColumnFamilyInputFormat.class); ConfigHelper.setInputRpcPort(job.getConfiguration(), "9160"); ConfigHelper.setInputInitialAddress(job.getConfiguration(), "localhost"); ConfigHelper.setInputPartitioner(job.getConfiguration(), "org.apache.cassandra.dht.Murmur3Partitioner"); ConfigHelper.setInputColumnFamily(job.getConfiguration(), WordCount.KEYSPACE, WordCountCounters.COUNTER_COLUMN_FAMILY); SlicePredicate predicate = new SlicePredicate() .setSlice_range(new SliceRange().setStart(ByteBufferUtil.EMPTY_BYTE_BUFFER) .setFinish(ByteBufferUtil.EMPTY_BYTE_BUFFER).setCount(100)); ConfigHelper.setInputSlicePredicate(job.getConfiguration(), predicate); job.waitForCompletion(true);/*from w ww . j a v a 2s . c o m*/ return 0; }
From source file:andromache.hadoop.CassandraRecordWriter.java
License:Apache License
/** * Checks for null or emptiness within a key. * * @param keyBuff//from w w w .ja v a 2s. com * @return * @throws java.nio.charset.CharacterCodingException */ private boolean isKeyEmpty(ByteBuffer keyBuff) throws CharacterCodingException { if (keyBuff == null) { return true; } boolean isEmptyBuffer = false; try { isEmptyBuffer = ByteBufferUtil.compareUnsigned(ByteBufferUtil.EMPTY_BYTE_BUFFER, keyBuff) == 0; } catch (Exception e) { isEmptyBuffer = false; // if we could not determine it, then we don't touch the logic } return isEmptyBuffer; }
From source file:co.cask.hydrator.plugin.batch.sink.BatchCassandraSink.java
License:Apache License
private ByteBuffer encodeObject(Object object, Schema schema) throws IOException { switch (schema.getType()) { case NULL:/*from w w w. j av a 2 s . c o m*/ return ByteBufferUtil.EMPTY_BYTE_BUFFER; case BOOLEAN: byte[] bytes = new byte[1]; bytes[0] = (byte) ((boolean) object ? 1 : 0); return ByteBuffer.wrap(bytes); case INT: return ByteBufferUtil.bytes((int) object); case LONG: return ByteBufferUtil.bytes((long) object); case FLOAT: return ByteBufferUtil.bytes((float) object); case DOUBLE: return ByteBufferUtil.bytes((double) object); case BYTES: return ByteBuffer.wrap((byte[]) object); case STRING: case ENUM: // Currently there is no standard container to represent enum type return ByteBufferUtil.bytes((String) object); case UNION: if (schema.isNullableSimple()) { return object == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : encodeObject(object, schema.getNonNullable()); } } throw new IOException("Unsupported field type; only simple types are supported: " + schema); }
From source file:com.datastax.demo.portfolio.controller.PortfolioMgrHandler.java
License:Apache License
public List<Portfolio> get_portfolios(String start_key, int limit) throws TException { KeyRange kr = new KeyRange(limit).setStart_key(start_key.getBytes()) .setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); try {/*from w ww .ja v a 2s . c om*/ List<KeySlice> kslices = getClient().get_range_slices(pcp, sp, kr, ConsistencyLevel.ONE); CqlResult result = getClient().execute_cql_query( ByteBufferUtil.bytes(buildPortfoliosQuery(start_key, limit)), Compression.NONE); //List<Portfolio> portfolios = buildPorfoliosFromRangeSlices(kslices); List<Portfolio> portfolios = buildPorfoliosFromCqlResult(result); addLossInformation(portfolios); addHistInformation(portfolios); return portfolios; } catch (Exception e) { throw new TException(e); } }
From source file:com.hmsonline.virgil.CassandraStorage.java
License:Apache License
@PooledConnection public JSONArray getRows(String keyspace, String columnFamily, String queryStr, ConsistencyLevel consistencyLevel) throws InvalidRequestException, UnavailableException, TimedOutException, TException, CharacterCodingException { if (StringUtils.isNotBlank(queryStr)) { return getRowsWithQuery(keyspace, columnFamily, queryStr, consistencyLevel); }//www . ja va 2 s . c o m SlicePredicate predicate = new SlicePredicate(); SliceRange range = new SliceRange(ByteBufferUtil.bytes(""), ByteBufferUtil.bytes(""), false, MAX_COLUMNS); predicate.setSlice_range(range); KeyRange keyRange = new KeyRange(MAX_ROWS); keyRange.setStart_key(ByteBufferUtil.bytes("")); keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); ColumnParent parent = new ColumnParent(columnFamily); List<KeySlice> rows = getConnection(keyspace).get_range_slices(parent, predicate, keyRange, consistencyLevel); return JsonMarshaller.marshallRows(rows, true); }
From source file:com.impetus.client.cassandra.query.CassQuery.java
License:Apache License
/** * Prepare index clause./*from ww w. ja v a2 s . c om*/ * * @param m * the m * @param isQueryForInvertedIndex * the is query for inverted index * @return the map */ Map<Boolean, List<IndexClause>> prepareIndexClause(EntityMetadata m, boolean isQueryForInvertedIndex) { IndexClause indexClause = new IndexClause(new ArrayList<IndexExpression>(), ByteBufferUtil.EMPTY_BYTE_BUFFER, maxResult); List<IndexClause> clauses = new ArrayList<IndexClause>(); List<IndexExpression> expr = new ArrayList<IndexExpression>(); Map<Boolean, List<IndexClause>> idxClauses = new HashMap<Boolean, List<IndexClause>>(1); // check if id column are mixed with other columns or not? String idColumn = ((AbstractAttribute) m.getIdAttribute()).getJPAColumnName(); boolean idPresent = false; if (log.isInfoEnabled()) { log.info("Preparing index clause for query {}", getJPAQuery()); } for (Object o : getKunderaQuery().getFilterClauseQueue()) { if (o instanceof FilterClause) { FilterClause clause = ((FilterClause) o); String fieldName = clause.getProperty(); // in case id column matches with field name, set it for first // time. if (!idPresent && idColumn.equalsIgnoreCase(fieldName)) { idPresent = true; } String condition = clause.getCondition(); List<Object> value = clause.getValue(); if (value != null && value.size() > 1) { log.error("IN clause is not enabled for thrift, use cql3."); throw new QueryHandlerException("IN clause is not enabled for thrift, use cql3."); } IndexOperator operator = getOperator(condition, idPresent); IndexExpression expression = new IndexExpression(ByteBufferUtil.bytes(fieldName), operator, getBytesValue(fieldName, m, value.get(0))); expr.add(expression); } else { // Case of AND and OR clause. String opr = o.toString(); if (opr.equalsIgnoreCase("or")) { log.error("Support for OR clause is not enabled within cassandra."); throw new QueryHandlerException("Unsupported clause " + opr + " for cassandra."); } } } if (!StringUtils.isBlank(getKunderaQuery().getFilter())) { indexClause.setExpressions(expr); clauses.add(indexClause); } idxClauses.put(idPresent, clauses); return idxClauses; }
From source file:com.impetus.client.cassandra.thrift.CQLTranslator.java
License:Apache License
/** * Append value./* www . jav a 2s .com*/ * * @param builder * the builder * @param fieldClazz * the field clazz * @param value * the value * @param useToken * the use token */ private void appendValue(StringBuilder builder, Class fieldClazz, Object value, boolean useToken) { // To allow handle byte array class object by converting it to string if (fieldClazz != null && fieldClazz.isAssignableFrom(byte[].class)) { value = value != null ? value : ByteBufferUtil.EMPTY_BYTE_BUFFER.array(); StringBuilder hexstr = new StringBuilder("0x"); builder.append(hexstr.append((Hex.encodeHex((byte[]) value)))); } else { if (useToken) { builder.append(TOKEN); } if (fieldClazz != null && value != null && (fieldClazz.isAssignableFrom(String.class) || isDate(fieldClazz) || fieldClazz.isAssignableFrom(char.class) || fieldClazz.isAssignableFrom(Character.class) || value instanceof Enum)) { if (fieldClazz.isAssignableFrom(String.class)) { // To allow escape character value = ((String) value).replaceAll("'", "''"); } builder.append("'"); if (isDate(fieldClazz)) // For CQL, date has to // be in date.getTime() { builder.append(PropertyAccessorFactory.getPropertyAccessor(fieldClazz).toString(value)); } else if (value instanceof Enum) { builder.append(((Enum) value).name()); } else { builder.append(value); } builder.append("'"); } else { builder.append(value); } } if (useToken) { builder.append(CLOSE_BRACKET); } }
From source file:com.impetus.client.cassandra.thrift.ThriftClient.java
License:Apache License
/** * Retrieves IDs for a given column.//w w w . j a v a 2 s.c o m * * @param schemaName * the schema name * @param tableName * the table name * @param pKeyName * the key name * @param columnName * the column name * @param columnValue * the column value * @param entityClazz * the entity clazz * @return the object[] */ @Override public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName, Object columnValue, Class entityClazz) { List<Object> rowKeys = new ArrayList<Object>(); if (getCqlVersion().equalsIgnoreCase(CassandraConstants.CQL_VERSION_3_0)) { rowKeys = findIdsByColumnUsingCql(schemaName, tableName, pKeyName, columnName, columnValue, entityClazz); } else { EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.setSlice_range(new SliceRange(ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, Integer.MAX_VALUE)); String childIdStr = PropertyAccessorHelper.getString(columnValue); IndexExpression ie = new IndexExpression( UTF8Type.instance.decompose(columnName + Constants.JOIN_COLUMN_NAME_SEPARATOR + childIdStr), IndexOperator.EQ, UTF8Type.instance.decompose(childIdStr)); List<IndexExpression> expressions = new ArrayList<IndexExpression>(); expressions.add(ie); IndexClause ix = new IndexClause(); ix.setStart_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); ix.setCount(Integer.MAX_VALUE); ix.setExpressions(expressions); ColumnParent columnParent = new ColumnParent(tableName); Connection conn = null; try { conn = getConnection(); List<KeySlice> keySlices = conn.getClient().get_indexed_slices(columnParent, ix, slicePredicate, getConsistencyLevel()); rowKeys = ThriftDataResultHelper.getRowKeys(keySlices, metadata); } catch (InvalidRequestException e) { log.error("Error while fetching key slices of column family {} for column name {} , Caused by: .", tableName, columnName, e); throw new KunderaException(e); } catch (UnavailableException e) { log.error("Error while fetching key slices of column family {} for column name {} , Caused by: .", tableName, columnName, e); throw new KunderaException(e); } catch (TimedOutException e) { log.error("Error while fetching key slices of column family {} for column name {} , Caused by: .", tableName, columnName, e); throw new KunderaException(e); } catch (TException e) { log.error("Error while fetching key slices of column family {} for column name {} , Caused by: .", tableName, columnName, e); throw new KunderaException(e); } finally { releaseConnection(conn); } } if (rowKeys != null && !rowKeys.isEmpty()) { return rowKeys.toArray(new Object[0]); } if (log.isInfoEnabled()) { log.info("No record found!, returning null."); } return null; }
From source file:com.impetus.client.cassandra.thrift.ThriftClient.java
License:Apache License
@Override public List<Object> findByRelation(String colName, Object colValue, Class entityClazz) { EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz); List<Object> entities = null; if (isCql3Enabled(m)) { entities = new ArrayList<Object>(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); List<AbstractManagedType> subManagedType = ((AbstractManagedType) entityType).getSubManagedType(); if (subManagedType.isEmpty()) { entities.addAll(findByRelationQuery(m, colName, colValue, entityClazz, dataHandler)); } else {//from w w w. jav a2 s. c om for (AbstractManagedType subEntity : subManagedType) { EntityMetadata subEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, subEntity.getJavaType()); entities.addAll(findByRelationQuery(subEntityMetadata, colName, colValue, subEntityMetadata.getEntityClazz(), dataHandler)); // TODOO:: if(entities != null) } } } else { SlicePredicate slicePredicate = new SlicePredicate(); slicePredicate.setSlice_range(new SliceRange(ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, Integer.MAX_VALUE)); IndexExpression ie = new IndexExpression(UTF8Type.instance.decompose(colName), IndexOperator.EQ, ByteBuffer.wrap(PropertyAccessorHelper.getBytes(colValue))); List<IndexExpression> expressions = new ArrayList<IndexExpression>(); expressions.add(ie); IndexClause ix = new IndexClause(); ix.setStart_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); ix.setCount(Integer.MAX_VALUE); ix.setExpressions(expressions); ColumnParent columnParent = new ColumnParent(m.getTableName()); List<KeySlice> keySlices = null; Connection conn = null; try { conn = getConnection(); if (!m.getType().equals(Type.SUPER_COLUMN_FAMILY)) { keySlices = conn.getClient().get_indexed_slices(columnParent, ix, slicePredicate, getConsistencyLevel()); } } catch (InvalidRequestException e) { if (e.why != null && e.why.contains("No indexed columns")) { return entities; } else { log.error("Error while finding relations for column family {} , Caused by: .", m.getTableName(), e); throw new KunderaException(e); } } catch (UnavailableException e) { log.error("Error while finding relations for column family {} , Caused by: .", m.getTableName(), e); throw new KunderaException(e); } catch (TimedOutException e) { log.error("Error while finding relations for column family {} , Caused by: .", m.getTableName(), e); throw new KunderaException(e); } catch (TException e) { log.error("Error while finding relations for column family {} , Caused by: .", m.getTableName(), e); throw new KunderaException(e); } finally { releaseConnection(conn); } if (keySlices != null) { entities = new ArrayList<Object>(keySlices.size()); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); List<AbstractManagedType> subManagedType = ((AbstractManagedType) entityType).getSubManagedType(); if (subManagedType.isEmpty()) { entities = populateData(m, keySlices, entities, m.getRelationNames() != null, m.getRelationNames()); } else { for (AbstractManagedType subEntity : subManagedType) { EntityMetadata subEntityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, subEntity.getJavaType()); entities = populateData(subEntityMetadata, keySlices, entities, subEntityMetadata.getRelationNames() != null, subEntityMetadata.getRelationNames()); // TODOO:: if(entities != null) } } } } return entities; }
From source file:com.impetus.client.cassandra.thrift.ThriftClient.java
License:Apache License
@Override public List find(List<IndexClause> ixClause, EntityMetadata m, boolean isRelation, List<String> relations, int maxResult, List<String> columns) { List<Object> entities = new ArrayList<Object>(); Connection conn = null;//from www .j a v a2s .co m try { // ixClause can be 0,1 or more! SlicePredicate slicePredicate = new SlicePredicate(); if (columns != null && !columns.isEmpty()) { List asList = new ArrayList(32); for (String colName : columns) { if (colName != null) { asList.add(UTF8Type.instance.decompose(colName)); } } slicePredicate.setColumn_names(asList); } else { SliceRange sliceRange = new SliceRange(); sliceRange.setStart(ByteBufferUtil.EMPTY_BYTE_BUFFER); sliceRange.setFinish(ByteBufferUtil.EMPTY_BYTE_BUFFER); slicePredicate.setSlice_range(sliceRange); } conn = getConnection(); if (ixClause.isEmpty()) { KeyRange keyRange = new KeyRange(maxResult); keyRange.setStart_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); keyRange.setEnd_key(ByteBufferUtil.EMPTY_BYTE_BUFFER); if (m.isCounterColumnType()) { List<KeySlice> ks = conn.getClient().get_range_slices(new ColumnParent(m.getTableName()), slicePredicate, keyRange, getConsistencyLevel()); entities = onCounterColumn(m, isRelation, relations, ks); } else { List<KeySlice> keySlices = conn.getClient().get_range_slices(new ColumnParent(m.getTableName()), slicePredicate, keyRange, getConsistencyLevel()); if (m.getType().isSuperColumnFamilyMetadata()) { Map<ByteBuffer, List<SuperColumn>> qResults = ThriftDataResultHelper .transformThriftResult(ColumnFamilyType.SUPER_COLUMN, keySlices, null); entities = new ArrayList<Object>(qResults.size()); computeEntityViaSuperColumns(m, isRelation, relations, entities, qResults); } else { Map<ByteBuffer, List<Column>> qResults = ThriftDataResultHelper .transformThriftResult(ColumnFamilyType.COLUMN, keySlices, null); entities = new ArrayList<Object>(qResults.size()); computeEntityViaColumns(m, isRelation, relations, entities, qResults); } } } else { entities = new ArrayList<Object>(); for (IndexClause ix : ixClause) { List<KeySlice> keySlices = conn.getClient().get_indexed_slices( new ColumnParent(m.getTableName()), ix, slicePredicate, getConsistencyLevel()); Map<ByteBuffer, List<Column>> qResults = ThriftDataResultHelper .transformThriftResult(ColumnFamilyType.COLUMN, keySlices, null); // iterate through complete map and populate. entities = new ArrayList<Object>(qResults.size()); computeEntityViaColumns(m, isRelation, relations, entities, qResults); } } } catch (InvalidRequestException irex) { log.error("Error during executing find of column family {}, Caused by: .", m.getTableName(), irex); throw new PersistenceException(irex); } catch (UnavailableException uex) { log.error("Error during executing find of column family {}, Caused by: .", m.getTableName(), uex); throw new PersistenceException(uex); } catch (TimedOutException tex) { log.error("Error during executing find of column family {}, Caused by: .", m.getTableName(), tex); throw new PersistenceException(tex); } catch (TException tex) { log.error("Error during executing find of column family {}, Caused by: .", m.getTableName(), tex); throw new PersistenceException(tex); } finally { releaseConnection(conn); } return entities; }