Changes between Version 2 and Version 3 of waue/2010/0128


Ignore:
Timestamp:
Jan 29, 2010, 5:34:14 PM (15 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2010/0128

    v2 v3  
    1414
    1515[http://mail-archives.apache.org/mod_mbox/hadoop-hbase-user/200908.mbox/%3C73d592f60908180339j3231f7f5w688260109ab3463@mail.gmail.com%3E]
     16
     17= what is secondary index (distinguished in primary index) =
     18
     19Explain the what is primary and secondary index.
     20
     21When you activate an object say ODS / DSO, the system automatically generate an index based on the key fields and this is primary index.
     22
     23In addition if you wish to create more indexes , then they are called secondary indexes.
     24
     25The primary index is distinguished from the secondary indexes of a table. The primary index contains the key fields of the table and a pointer to the non-key fields of the table. The primary index is created automatically when the table is created in the database.
     26
     27You can also create further indexes on a table. These are called secondary indexes. This is necessary if the table is frequently accessed in a way that does not take advantage of the sorting of the primary index for the access. Different indexes on the same table are distinguished with a three-place index identifier.
     28
     29 = hbase's secondary index detail =
    1630
    1731{{{
     
    5872}}}
    5973
    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
    66190[http://rajeev1982.blogspot.com/2009/06/secondary-indexes-in-hbase.html]
    67191
     
    226350}
    227351}}}
     352
     353 = Hadoop HBase數據導入和索引測試  =
     354[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]