如何运行Hbase Java示例?

Ami*_*gar 9 hadoop hbase

我遇到运行简单的Hbase示例的问题.

我在HbaseTest.java上创建了一个表,并插入了一些记录.在Unix中,我可以编译java类.通过.

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

但我无法运行此程序:$ java -classpath hbase-0.94.2.jar:hadoop-core-1.0.4.jar HBaseTest

以上命令对我不起作用.不确定是什么问题?这是运行Hbase Java示例的正确方法吗?

小智 17

您可以使用"hbase classpath"来获取所需的类路径.

    /*
     * 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();
            }
        }
    }

  • 是.它的工作原理是1. Hbase/bin $./ hbase classpath 2.为classpath提供了非常庞大的字符串,其中包含运行程序所需的所有路径3. $ java%hbase字符串的类路径(步骤2)....现在它正在工作:) (2认同)