Skip to content

Commit

Permalink
Updated ch04 examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgeorge committed Mar 29, 2015
1 parent 09b2649 commit 20eb4f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
40 changes: 28 additions & 12 deletions ch04/src/main/java/client/IncrementMultipleExample.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package client;

// cc IncrementMultipleExample Example incrementing multiple counters in one row
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;

import java.io.IOException;
import util.HBaseHelper;

public class IncrementMultipleExample {

Expand All @@ -26,23 +29,35 @@ public static void main(String[] args) throws IOException {

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

// vv IncrementMultipleExample
Increment increment1 = new Increment(Bytes.toBytes("20110101"));
Increment increment1 = new Increment(Bytes.toBytes("20150101"));

increment1.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("clicks"), 1);
increment1.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); // co IncrementMultipleExample-1-Incr1 Increment the counters with various values.
increment1.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("clicks"), 10);
increment1.addColumn(Bytes.toBytes("weekly"), Bytes.toBytes("hits"), 10);
// vv IncrementMultipleExample
Map<byte[], NavigableMap<byte[], Long>> longs =
increment1.getFamilyMapOfLongs();
for (byte[] family : longs.keySet()) {
System.out.println("Increment #1 - family: " + Bytes.toString(family));
NavigableMap<byte[], Long> longcols = longs.get(family);
for (byte[] column : longcols.keySet()) {
System.out.print(" column: " + Bytes.toString(column));
System.out.println(" - value: " + longcols.get(column));
}
}
// ^^ IncrementMultipleExample

Result result1 = table.increment(increment1); // co IncrementMultipleExample-2-Incr2 Call the actual increment method with the above counter updates and receive the results.

for (KeyValue kv : result1.raw()) {
System.out.println("KV: " + kv +
" Value: " + Bytes.toLong(kv.getValue())); // co IncrementMultipleExample-3-Dump1 Print the KeyValue and returned counter value.
for (Cell cell : result1.rawCells()) {
System.out.println("Cell: " + cell +
" Value: " + Bytes.toLong(cell.getValueArray(), cell.getValueOffset(),
cell.getValueLength())); // co IncrementMultipleExample-3-Dump1 Print the cell and returned counter value.
}

Increment increment2 = new Increment(Bytes.toBytes("20110101"));
Increment increment2 = new Increment(Bytes.toBytes("20150101"));

increment2.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("clicks"), 5);
increment2.addColumn(Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1); // co IncrementMultipleExample-4-Incr3 Use positive, negative, and zero increment values to achieve the wanted counter changes.
Expand All @@ -51,9 +66,10 @@ public static void main(String[] args) throws IOException {

Result result2 = table.increment(increment2);

for (KeyValue kv : result2.raw()) {
System.out.println("KV: " + kv +
" Value: " + Bytes.toLong(kv.getValue()));
for (Cell cell : result2.rawCells()) {
System.out.println("Cell: " + cell +
" Value: " + Bytes.toLong(cell.getValueArray(),
cell.getValueOffset(), cell.getValueLength()));
}
// ^^ IncrementMultipleExample
table.close();
Expand Down
8 changes: 4 additions & 4 deletions ch04/src/main/java/client/IncrementSingleExample.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package client;

// cc IncrementSingleExample Example using the single counter increment methods
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import util.HBaseHelper;

import java.io.IOException;
import util.HBaseHelper;

public class IncrementSingleExample {

Expand All @@ -20,10 +21,9 @@ public static void main(String[] args) throws IOException {
HBaseHelper helper = HBaseHelper.getHelper(conf);
helper.dropTable("testtable");
helper.createTable("testtable", "daily");
// vv IncrementSingleExample
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("testtable"));

// vv IncrementSingleExample
long cnt1 = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-1-Incr1 Increase counter by one.
Bytes.toBytes("daily"), Bytes.toBytes("hits"), 1);
long cnt2 = table.incrementColumnValue(Bytes.toBytes("20110101"), // co IncrementSingleExample-2-Incr2 Increase counter by one a second time.
Expand Down

0 comments on commit 20eb4f4

Please sign in to comment.