You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by y_...@tsmc.com on 2009/12/09 11:15:09 UTC

IndexedTableTest sample

Hi,

createTableWithSecondaryIndexes()
writeToTable()

After running above two functions, I can see two tablescreated in HBase
(test_table  and test_table-column1).
There are 100 rows data in test_table , but I can't see any data in
test_table-column1.
I want to know whether I should configure something to make secondary index
work.
Thanks.

public void writeToTable() throws IOException {
        HBaseConfiguration conf = new HBaseConfiguration();
        conf.addResource(new Path(hbase_res));

        IndexedTable table = new IndexedTable(conf,
Bytes.toBytes("test_table"));

        String row = "test_row";
        Put row_update = null;

      byte[] column = Bytes.toBytes("columnfamily1");
      byte[][] qualifier = new byte[][] {
          Bytes.toBytes("column1"),
          Bytes.toBytes("column2"),
          Bytes.toBytes("column3"),
      };

        for (int i = 0; i < 100; i++) {
            row_update = new Put(Bytes.toBytes(row + i));
            row_update.add(column, qualifier[0], Bytes.toBytes("ray1-" +
(i/4)));
            row_update.add(column, qualifier[1], Bytes.toBytes("ray2-" +
i));
            row_update.add(column, qualifier[2], Bytes.toBytes("ray3-" +
i));
            table.put(row_update);
        }

        table.close();
    }

    public void createTableWithSecondaryIndexes() throws IOException {
        HBaseConfiguration conf = new HBaseConfiguration();
        conf.addResource(new Path(hbase_res));

        HTableDescriptor desc = new HTableDescriptor("test_table");
        desc.addFamily(new HColumnDescriptor("columnfamily1"));


        IndexedTableDescriptor idx_desc = new IndexedTableDescriptor(desc);
        idx_desc.addIndex(new IndexSpecification("column1",
                          Bytes.toBytes("columnfamily1:column1")));

        IndexedTableAdmin admin = null;
        admin = new IndexedTableAdmin(conf);

        if (admin.tableExists(Bytes.toBytes("test_table"))) {
            if (admin.isTableEnabled("test_table")) {
                admin.disableTable(Bytes.toBytes("test_table"));
            }

            admin.deleteTable(Bytes.toBytes("test_table"));
        }

        if (admin.tableExists(Bytes.toBytes("test_table-column1"))) {
            if (admin.isTableEnabled("test_table-column1")) {
                admin.disableTable(Bytes.toBytes("test_table-column1"));
            }

            admin.deleteTable(Bytes.toBytes("test_table-column1"));
        }

        admin.createIndexedTable(idx_desc);
    }




Fleming Chiu(邱宏明)
707-6128
y_823910@tsmc.com
週一無肉日吃素救地球(Meat Free Monday Taiwan)


 --------------------------------------------------------------------------- 
                                                         TSMC PROPERTY       
 This email communication (and any attachments) is proprietary information   
 for the sole use of its                                                     
 intended recipient. Any unauthorized review, use or distribution by anyone  
 other than the intended                                                     
 recipient is strictly prohibited.  If you are not the intended recipient,   
 please notify the sender by                                                 
 replying to this email, and then delete this email and any copies of it     
 immediately. Thank you.                                                     
 --------------------------------------------------------------------------- 




Re: IndexedTableTest sample

Posted by Clint Morgan <cl...@troove.net>.
A bit of config is needed. Look at the package javadoc in
o.a.h.h.client.tableindexed. Have a look at TestTableIndexed for client
usage.

The following test made from your code works for me:

package org.apache.hadoop.hbase.client.tableindexed;

import java.io.IOException;

import junit.framework.Assert;

import org.apache.hadoop.hbase.HBaseClusterTestCase;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import
org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegionServer;
import org.apache.hadoop.hbase.util.Bytes;

public class TestIndexedTable extends HBaseClusterTestCase {

    /** constructor */
  public TestIndexedTable() {
    conf
        .set(HConstants.REGION_SERVER_IMPL,
IndexedRegionServer.class.getName());
    conf.setInt("hbase.master.info.port", -1);
    conf.setInt("hbase.regionserver.info.port", -1);
  }

  public void testIt() throws IOException {
    createTableWithSecondaryIndexes();
    writeToTable();
    checkIt();
  }

  private void checkIt() throws IOException {
    int numRowsInTheTable = 0;
    HTable table = new HTable(conf,
        Bytes.toBytes("test_table"));
    for(Result r : table.getScanner(Bytes.toBytes("columnfamily1"))) {
      numRowsInTheTable++;
    }
    Assert.assertEquals(100, numRowsInTheTable);

    int numRowsInTheIndex = 0;
     table = new IndexedTable(conf,
        Bytes.toBytes("test_table-column1"));
    for(Result r : table.getScanner(Bytes.toBytes("columnfamily1"))) {
      numRowsInTheIndex++;
    }
    Assert.assertEquals(100, numRowsInTheIndex);
  }

  public void writeToTable() throws IOException {

    IndexedTable table = new IndexedTable(conf,
Bytes.toBytes("test_table"));

    String row = "test_row";
    Put row_update = null;

  byte[] column = Bytes.toBytes("columnfamily1");
  byte[][] qualifier = new byte[][] {
      Bytes.toBytes("column1"),
      Bytes.toBytes("column2"),
      Bytes.toBytes("column3"),
  };

    for (int i = 0; i < 100; i++) {
        row_update = new Put(Bytes.toBytes(row + i));
        row_update.add(column, qualifier[0], Bytes.toBytes("ray1-" +
(i/4)));
        row_update.add(column, qualifier[1], Bytes.toBytes("ray2-" +
i));
        row_update.add(column, qualifier[2], Bytes.toBytes("ray3-" +
i));
        table.put(row_update);
    }

    table.close();
}

public void createTableWithSecondaryIndexes() throws IOException {

    HTableDescriptor desc = new HTableDescriptor("test_table");
    desc.addFamily(new HColumnDescriptor("columnfamily1"));


    IndexedTableDescriptor idx_desc = new IndexedTableDescriptor(desc);
    idx_desc.addIndex(new IndexSpecification("column1",
                      Bytes.toBytes("columnfamily1:column1")));

    IndexedTableAdmin admin = null;
    admin = new IndexedTableAdmin(conf);

    if (admin.tableExists(Bytes.toBytes("test_table"))) {
        if (admin.isTableEnabled("test_table")) {
            admin.disableTable(Bytes.toBytes("test_table"));
        }

        admin.deleteTable(Bytes.toBytes("test_table"));
    }

    if (admin.tableExists(Bytes.toBytes("test_table-column1"))) {
        if (admin.isTableEnabled("test_table-column1")) {
            admin.disableTable(Bytes.toBytes("test_table-column1"));
        }

        admin.deleteTable(Bytes.toBytes("test_table-column1"));
    }

    admin.createIndexedTable(idx_desc);
}


}

2009/12/9 <y_...@tsmc.com>

> Hi,
>
> createTableWithSecondaryIndexes()
> writeToTable()
>
> After running above two functions, I can see two tablescreated in HBase
> (test_table  and test_table-column1).
> There are 100 rows data in test_table , but I can't see any data in
> test_table-column1.
> I want to know whether I should configure something to make secondary index
> work.
> Thanks.
>
> public void writeToTable() throws IOException {
>        HBaseConfiguration conf = new HBaseConfiguration();
>        conf.addResource(new Path(hbase_res));
>
>        IndexedTable table = new IndexedTable(conf,
> Bytes.toBytes("test_table"));
>
>        String row = "test_row";
>        Put row_update = null;
>
>      byte[] column = Bytes.toBytes("columnfamily1");
>      byte[][] qualifier = new byte[][] {
>          Bytes.toBytes("column1"),
>          Bytes.toBytes("column2"),
>          Bytes.toBytes("column3"),
>      };
>
>        for (int i = 0; i < 100; i++) {
>            row_update = new Put(Bytes.toBytes(row + i));
>            row_update.add(column, qualifier[0], Bytes.toBytes("ray1-" +
> (i/4)));
>            row_update.add(column, qualifier[1], Bytes.toBytes("ray2-" +
> i));
>            row_update.add(column, qualifier[2], Bytes.toBytes("ray3-" +
> i));
>            table.put(row_update);
>        }
>
>        table.close();
>    }
>
>    public void createTableWithSecondaryIndexes() throws IOException {
>        HBaseConfiguration conf = new HBaseConfiguration();
>        conf.addResource(new Path(hbase_res));
>
>        HTableDescriptor desc = new HTableDescriptor("test_table");
>        desc.addFamily(new HColumnDescriptor("columnfamily1"));
>
>
>        IndexedTableDescriptor idx_desc = new IndexedTableDescriptor(desc);
>        idx_desc.addIndex(new IndexSpecification("column1",
>                          Bytes.toBytes("columnfamily1:column1")));
>
>        IndexedTableAdmin admin = null;
>        admin = new IndexedTableAdmin(conf);
>
>        if (admin.tableExists(Bytes.toBytes("test_table"))) {
>            if (admin.isTableEnabled("test_table")) {
>                admin.disableTable(Bytes.toBytes("test_table"));
>            }
>
>            admin.deleteTable(Bytes.toBytes("test_table"));
>        }
>
>        if (admin.tableExists(Bytes.toBytes("test_table-column1"))) {
>            if (admin.isTableEnabled("test_table-column1")) {
>                admin.disableTable(Bytes.toBytes("test_table-column1"));
>            }
>
>            admin.deleteTable(Bytes.toBytes("test_table-column1"));
>        }
>
>        admin.createIndexedTable(idx_desc);
>    }
>
>
>
>
> Fleming Chiu(邱宏明)
> 707-6128
> y_823910@tsmc.com
> 週一無肉日吃素救地球(Meat Free Monday Taiwan)
>
>
>
>  ---------------------------------------------------------------------------
>                                                         TSMC PROPERTY
>  This email communication (and any attachments) is proprietary information
>  for the sole use of its
>  intended recipient. Any unauthorized review, use or distribution by anyone
>  other than the intended
>  recipient is strictly prohibited.  If you are not the intended recipient,
>  please notify the sender by
>  replying to this email, and then delete this email and any copies of it
>  immediately. Thank you.
>
>  ---------------------------------------------------------------------------
>
>
>
>