如何运行Hbase Java示例?

时间:2022-06-30 00:55:12

I am facing issue with run simple Hbase example.

我正面临运行简单Hbase示例的问题。

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.

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

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

美元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

但是我不能通过:$java -classpath hbase- 0.92 .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 ?

上面的命令对我不起作用。不知道是什么问题?运行Hbase Java示例的方式正确吗?

2 个解决方案

#1


16  

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

可以使用“hbase类路径”获取所需的类路径。

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

#2


1  

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

如果它不能运行,您可以(如果可能的话)使用类似NetBeans的IDE来开发Java HBase客户端API程序。

  • After opening NetBeans, Go to Tools=>Libraries and click on 'New Library' and give it a name like 'MapReduce'.
  • 打开NetBeans后,转到Tools=>库并单击“New Library”,并给它起一个类似“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.
  • 给它一个名称,然后单击Add JAR/文件夹并选择所需的JAR文件,通常在/usr/local/hadoop . 2.x下。适用于hadoop库和/usr/local/hbase-1.x。x/lib用于HBase库,导航文件系统浏览器(或导航到安装目录)。Shift+箭头键用于批量选择jar文件。
  • Now create a New Java Project as usual and right click it on the Project pane and go to 'Properties'.
  • 现在像往常一样创建一个新的Java项目,然后在项目面板上单击它,然后进入“属性”。
  • 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.
  • 现在单击Libraries=>Add Library并从列表中选择你的Library。您现在应该很好地编译并运行它们,因为您已经运行了所有进程。

#1


16  

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

可以使用“hbase类路径”获取所需的类路径。

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

#2


1  

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

如果它不能运行,您可以(如果可能的话)使用类似NetBeans的IDE来开发Java HBase客户端API程序。

  • After opening NetBeans, Go to Tools=>Libraries and click on 'New Library' and give it a name like 'MapReduce'.
  • 打开NetBeans后,转到Tools=>库并单击“New Library”,并给它起一个类似“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.
  • 给它一个名称,然后单击Add JAR/文件夹并选择所需的JAR文件,通常在/usr/local/hadoop . 2.x下。适用于hadoop库和/usr/local/hbase-1.x。x/lib用于HBase库,导航文件系统浏览器(或导航到安装目录)。Shift+箭头键用于批量选择jar文件。
  • Now create a New Java Project as usual and right click it on the Project pane and go to 'Properties'.
  • 现在像往常一样创建一个新的Java项目,然后在项目面板上单击它,然后进入“属性”。
  • 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.
  • 现在单击Libraries=>Add Library并从列表中选择你的Library。您现在应该很好地编译并运行它们,因为您已经运行了所有进程。