Simple HBase stress tester

What is below is a simple class to read a random set of records from a table. This should be a good start if you are looking for a simple one class test case, that may also be useful for simple stress testing.

As other example regarding HBase, it assumes you have configured your hbase-site.xml with the location of your HBase database.

import java.util.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;

class hbaseStresser implements Runnable {
  Thread t;

  public static void main(String args[]) {
    try {

      System.out.println(new java.util.Date());
      for(int i=1; i <= Integer.parseInt(args[0]); i++){  //pass in the number of threads you want to run
        hbaseStresser r = new hbaseStresser();
      }
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  hbaseStresser() {
    try {
      t = new Thread(this);
      t.start();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void run() {
    try {
      HTable hTable = new HTable("t1");  //this is your table name

      byte[] rowId = null;
      byte[] famA = Bytes.toBytes("data");  //this is your column family
      byte[] col1 = Bytes.toBytes("xml"); //this is your on the fly column name in the table

      Random rId = new Random();
      for (int j = 1; j < 1000; j++) {  //each thread loops 1000 times
        int id = rId.nextInt(100000000); //and randomly selects a record with an id between 1 and 100 million
        rowId = Bytes.toBytes(Integer.toString(id));
        Get get = new Get(rowId);
        Result result = hTable.get(get);
        byte[] value = result.getValue(famA, col1);
        System.out.println(id);
        System.out.println(Bytes.toString(value));
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.