How to run an Hbase Java example?

I ran into a problem while running a simple Hbase example.

I create on HbaseTest.java that create one table and insert some records. On Unix, I can compile the java class. by way.

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

But I can not run this program: $ java -classpath hbase-0.94.2.jar: hasoop-core-1.0.4.jar HBaseTest

The above command does not work for me. Not sure what the problem is? Is the right option to run the Hbase Java example?

+8
hbase hadoop
source share
2 answers

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

     / *
      * 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 ();
             }
         }
     }
+16
source share

If it does not start, you can, if possible, use an IDE, such as NetBeans, to develop Java HBase API API programs.

  • After opening NetBeans, go to Tools> Libraries and click Create. Library 'and give it the name "MapReduce".
  • Give it a name, then click "Add JAR / Folder" and select the required JAR files, usually under /usr/local/hadoop-2.xx/share/hadoop for the hadoop and /usr/local/hbase-1.xx/lib libraries for HBase libraries, by navigating the file system browser (or simply navigating to the installation directories). Shift + arrow keys work for bulk selection of jar files.
  • Now create a new Java project, as usual, and right-click on it the Project Panel and go to Properties.
  • Now click "Libraries => Add Library" and select your library from the list. You should compile and run them well now, given that all processes are running.
+1
source share

All Articles