wiki:waue/2009/0626

Version 1 (modified by waue, 15 years ago) (diff)

--

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Scanner;
import org.apache.hadoop.hbase.io.BatchUpdate;
import org.apache.hadoop.hbase.io.Cell;
import org.apache.hadoop.hbase.io.RowResult;
import org.apache.hadoop.hbase.util.Bytes;

public class hbase {

  public static void main(String args[]) throws IOException {

    String table_name = "waue"; 
    String colomn_family = "family:";
    String column_quolify= "qf";
    String hbase_row = "w-row";
    String value = "0911311311";
    
    HBaseConfiguration config = new HBaseConfiguration();


    HBaseAdmin admin = new HBaseAdmin(config);
    if (!admin.tableExists(table_name)) {
      System.out.println("HTable : " + table_name
          + "  creating ... please wait");
      HTableDescriptor tableDesc = new HTableDescriptor(table_name);
      // add column family
      tableDesc.addFamily(new HColumnDescriptor(colomn_family));
      admin.createTable(tableDesc);
    }

    HTable table = new HTable(config, table_name);


    BatchUpdate batchUpdate = new BatchUpdate(hbase_row);


    batchUpdate.put(colomn_family+column_quolify, Bytes
        .toBytes(value));


    batchUpdate.delete(colomn_family+"cellIWantDeleted");


    table.commit(batchUpdate);


    Cell cell = table.get(hbase_row, colomn_family+column_quolify);
    System.out.print(cell.getValue());


    Scanner scanner =
    table.getScanner(new String[] { colomn_family+column_quolify });

    RowResult rowResult = scanner.next();

    while (rowResult != null) {

      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
      rowResult = scanner.next();
    }

    for (RowResult result : scanner) {
      System.out.println("Found row: "
          + Bytes.toString(rowResult.getRow())
          + " with value: "
          + rowResult.get(Bytes
              .toBytes(colomn_family+column_quolify)));
    }

    scanner.close();
  }
}