5

如何从命令行运行下面的代码?

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

如何设置我的 hbase 类路径?我的类路径中有一个巨大的字符串。

更新

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)

我能做到

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

我做错什么了吗?

4

2 回答 2

9

你也可以这样做: -

# export HADOOP_CLASSPATH=`./hbase classpath`

然后将这个java捆绑在jar中以在hadoop集群中运行它,如下所示: -

#hadoop jar <jarfile> <mainclass>
于 2013-08-20T13:41:23.980 回答
5

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

于 2013-04-22T15:39:47.760 回答