import org.apache.flume.Context; import org.apache.flume.Event; import org.apache.flume.conf.ComponentConfiguration; import org.apache.flume.sink.hbase.AsyncHbaseEventSerializer; import org.hbase.async.AtomicIncrementRequest; import org.hbase.async.PutRequest; import java.util.ArrayList; import java.util.List; /** * Created by root on 12/5/17. */ public class SplittingSerializer implements AsyncHbaseEventSerializer { private byte[] table; private byte[] colFam; private Event currentEvent; private byte[][]rentRowKey; private final byte[] eventCountCol = "eventCount".getBytes(); columnNames; private final List puts = new ArrayList(); private final List incs = new ArrayList(); private byte[] cur public void initialize(byte[] table, byte[] cf) { this.table = table; this.colFam = cf; //Can not get the columns from context in configure method. Had to hard coded here. columnNames = new byte[3][]; columnNames[0] = "name".getBytes(); columnNames[1] = "id".getBytes(); columnNames[2] = "phone".getBytes(); } public void setEvent(Event event) { // Set the event and verify that the rowKey is not present this.currentEvent = event; /* //Don't know how to set the key of event header. String rowKeyStr = currentEvent.getHeaders().get("rowKey"); if (rowKeyStr == null) { throw new FlumeException("No row key found in headers!"); } currentRowKey = rowKeyStr.getBytes();*/ } public List getActions() { // Split the event body and get the values for the columns String eventStr = new String(currentEvent.getBody()); String[] cols = eventStr.split(","); Long currTime = System.currentTimeMillis(); long revTs = Long.MAX_VALUE - currTime; currentRowKey = (Long.toString(revTs) + cols[0]).getBytes(); puts.clear(); for (int i = 0; i < cols.length; i++) { //Generate a PutRequest for each column. PutRequest req = new PutRequest(table, currentRowKey, colFam, columnNames[i], cols[i].getBytes()); puts.add(req); } return puts; } public List getIncrements() { incs.clear(); //Increment the number of events received incs.add(new AtomicIncrementRequest(table, "totalEvents".getBytes(), colFam, eventCountCol)); return incs; } public void cleanUp() { table = null; colFam = null; currentEvent = null; columnNames = null; currentRowKey = null; } public void configure(Context context) { //Get the column names from the configuration //Did not work. Don't know how to use it. String cols = new String(context.getString("columns")); String[] names = cols.split(","); byte[][] columnNames = new byte[names.length][]; int i = 0; System.out.println("getting columnNames"); for(String name : names) { columnNames[i++] = name.getBytes(); } } public void configure(ComponentConfiguration componentConfiguration) { } } |