Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Hbase Java example?

Tags:

hadoop

hbase

I am facing issue with run simple Hbase example.

I have create on HbaseTest.java which create one table and insert some records. In Unix, I can able to compile the java class. by.

$javac -classpath hbase-0.94.2.jar:hadoop-core-1.0.4.jar HBaseTest.java

But I am not able to run this program by : $java -classpath hbase-0.94.2.jar:hadoop-core-1.0.4.jar HBaseTest

Above command is not working for me. Not sure what is the issue ? Is it correct way to run Hbase Java Example ?

like image 455
Amit Nagar Avatar asked Dec 05 '12 17:12

Amit Nagar


2 Answers

You can use "hbase classpath" to get the class path needed.

    /*
     * Compile and run with:
     * javac -cp `hbase classpath` TestHBase.java 
     * java -cp `hbase classpath` TestHBase
     */
    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 TestHBase {
        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();
            }
        }
    }
like image 95
th30z Avatar answered Oct 23 '22 11:10

th30z


If it fails to run you could, if possible, use an IDE like NetBeans to develop Java HBase Client API programs.

  • After opening NetBeans, Go to Tools=>Libraries and click on 'New Library' and give it a name like 'MapReduce'.
  • Give it a name and then click on Add JAR/Folder and select the required JAR files, typically under /usr/local/hadoop-2.x.x/share/hadoop for hadoop libraries and/usr/local/hbase-1.x.x/lib for HBase libraries, by navigating the file system browser(or just navigate to your installation directories). Shift+arrow keys work to mass select jar files.
  • Now create a New Java Project as usual and right click it on the Project pane and go to 'Properties'.
  • Now click on Libraries=>Add Library and select your library from the list shown. You should be good to compile and run them now, given you have all the processes running.
like image 1
Nikhil Vandanapu Avatar answered Oct 23 '22 11:10

Nikhil Vandanapu