Created
August 23, 2012 18:40
-
-
Save singhgurjeet/3440073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class changeme extends TableMapFn { | |
HTable table; | |
TableTranslator translator; | |
Map<Integer, Tables.RowSegment.Builder> rowSegments; | |
public changeme(HTable table, TableTranslator translator) { | |
this.table = table; | |
this.translator = translator; | |
} | |
private int changemetoo(ColumnInfo col) { | |
int segmentIndex = (int)col.getIndex()/translator.getSegmentSize(); | |
if (! rowSegments.containsKey(segmentIndex)) | |
rowSegments.put(segmentIndex, Tables.RowSegment.newBuilder()); | |
return segmentIndex; | |
} | |
@Override | |
public boolean onCell(long row, ColumnInfo col, long val) { | |
int segmentIndex = changemetoo(col); | |
rowSegments.get(segmentIndex).addCell((int)col.getIndex()/translator.getSegmentSize(), CellValue.newBuilder().setL(val)); | |
return true; | |
} | |
@Override | |
public boolean onCell(long row, ColumnInfo col, double val) { | |
int segmentIndex = changemetoo(col); | |
rowSegments.get(segmentIndex).addCell((int)col.getIndex()/translator.getSegmentSize(), CellValue.newBuilder().setD(val)); | |
return true; | |
} | |
@Override | |
public boolean onCell(long row, ColumnInfo col, String val) { | |
int segmentIndex = changemetoo(col); | |
rowSegments.get(segmentIndex).addCell((int)col.getIndex()/translator.getSegmentSize(), CellValue.newBuilder().setS(val)); | |
return true; | |
} | |
@Override | |
public boolean onRowEnd(long row) { | |
List<Put> puts = new ArrayList<Put>(); | |
for (Map.Entry<Integer, Tables.RowSegment.Builder> entry : rowSegments.entrySet()) | |
puts.add(new Put(Bytes.toBytes(row)).add(translator.getFamily(), Bytes.toBytes((int)entry.getKey()), entry.getValue().build().toByteArray())); | |
try { | |
table.put(puts); | |
} catch (IOException e) { | |
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. | |
} | |
return true; | |
} | |
@Override | |
public void onRowBegin(long row) { | |
rowSegments = new HashMap<Integer, Tables.RowSegment.Builder>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment