public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create();

    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable");
    helper.createTable("testtable", "colfam1");

    Connection connection = ConnectionFactory.createConnection(conf);
    Table table = connection.getTable(TableName.valueOf("testtable"));

    List<Put> puts = new ArrayList<Put>();

    // vv PutListErrorExample2
    Put put1 = new Put(Bytes.toBytes("row1"));
    put1.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual1"), Bytes.toBytes("val1"));
    puts.add(put1);
    Put put2 = new Put(Bytes.toBytes("row2"));
    put2.addColumn(Bytes.toBytes("BOGUS"), Bytes.toBytes("qual1"), Bytes.toBytes("val2"));
    puts.add(put2);
    Put put3 = new Put(Bytes.toBytes("row2"));
    put3.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("qual2"), Bytes.toBytes("val3"));
    puts.add(put3);
    /*[*/ Put put4 = new Put(Bytes.toBytes("row2"));
    puts.add(
        put4); /*]*/ // co PutListErrorExample2-1-AddErrorPut Add put with no content at all to
                     // list.

    /*[*/ try {
        /*]*/
      table.put(puts);
      /*[*/ } catch (Exception e) {
      System.err.println("Error: " + e);
      // table.flushCommits();
      // todo: FIX!
      /*]*/
      // co PutListErrorExample2-2-Catch Catch local exception and commit queued updates.
      /*[*/ } /*]*/
    // ^^ PutListErrorExample2
    table.close();
    connection.close();
    helper.close();
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws IOException {
    Configuration conf = HBaseConfiguration.create();

    conf.set(
        "hbase.zookeeper.quorum",
        "master-1.internal.larsgeorge.com,"
            + "master-2.internal.larsgeorge.com,master-3.internal.larsgeorge.com");

    HBaseHelper helper = HBaseHelper.getHelper(conf);
    helper.dropTable("testtable");
    helper.createTable("testtable", "colfam1");
    System.out.println("Adding rows to table...");
    helper.fillTable("testtable", 1, 5, 1, "colfam1");

    System.out.println("Table before the operations:");
    helper.dump("testtable");

    Connection connection = ConnectionFactory.createConnection(conf);
    TableName tableName = TableName.valueOf("testtable");
    Table table = connection.getTable(tableName);

    // vv ScanConsistencyExample1
    Scan scan = new Scan();
    scan.setCaching(
        1); // co ScanConsistencyExample1-1-ConfScan Configure scan to iterate over each row
            // separately.
    ResultScanner scanner = table.getScanner(scan);

    // ^^ ScanConsistencyExample1
    System.out.println("Starting scan, reading one row...");
    // vv ScanConsistencyExample1
    Result result = scanner.next();
    helper.dumpResult(result);

    // ^^ ScanConsistencyExample1
    System.out.println("Applying mutations...");
    // vv ScanConsistencyExample1
    Put put = new Put(Bytes.toBytes("row-3"));
    put.addColumn(Bytes.toBytes("colfam1"), Bytes.toBytes("col-1"), Bytes.toBytes("val-999"));
    table.put(put); // co ScanConsistencyExample1-2-Put Update a later row with a new value.

    Delete delete = new Delete(Bytes.toBytes("row-4"));
    table.delete(
        delete); // co ScanConsistencyExample1-3-Delete Remove an entire row, that is located at the
                 // end of the scan.

    // ^^ ScanConsistencyExample1
    System.out.println("Resuming original scan...");
    // vv ScanConsistencyExample1
    for (Result result2 : scanner) {
      helper.dumpResult(
          result2); // co ScanConsistencyExample1-4-Scan Scan the rest of the table to see if the
                    // mutations are visible.
    }
    scanner.close();

    // ^^ ScanConsistencyExample1
    System.out.println("Print table under new scanner...");
    // vv ScanConsistencyExample1
    helper.dump(
        "testtable"); // co ScanConsistencyExample1-5-Dump Print the entire table again, with a new
                      // scanner instance.
    // ^^ ScanConsistencyExample1
    table.close();
    connection.close();
    helper.close();
  }