60 | | Hadoop HBase數據導入和索引測試 |
61 | | [http://bbs.telewiki.cn/blog/qianling/entry/hadoop_hbase%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%85%A5%E5%92%8C%E7%B4%A2%E5%BC%95%E6%B5%8B%E8%AF%95] |
62 | | |
63 | | |
64 | | |
65 | | = Secondary indexes in HBase = |
| 74 | |
| 75 | = Secondary indexes in HBase 程式碼說明 (v 0.19 ) = |
| 76 | == conf/hbase-site.xml == |
| 77 | {{{ |
| 78 | #!text |
| 79 | $HBASE_INSTALL_DIR/conf/hbase-site.xml and add the following property to it. |
| 80 | |
| 81 | <property> |
| 82 | <name>hbase.regionserver.class</name> |
| 83 | <value>org.apache.hadoop.hbase.ipc.IndexedRegionInterface</value> |
| 84 | </property> |
| 85 | |
| 86 | <property> |
| 87 | <name>hbase.regionserver.impl</name> |
| 88 | <value> |
| 89 | org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegionServer |
| 90 | </value> |
| 91 | </property> |
| 92 | }}} |
| 93 | |
| 94 | == Adding secondary index while creating table == |
| 95 | |
| 96 | {{{ |
| 97 | #!java |
| 98 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 99 | conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml")); |
| 100 | |
| 101 | HTableDescriptor desc = new HTableDescriptor("test_table"); |
| 102 | |
| 103 | desc.addFamily(new HColumnDescriptor("columnfamily1:")); |
| 104 | desc.addFamily(new HColumnDescriptor("columnfamily2:")); |
| 105 | |
| 106 | desc.addIndex(new IndexSpecification("column1", |
| 107 | Bytes.toBytes("columnfamily1:column1"))); |
| 108 | desc.addIndex(new IndexSpecification("column2", |
| 109 | Bytes.toBytes("columnfamily1:column2"))); |
| 110 | |
| 111 | IndexedTableAdmin admin = null; |
| 112 | admin = new IndexedTableAdmin(conf); |
| 113 | |
| 114 | admin.createTable(desc); |
| 115 | |
| 116 | }}} |
| 117 | |
| 118 | == Adding index in an existing table == |
| 119 | {{{ |
| 120 | #!java |
| 121 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 122 | conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml")); |
| 123 | |
| 124 | IndexedTableAdmin admin = null; |
| 125 | admin = new IndexedTableAdmin(conf); |
| 126 | |
| 127 | admin.addIndex(Bytes.toBytes("test_table"), new IndexSpecification("column2", |
| 128 | Bytes.toBytes("columnfamily1:column2"))); |
| 129 | }}} |
| 130 | |
| 131 | == Deleting existing index from a table == |
| 132 | {{{ |
| 133 | #!java |
| 134 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 135 | conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml")); |
| 136 | |
| 137 | IndexedTableAdmin admin = null; |
| 138 | admin = new IndexedTableAdmin(conf); |
| 139 | |
| 140 | admin.removeIndex(Bytes.toBytes("test_table"), "column2"); |
| 141 | }}} |
| 142 | |
| 143 | == read from a secondary index, get a scanner for the index and scan through the data == |
| 144 | {{{ |
| 145 | #!java |
| 146 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 147 | conf.addResource(new Path("/opt/hbase-0.19.3/conf/hbase-site.xml")); |
| 148 | |
| 149 | IndexedTable table = new IndexedTable(conf, Bytes.toBytes("test_table")); |
| 150 | |
| 151 | // You need to specify which columns to get |
| 152 | Scanner scanner = table.getIndexedScanner("column1", |
| 153 | HConstants.EMPTY_START_ROW, null, null, new byte[][] { |
| 154 | Bytes.toBytes("columnfamily1:column1"), |
| 155 | Bytes.toBytes("columnfamily1:column2") }); |
| 156 | |
| 157 | for (RowResult rowResult : scanner) { |
| 158 | String value1 = new String( |
| 159 | rowResult.get(Bytes.toBytes("columnfamily1:column1")).getValue()); |
| 160 | String value2 = new String( |
| 161 | rowResult.get(Bytes.toBytes("columnfamily1:column2")).getValue()); |
| 162 | System.out.println(value1 + ", " + value2); |
| 163 | } |
| 164 | |
| 165 | table.close(); |
| 166 | }}} |
| 167 | |
| 168 | == get a scanner to a subset of the rows specify a column filter == |
| 169 | {{{ |
| 170 | #!java |
| 171 | ColumnValueFilter filter = |
| 172 | new ColumnValueFilter(Bytes.toBytes("columnfamily1:column1"), |
| 173 | CompareOp.LESS, Bytes.toBytes("value1-10")); |
| 174 | |
| 175 | scanner = table.getIndexedScanner("column1", HConstants.EMPTY_START_ROW, |
| 176 | null, filter, new byte[][] { Bytes.toBytes("columnfamily1:column1"), |
| 177 | Bytes.toBytes("columnfamily1:column2")); |
| 178 | |
| 179 | for (RowResult rowResult : scanner) { |
| 180 | String value1 = new String( |
| 181 | rowResult.get(Bytes.toBytes("columnfamily1:column1")).getValue()); |
| 182 | String value2 = new String( |
| 183 | rowResult.get(Bytes.toBytes("columnfamily1:column2")).getValue()); |
| 184 | System.out.println(value1 + ", " + value2); |
| 185 | } |
| 186 | }}} |
| 187 | |
| 188 | == Secondary indexes in HBase 完整程式碼 == |
| 189 | |