| 1 | |
| 2 | {{{ |
| 3 | #!java |
| 4 | package tsmc; |
| 5 | |
| 6 | // TSMC serial program number 4 |
| 7 | // 0. after TSMC3CalculateMR |
| 8 | // 1. run addIndexToTurnover() would add index table to tsmc. |
| 9 | // if you have indexed the table before, don't do it again. |
| 10 | // 2. tune the filter_value in main() and run readSortedValGreater() would show report |
| 11 | |
| 12 | import java.io.IOException; |
| 13 | |
| 14 | import org.apache.hadoop.fs.Path; |
| 15 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 16 | import org.apache.hadoop.hbase.HConstants; |
| 17 | import org.apache.hadoop.hbase.client.Result; |
| 18 | import org.apache.hadoop.hbase.client.ResultScanner; |
| 19 | import org.apache.hadoop.hbase.client.tableindexed.IndexSpecification; |
| 20 | import org.apache.hadoop.hbase.client.tableindexed.IndexedTable; |
| 21 | import org.apache.hadoop.hbase.client.tableindexed.IndexedTableAdmin; |
| 22 | import org.apache.hadoop.hbase.filter.CompareFilter; |
| 23 | import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; |
| 24 | import org.apache.hadoop.hbase.util.Bytes; |
| 25 | |
| 26 | public class TSMC4SortTurnover { |
| 27 | |
| 28 | public void addIndexToTurnover(String OriTable, String IndexID, |
| 29 | String OriColumn) throws IOException { |
| 30 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 31 | conf.addResource(new Path("/opt/hbase/conf/hbase-site.xml")); |
| 32 | IndexedTableAdmin admin = new IndexedTableAdmin(conf); |
| 33 | admin.addIndex(Bytes.toBytes(OriTable), new IndexSpecification(IndexID, |
| 34 | Bytes.toBytes(OriColumn))); |
| 35 | |
| 36 | } |
| 37 | |
| 38 | public void readSortedValGreater(String filter_val) throws IOException { |
| 39 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 40 | conf.addResource(new Path("/opt/hbase/conf/hbase-site.xml")); |
| 41 | // the id of the index to use |
| 42 | String tablename = "tsmc"; |
| 43 | String indexId = "Sum"; |
| 44 | byte[] column_1 = Bytes.toBytes("Turnover:Sum"); |
| 45 | byte[] column_2 = Bytes.toBytes("Detail:Name"); |
| 46 | |
| 47 | byte[] indexStartRow = HConstants.EMPTY_START_ROW; |
| 48 | byte[] indexStopRow = null; |
| 49 | byte[][] indexColumns = null; |
| 50 | |
| 51 | SingleColumnValueFilter indexFilter = new SingleColumnValueFilter(Bytes |
| 52 | .toBytes("Turnover"), Bytes.toBytes("Sum"), |
| 53 | CompareFilter.CompareOp.GREATER_OR_EQUAL, Bytes |
| 54 | .toBytes(filter_val)); |
| 55 | |
| 56 | byte[][] baseColumns = new byte[][] { column_1, column_2 }; |
| 57 | |
| 58 | IndexedTable table = new IndexedTable(conf, Bytes.toBytes(tablename)); |
| 59 | |
| 60 | ResultScanner scanner = table.getIndexedScanner(indexId, indexStartRow, |
| 61 | indexStopRow, indexColumns, indexFilter, baseColumns); |
| 62 | |
| 63 | for (Result rowResult : scanner) { |
| 64 | String sum = Bytes.toString(rowResult.getValue(column_1)); |
| 65 | String name = Bytes.toString(rowResult.getValue(column_2)); |
| 66 | System.out.println(name + " 's turnover is " + sum + " $."); |
| 67 | } |
| 68 | table.close(); |
| 69 | } |
| 70 | |
| 71 | public static void main(String[] args) throws IOException { |
| 72 | |
| 73 | TSMC4SortTurnover tt = new TSMC4SortTurnover(); |
| 74 | tt.addIndexToTurnover("tsmc", "Sum", "Turnover:Sum"); |
| 75 | tt.readSortedValGreater("130"); |
| 76 | |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | }}} |