org.apache.cassandra.thrift.Column Java Examples

The following examples show how to use org.apache.cassandra.thrift.Column. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraTransaction.java    From Doradus with Apache License 2.0 6 votes vote down vote up
private static Mutation createMutation(byte[] colName, byte[] colValue, long timestamp) {
    if (colValue == null) {
        colValue = EMPTY_BYTES;
    }
    Column col = new Column();
    col.setName(colName);
    col.setValue(colValue);
    col.setTimestamp(timestamp);
    
    ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
    cosc.setColumn(col);
    
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(cosc);
    return mutation;
}
 
Example #2
Source File: ThriftByFieldNamesFn.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
private List<Mutation> getMutations(final T input) {
  List<Mutation> mutations = Lists.newArrayList();

  long timestamp = getTimestamp(input);
  Optional<Integer> ttl = getTtl(input);

  for (Schema.Field field : input.getSchema().getFields()) {
    int fieldPos = field.pos();
    if (fieldPos == rowKeyIndex || fieldPos == ttlIndex || fieldPos == timestampIndex) {
      continue;
    }

    Object fieldValue = input.get(fieldPos);

    Column column = new Column();
    column.setName(ByteBufferUtil.bytes(field.name()));
    column.setTimestamp(timestamp);
    if (ttl.isPresent()) {
      column.setTtl(ttl.get());
    }
    column.setValue(CassandraRecordUtils.toByteBuffer(fieldValue));

    Mutation mutation = new Mutation();
    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
    mutation.column_or_supercolumn.column = column;

    mutations.add(mutation);
  }


  return mutations;
}
 
Example #3
Source File: CassandraInputData.java    From learning-hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a cassandra row to a Kettle row
 * 
 * @param metaData meta data on the cassandra column family being read from
 * @param cassandraRow a row from the column family
 * @param outputFormatMap a Map of output field names to indexes in the
 *          outgoing Kettle row structure
 * @return a Kettle row
 * @throws KettleException if a problem occurs
 */
public Object[] cassandraRowToKettle(CassandraColumnMetaData metaData,
    CqlRow cassandraRow, Map<String, Integer> outputFormatMap)
    throws KettleException {

  Object[] outputRowData = RowDataUtil
      .allocateRowData(m_outputRowMeta.size());
  Object key = metaData.getKeyValue(cassandraRow);
  if (key == null) {
    throw new KettleException("Unable to obtain a key value for the row!");
  }

  String keyName = metaData.getKeyName();
  int keyIndex = m_outputRowMeta.indexOfValue(keyName);
  if (keyIndex < 0) {
    throw new KettleException("Unable to find the key field name '" + keyName
        + "' in the output row meta data!");
  }
  outputRowData[keyIndex] = key;

  // do the columns
  List<Column> rowColumns = cassandraRow.getColumns();
  for (Column aCol : rowColumns) {
    String colName = metaData.getColumnName(aCol);
    Integer outputIndex = outputFormatMap.get(colName);
    if (outputIndex != null) {
      Object colValue = metaData.getColumnValue(aCol);
      outputRowData[outputIndex.intValue()] = colValue;
    }
  }

  return outputRowData;
}
 
Example #4
Source File: CassandraRecordUtils.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
public static Mutation createMutation(Object name, Object value, long timestamp, int ttl) {
  Column column = new Column();
  column.setName(toByteBuffer(name));
  column.setValue(toByteBuffer(value));
  column.setTimestamp(timestamp);
  if (ttl > 0) {
    column.setTtl(ttl);
  }

  Mutation mutation = new Mutation();
  mutation.column_or_supercolumn = new ColumnOrSuperColumn();
  mutation.column_or_supercolumn.column = column;
  return mutation;
}
 
Example #5
Source File: AbstractCassandraInputAdapter.java    From OpenRate with Apache License 2.0 5 votes vote down vote up
private void insertColumnValue(String parentName, String id, String name, String value, long ts) throws TException, TimedOutException, UnavailableException, InvalidRequestException, UnsupportedEncodingException {
  ColumnParent parent = new ColumnParent(parentName);
  Column column = new Column(toByteBuffer(name));
  column.setValue(toByteBuffer(value));
  column.setTimestamp(ts);
  getClient().insert(toByteBuffer(id), parent, column, ConsistencyLevel.ONE);
}
 
Example #6
Source File: QueryProcessor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static Column thriftify(org.apache.cassandra.db.Cell c)
{
    ByteBuffer value = (c instanceof CounterCell)
                       ? ByteBufferUtil.bytes(CounterContext.instance().total(c.value()))
                       : c.value();
    return new Column(c.name().toByteBuffer()).setValue(value).setTimestamp(c.timestamp());
}
 
Example #7
Source File: ThriftInserter.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public void run(final ThriftClient client) throws IOException
{
    final ByteBuffer key = getKey();
    final List<Column> columns = getColumns();

    List<Mutation> mutations = new ArrayList<>(columns.size());
    for (Column c : columns)
    {
        ColumnOrSuperColumn column = new ColumnOrSuperColumn().setColumn(c);
        mutations.add(new Mutation().setColumn_or_supercolumn(column));
    }
    Map<String, List<Mutation>> row = Collections.singletonMap(type.table, mutations);

    final Map<ByteBuffer, Map<String, List<Mutation>>> record = Collections.singletonMap(key, row);

    timeWithRetry(new RunOp()
    {
        @Override
        public boolean run() throws Exception
        {
            client.batch_mutate(record, settings.command.consistencyLevel);
            return true;
        }

        @Override
        public int partitionCount()
        {
            return 1;
        }

        @Override
        public int rowCount()
        {
            return 1;
        }
    });
}
 
Example #8
Source File: ThriftInserter.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected List<Column> getColumns()
{
    final ColumnSelection selection = select();
    final List<ByteBuffer> values = getColumnValues(selection);
    final List<Column> columns = new ArrayList<>(values.size());
    final List<ByteBuffer> names = select().select(settings.columns.names);
    for (int i = 0 ; i < values.size() ; i++)
        columns.add(new Column(names.get(i))
                    .setValue(values.get(i))
                    .setTimestamp(settings.columns.timestamp != null
                                  ? Long.parseLong(settings.columns.timestamp)
                                  : FBUtilities.timestampMicros()));
    return columns;
}
 
Example #9
Source File: ColumnFamilyWideRowRecordReader.java    From Hive-Cassandra with Apache License 2.0 5 votes vote down vote up
private IColumn unthriftifySuper(SuperColumn super_column) {
  org.apache.cassandra.db.SuperColumn sc = new org.apache.cassandra.db.SuperColumn(
      super_column.name, subComparator);
  for (Column column : super_column.columns) {
    sc.addColumn(unthriftifySimple(column));
  }
  return sc;
}
 
Example #10
Source File: TestRingCache.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * usage: java -cp <configpath> org.apache.cassandra.client.TestRingCache [keyspace row-id-prefix row-id-int]
 * to test a single keyspace/row, use the parameters. row-id-prefix and row-id-int are appended together to form a
 * single row id.  If you supply now parameters, 'Keyspace1' is assumed and will check 9 rows ('row1' through 'row9').
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Throwable
{
    int minRow;
    int maxRow;
    String rowPrefix, keyspace = "Keyspace1";

    if (args.length > 0)
    {
        keyspace = args[0];
        rowPrefix = args[1];
        minRow = Integer.parseInt(args[2]);
        maxRow = minRow + 1;
    }
    else
    {
        minRow = 1;
        maxRow = 10;
        rowPrefix = "row";
    }

    TestRingCache tester = new TestRingCache(keyspace);

    for (int nRows = minRow; nRows < maxRow; nRows++)
    {
        ByteBuffer row = ByteBufferUtil.bytes((rowPrefix + nRows));
        ColumnPath col = new ColumnPath("Standard1").setSuper_column((ByteBuffer)null).setColumn("col1".getBytes());
        ColumnParent parent = new ColumnParent("Standard1").setSuper_column((ByteBuffer)null);

        Collection<InetAddress> endpoints = tester.ringCache.getEndpoint(row);
        InetAddress firstEndpoint = endpoints.iterator().next();
        System.out.printf("hosts with key %s : %s; choose %s%n",
                          new String(row.array()), StringUtils.join(endpoints, ","), firstEndpoint);

        // now, read the row back directly from the host owning the row locally
        tester.setup(firstEndpoint.getHostAddress(), DatabaseDescriptor.getRpcPort());
        tester.thriftClient.set_keyspace(keyspace);
        tester.thriftClient.insert(row, parent, new Column(ByteBufferUtil.bytes("col1")).setValue(ByteBufferUtil.bytes("val1")).setTimestamp(1), ConsistencyLevel.ONE);
        Column column = tester.thriftClient.get(row, col, ConsistencyLevel.ONE).column;
        System.out.println("read row " + new String(row.array()) + " " + new String(column.name.array()) + ":" + new String(column.value.array()) + ":" + column.timestamp);
    }

    System.exit(1);
}
 
Example #11
Source File: ColumnFamilyWideRowRecordReader.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
private IColumn unthriftifyCounter(CounterColumn column) {
  // CounterColumns read the nodeID from the System table, so need the StorageService running
  // and access
  // to cassandra.yaml. To avoid a Hadoop needing access to yaml return a regular Column.
  return new org.apache.cassandra.db.Column(column.name, ByteBufferUtil.bytes(column.value), 0);
}
 
Example #12
Source File: ColumnFamilyWideRowRecordReader.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
private IColumn unthriftifySimple(Column column) {
  return new org.apache.cassandra.db.Column(column.name, column.value, column.timestamp);
}
 
Example #13
Source File: CassandraSuperPut.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public void write(String keySpace, CassandraProxyClient client, JobConf jc) throws IOException {
  ConsistencyLevel flevel = getConsistencyLevel(jc);
  int batchMutation = getBatchMutationSize(jc);
  Map<ByteBuffer,Map<String,List<Mutation>>> mutation_map = new HashMap<ByteBuffer,Map<String,List<Mutation>>>();
  Map<String, List<Mutation>> maps = new HashMap<String, List<Mutation>>();

  int count = 0;
  for (CassandraPut c : subColumns) {
    List<Column> columns = new ArrayList<Column>();
    for (CassandraColumn col : c.getColumns()) {
      Column cassCol = new Column();
      cassCol.setValue(col.getValue());
      cassCol.setTimestamp(col.getTimeStamp());
      cassCol.setName(col.getColumn());
      columns.add(cassCol);

      ColumnOrSuperColumn thisSuperCol = new ColumnOrSuperColumn();
      thisSuperCol.setSuper_column(new SuperColumn(c.getKey(), columns));

      Mutation mutation = new Mutation();
      mutation.setColumn_or_supercolumn(thisSuperCol);

      List<Mutation> mutList = maps.get(col.getColumnFamily());
      if (mutList == null) {
        mutList = new ArrayList<Mutation>();
        maps.put(col.getColumnFamily(), mutList);
      }

      mutList.add(mutation);

      count ++;

      if (count == batchMutation) {
        mutation_map.put(key, maps);

        commitChanges(keySpace, client, flevel, mutation_map);

        //reset mutation map, maps and count;
        mutation_map = new HashMap<ByteBuffer,Map<String,List<Mutation>>>();
        maps = new HashMap<String, List<Mutation>>();
        count = 0;
      }

    }
  }

  if(count > 0) {
    mutation_map.put(key, maps);
    commitChanges(keySpace, client, flevel, mutation_map);
  }
}
 
Example #14
Source File: CassandraPut.java    From Hive-Cassandra with Apache License 2.0 4 votes vote down vote up
@Override
public void write(String keySpace, CassandraProxyClient client, JobConf jc) throws IOException {
  ConsistencyLevel flevel = getConsistencyLevel(jc);
  int batchMutation = getBatchMutationSize(jc);
  Map<ByteBuffer,Map<String,List<Mutation>>> mutation_map = new HashMap<ByteBuffer,Map<String,List<Mutation>>>();

  Map<String, List<Mutation>> maps = new HashMap<String, List<Mutation>>();

  int count = 0;
  // TODO check for counter
  for (CassandraColumn col : columns) {
    Column cassCol = new Column();
    cassCol.setValue(col.getValue());
    cassCol.setTimestamp(col.getTimeStamp());
    cassCol.setName(col.getColumn());

    ColumnOrSuperColumn thisCol = new ColumnOrSuperColumn();
    thisCol.setColumn(cassCol);

    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(thisCol);

    List<Mutation> mutList = maps.get(col.getColumnFamily());
    if (mutList == null) {
      mutList = new ArrayList<Mutation>();
      maps.put(col.getColumnFamily(), mutList);
    }

    mutList.add(mutation);
    count ++;

    if (count == batchMutation) {
      mutation_map.put(key, maps);

      commitChanges(keySpace, client, flevel, mutation_map);

      //reset mutation map, maps and count;
      mutation_map = new HashMap<ByteBuffer,Map<String,List<Mutation>>>();
      maps = new HashMap<String, List<Mutation>>();
      count = 0;
    }
  }

  if(count > 0) {
    mutation_map.put(key, maps);
    commitChanges(keySpace, client, flevel, mutation_map);
  }
}
 
Example #15
Source File: CassandraOutputData.java    From learning-hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Adds a kettle row to a thrift-based batch (builds the map of keys to
 * mutations).
 * 
 * @param thriftBatch
 *            the map of keys to mutations
 * @param colFamilyName
 *            the name of the column family (table) to insert into
 * @param inputMeta
 *            Kettle input row meta data
 * @param keyIndex
 *            the index of the incoming field to use as the key for
 *            inserting
 * @param row
 *            the Kettle row
 * @param cassandraMeta
 *            meta data on the columns in the cassandra column family
 *            (table)
 * @param insertFieldsNotInMetaData
 *            true if any Kettle fields that are not in the Cassandra column
 *            family (table) meta data are to be inserted. This is
 *            irrelevant if the user has opted to have the step initially
 *            update the Cassandra meta data for incoming fields that are
 *            not known about.
 * 
 * @return true if the row was added to the batch
 * 
 * @throws KettleException
 *             if a problem occurs
 */
public static boolean addRowToThriftBatch(
		Map<ByteBuffer, Map<String, List<Mutation>>> thriftBatch,
		String colFamilyName, RowMetaInterface inputMeta, int keyIndex,
		Object[] row, CassandraColumnMetaData cassandraMeta,
		boolean insertFieldsNotInMetaData, LogChannelInterface log,
		boolean isAsIndexColumn) throws KettleException {

	if (!preAddChecks(inputMeta, keyIndex, row, log)) {
		return false;
	}
	ValueMetaInterface keyMeta = inputMeta.getValueMeta(keyIndex);
	ByteBuffer keyBuff = cassandraMeta.kettleValueToByteBuffer(keyMeta,
			row[keyIndex], true);

	Map<String, List<Mutation>> mapCF = thriftBatch.get(keyBuff);
	List<Mutation> mutList = null;

	// check to see if we have already got some mutations for this key in
	// the batch
	if (mapCF != null) {
		mutList = mapCF.get(colFamilyName);
	} else {
		mapCF = new HashMap<String, List<Mutation>>(1);
		mutList = new ArrayList<Mutation>();
	}

	for (int i = 0; i < inputMeta.size(); i++) {
		if (i != keyIndex) {
			ValueMetaInterface colMeta = inputMeta.getValueMeta(i);
			String colName = colMeta.getName();
			if (!cassandraMeta.columnExistsInSchema(colName)
					&& !insertFieldsNotInMetaData) {
				continue;
			}

			// don't insert if null!
			if (colMeta.isNull(row[i])) {
				continue;
			}

			Column col = new Column(
					cassandraMeta.columnNameToByteBuffer(colName));
			if (isAsIndexColumn) {
				col = col.setValue(cassandraMeta.kettleValueToByteBuffer(
						colMeta, "-", false));
			} else {
				col = col.setValue(cassandraMeta.kettleValueToByteBuffer(
						colMeta, row[i], false));
			}
	
			col = col.setTimestamp(System.currentTimeMillis());
			ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
			cosc.setColumn(col);
			Mutation mut = new Mutation();
			mut.setColumn_or_supercolumn(cosc);
			mutList.add(mut);
		}
	}

	// column family name -> mutations
	mapCF.put(colFamilyName, mutList);

	// row key -> column family - > mutations
	thriftBatch.put(keyBuff, mapCF);

	return true;
}
 
Example #16
Source File: CassandraColumnMetaData.java    From learning-hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Decode the supplied column value. Uses the default validation class to
 * decode the value if the column is not explicitly defined in the schema.
 * 
 * @param aCol
 * @return
 * @throws KettleException
 */
public Object getColumnValue(Column aCol) throws KettleException {
  String colName = getColumnName(aCol);

  // Clients should use getKey() for getting the key
  if (colName.equals("KEY")) {
    return null;
  }

  String decoder = m_columnMeta.get(colName);
  if (decoder == null) {
    // column is not in schema so use default validator
    decoder = m_defaultValidationClass;
  }

  String fullDecoder = decoder;
  if (decoder.indexOf('(') > 0) {
    decoder = decoder.substring(0, decoder.indexOf('('));
  }

  if (decoder.indexOf("BytesType") > 0) {
    return aCol.getValue(); // raw bytes
  }

  ByteBuffer valueBuff = aCol.bufferForValue();
  Object result = getColumnValue(valueBuff, fullDecoder);

  // check for indexed values
  if (m_indexedVals.containsKey(colName)) {
    HashSet<Object> vals = m_indexedVals.get(colName);

    // look for the correct index
    int foundIndex = -1;
    Object[] indexedV = vals.toArray();
    for (int i = 0; i < indexedV.length; i++) {
      if (indexedV[i].equals(result)) {
        foundIndex = i;
        break;
      }
    }

    if (foundIndex >= 0) {
      result = new Integer(foundIndex);
    } else {
      result = null; // any values that are not indexed are unknown...
    }
  }

  return result;
}
 
Example #17
Source File: CassandraColumnMetaData.java    From learning-hadoop with Apache License 2.0 4 votes vote down vote up
public String getColumnName(Column aCol) throws KettleException {
  ByteBuffer b = aCol.bufferForName();

  String decodedColName = getColumnValue(b, m_columnComparator).toString();
  return decodedColName;
}
 
Example #18
Source File: CassandraInputData.java    From learning-hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * Converts a cassandra row to a Kettle row in the key, colName, colValue,
 * timestamp format
 * 
 * @param metaData meta data on the cassandra column family being read from
 * @param cassandraRow a row from the column family
 * @param cassandraColIter an interator over columns for the current row
 * 
 * @return a Kettle row
 * @throws KettleException if a problem occurs
 */
public Object[] cassandraRowToKettleTupleMode(
    CassandraColumnMetaData metaData, CqlRow cassandraRow,
    Iterator<Column> cassandraColIter) throws KettleException {

  Object[] outputRowData = RowDataUtil
      .allocateRowData(m_outputRowMeta.size());
  Object key = metaData.getKeyValue(cassandraRow);
  if (key == null) {
    throw new KettleException("Unable to obtain a key value for the row!");
  }

  String keyName = metaData.getKeyName();
  int keyIndex = m_outputRowMeta.indexOfValue(keyName);
  if (keyIndex < 0) {
    throw new KettleException("Unable to find the key field name '" + keyName
        + "' in the output row meta data!");
  }
  outputRowData[keyIndex] = key;

  // advance the iterator to the next column
  if (cassandraColIter.hasNext()) {
    Column aCol = cassandraColIter.next();

    String colName = metaData.getColumnName(aCol);

    // skip the key
    if (colName.equals("KEY")) {
      if (cassandraColIter.hasNext()) {
        aCol = cassandraColIter.next();
        colName = metaData.getColumnName(aCol);
      } else {
        // run out of columns
        return null;
      }
    }

    // for queries that specify column names we need to check that the value
    // is not null in this row
    while (metaData.getColumnValue(aCol) == null) {
      if (cassandraColIter.hasNext()) {
        aCol = cassandraColIter.next();
        colName = metaData.getColumnName(aCol);
      } else {
        return null;
      }
    }

    outputRowData[1] = colName;

    // do the value (stored as a string)
    Object colValue = metaData.getColumnValue(aCol);

    String stringV = colValue.toString();
    outputRowData[2] = stringV;

    if (colValue instanceof Date) {
      ValueMeta tempDateMeta = new ValueMeta("temp",
          ValueMetaInterface.TYPE_DATE);
      stringV = tempDateMeta.getString(colValue);
      outputRowData[2] = stringV;
    } else if (colValue instanceof byte[]) {
      outputRowData[2] = colValue;
    }

    // the timestamp as a date object
    long timestampL = aCol.getTimestamp();
    outputRowData[3] = timestampL;
  } else {
    return null; // signify no more columns for this row...
  }

  return outputRowData;
}