Hbase Scan example
- scan table from "2012-05-09-09-35-192.168.0.0" to "2012-05-09-09-40-192.168.0.0" with row keys.
- Filter row key which has string "192.168.210.10"
source code
package org.hadoop.dna.analyzer;
import java.io.IOException;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.filter.*;
public class TrafficScan {
/**
* hbase table write example
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
Configuration config = HBaseConfiguration.create();
config.addResource(new Path("/usr/local/hbase/conf/hbase-default.xml"));
config.addResource(new Path("/usr/local/hbase/conf/hbase-site.xml"));
// Load Hbase table
HTable htable = new HTable(config, "Traffic");
// Scan
Scan s = new Scan();
s.setStartRow(Bytes.toBytes("2012-05-09-09-35-192.168.0.0"));
s.setStopRow(Bytes.toBytes("2012-05-09-09-40-192.168.0.0"));
RowFilter rf = new RowFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("192.168.210.10"));
s.setFilter(rf);
ResultScanner scanner = htable.getScanner(s);
for (Result rr = scanner.next(); rr != null; rr = scanner.next()) {
System.out.println("Found row: " + rr);
}
scanner.close();
} catch (IOException e) {
System.out.println("IOExeption: cannot insert table");
e.printStackTrace();
}
}
}
Compile
javac -classpath "/usr/local/hadoop/hadoop-core-1.0.1.jar":"/usr/local/hadoop/lib/*":"./class":"/usr/local/hbase/hbase-0.92.1.jar":"/usr/local/hbase/lib/zookeeper-3.4.3.jar" -d class TrafficScan.java
Packaging
jar -cvf hadoop-dna.jar -C class/ .
Execution
hadoop jar hadoop-dna.jar org.hadoop.dna.analyzer.TrafficScan