R&D/클라우드

[Hbase] Hadoop-dna Get example

sunshout 2012. 5. 29. 18:57

HBase Get example


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.HTable;

import org.apache.hadoop.hbase.client.Result;

import org.apache.hadoop.hbase.util.Bytes;


public class TrafficGet {


    /**

     * 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");

            // Get

            // rowkey1  : B:<Dst IP> -> xx:xx bytes

            Get get = new Get(Bytes.toBytes("2012-05-09-09-35-192.168.210.20"));

            get.addColumn(Bytes.toBytes("B"), Bytes.toBytes("108.61.73.243"));

            Result result = htable.get(get);


            byte[] val = result.getValue(Bytes.toBytes("B"), Bytes.toBytes("108.61.73.243"));

            System.out.println("Value:" + Bytes.toString(val));



        } 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 TrafficGet.java


Packaging
jar -cvf hadoop-dna.jar -C class/ .


Execution
hadoop jar hadoop-dna.jar org.hadoop.dna.analyzer.TrafficGet