Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run HBase program

Tags:

hadoop

hbase

How can I run the code below from the command line?

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.*;

public class MyHBase {
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        HBaseAdmin admin = new HBaseAdmin(conf);
        try {
            HTable table = new HTable(conf, "test-table");
            Put put = new Put(Bytes.toBytes("test-key"));
            put.add(Bytes.toBytes("cf"), Bytes.toBytes("q"), Bytes.toBytes("value"));
            table.put(put);
        } finally {
            admin.close();
        }
    }
}

How to set my hbase classpath? I am getting a huge string in my classpath.

UPDATE

root# vi MyHBase.java 
hbase-0.92.2 root# java -classpath `hbase classpath`:./ /var/root/MyHBase
-sh: hbase: command not found
Exception in thread "main" java.lang.NoClassDefFoundError: /var/root/MyHBase
Caused by: java.lang.ClassNotFoundException: .var.root.MyHBase
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

I am able to do

hbase-0.92.2 root# bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.92.2, r1379292, Fri Aug 31 13:13:53 UTC 2012

hbase(main):001:0> exit

Am i doing anything wrong?

like image 987
Unmesha Sreeveni U.B Avatar asked Apr 22 '13 10:04

Unmesha Sreeveni U.B


2 Answers

You can also do something like this:-

# export HADOOP_CLASSPATH=`./hbase classpath`

and then bundle this java in jar to run it within hadoop cluster like this:-

#hadoop jar <jarfile> <mainclass>
like image 72
techvineet Avatar answered Oct 24 '22 13:10

techvineet


Use this

java -classpath `hbase classpath`:./ MyHBase

this will get the entire classpath string that HBase uses and adds the current directory as well to the classpath.

Cheers Rags

like image 31
Rags Avatar answered Oct 24 '22 13:10

Rags