Skip to content
This repository

Welcome to the hbase-dsl wiki!

Over the next few weeks I hope to add more examples of how using this dsl can reduce and simplify your hbase code. But for now I will show a few simple examples.

Simple Put and Get Example

Direct HBase API:

public class PutAndGet {
  public static void main(String[] args) throws IOException {
    HTable hTable = new HTable(“test”);
    byte[] rowId = Bytes.toBytes(“abcd”);
    byte[] famA = Bytes.toBytes(“famA”);
    byte[] col1 = Bytes.toBytes(“col1”);
    Put put = new Put(rowId).
      add(famA, col1, Bytes.toBytes(“hello world!”));
    hTable.put(put);
    Get get = new Get(rowId);
    Result result = hTable.get(get);
    byte[] value = result.getValue(famA, col1);
    System.out.println(Bytes.toString(value));
  }
}

HBase-dsl API:

public class PutAndGetWithDsl {
  public static void main(String[] args) throws IOException {
    HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);
    hBase.save(“test”).
      row(“abcd”).
        family(“famA”).
          col(“col1”, “hello world!”);
    String value = hBase.fetch(“test”).
      row(“abcd”).
        family(“famA”).
          value(“col1”, String.class);
    System.out.println(value);
  }
}

Now this is where the dsl becomes more powerful.

Scanner Example

Direct HBase API:

public class Scanner {
  public static void main(String[] args) throws IOException {
    byte[] famA = Bytes.toBytes(“famA”);
    byte[] col1 = Bytes.toBytes(“col1”);  

    HTable hTable = new HTable(“test”);  

    Scan scan = new Scan(Bytes.toBytes(“a”), Bytes.toBytes(“z”));
    scan.addColumn(famA, col1);  

    SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
        famA, col1, CompareOp.EQUAL, Bytes.toBytes(“hello world!”));
    singleColumnValueFilterA.setFilterIfMissing(true);  

    SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter(
        famA, col1, CompareOp.EQUAL, Bytes.toBytes(“hello hbase!”));
    singleColumnValueFilterB.setFilterIfMissing(true);  

    FilterList filter = new FilterList(Operator.MUST_PASS_ONE, Arrays
        .asList((Filter) singleColumnValueFilterA,
            singleColumnValueFilterB));  

    scan.setFilter(filter);  

    ResultScanner scanner = hTable.getScanner(scan);  

    for (Result result : scanner) {
      System.out.println(Bytes.toString(result.getValue(famA, col1)));
    }
  }
}

HBase-dsl API:

public class ScannerWithDsl {
  public static void main(String[] args) throws IOException {
    HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);
    hBase.scan(“test”,“a”,“z”).
      select().
        family(“famA”).
          col(“col1”).
      where().
        family(“famA”).
          col(“col1”).eq(“hello world!”,“hello hbase!”).
      foreach(new ForEach<Row>() {
        @Override
        public void process(Row row) {
          System.out.println(row.value(“famA”, “col1”, String.class));
        }
      });
  }
}

For those that still want to use byte arrays.

Byte Array Example

public class UsingBytes {
  public static void main(String[] args) {
    HBase<QueryOps, String> hBase = new HBase<QueryOps, String>(String.class);
    byte[] rowId = Bytes.toBytes(“1234”);
    byte[] helloworld = Bytes.toBytes(“hello world!”);
    hBase.save(“test”).
      row(rowId).
        family(“famA”).
          col(“col1”, helloworld);  

    Iterable<Row<byte[]>> it = hBase.scan(“test”).
      where().
        family(“famA”).
          col(“col1”).eq(helloworld);  

    for (Row<byte[]> row : it) {
      byte[] value = row.value(“famA”, “col1”, byte[].class);
      System.out.println(Bytes.toString(value));
    }
  }
}

See the unit tests, for more examples.

Something went wrong with that request. Please try again.