USB
USB

Reputation: 6139

How to run HBase program

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?

Upvotes: 5

Views: 11239

Answers (2)

techvineet
techvineet

Reputation: 5111

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>

Upvotes: 9

Rags
Rags

Reputation: 1881

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

Upvotes: 5

Related Questions