All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hmsonline.storm.cassandra.bolt.mapper.DelimitedColumnsMapper Maven / Gradle / Ivy

package com.hmsonline.storm.cassandra.bolt.mapper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Tuple;
import backtype.storm.tuple.Values;

/**
 * A mapper implementation that emits tuples based on a combination of cassandra
 * rowkey, columnkey, and delimiter.
 * 

* When this bolt received a tuple, it will attempt the following: *

    *
  1. Look up a value in the tuple using rowKeyField
  2. *
  3. Fetch the corresponding row from cassandra
  4. *
  5. Fetch the column columnKeyField value from the row.
  6. *
  7. Split the column value into an array based on delimiter
  8. *
  9. For each value, emit a tuple with {emitIdFieldName}={value}
  10. *
* For example, given the following cassandra row:
* *
 * RowKey: mike
 * => (column=followers, value=john:bob, timestamp=1328848653055000)
 * 
* * and the following bolt setup: * *
 * rowKeyField = "rowKey"
 * columnKeyField = "followers"
 * delimiter = ":"
 * emitIdFieldName = "rowKey"
 * emitValueFieldName = "follower"
 * 
* * if the following tuple were received by the bolt: * *
 * {rowKey:mike}
 * 
* * The following tuples would be emitted: * *
 * {rowKey:mike, follower:john}
 * {rowKey:mike, follower:bob}
 * 
* * @author tgoetz * @author boneill42 */ @SuppressWarnings("serial") public class DelimitedColumnsMapper implements ColumnMapper, Serializable { private String columnKeyField; private String emitIdFieldName; private String emitValueFieldName; private String delimiter; private boolean isDrpc; public DelimitedColumnsMapper(String columnKeyField, String delimiter, String emitIdFieldName, String emitValueFieldName, boolean isDrpc) { this.columnKeyField = columnKeyField; this.delimiter = delimiter; this.isDrpc = isDrpc; this.emitIdFieldName = emitIdFieldName; this.emitValueFieldName = emitValueFieldName; } /** * Declares the fields produced by this bolt. * * @param declarer */ @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { if (this.isDrpc) { declarer.declare(new Fields("id", this.emitIdFieldName, this.emitValueFieldName)); } else { declarer.declare(new Fields(this.emitIdFieldName, this.emitValueFieldName)); } } /** * Given a set of columns, maps to values to emit. * * @param columns * @return */ @Override public List mapToValues(String rowKey, Map columns, Tuple input) { List values = new ArrayList(); String delimVal = columns.get(this.columnKeyField); if (delimVal != null) { String[] vals = delimVal.split(this.delimiter); for (String val : vals) { if (this.isDrpc) { values.add(new Values(input.getValue(0), rowKey, val)); } else { values.add(new Values(rowKey, val)); } } } return values; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy