Java Code Examples for org.apache.cassandra.thrift.Column#setValue()

The following examples show how to use org.apache.cassandra.thrift.Column#setValue() . 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: 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 3
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 4
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 5
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 6
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 7
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);
  }
}