今天写了一段简单的java代码调用Hbase的API,代码如下:
public class HbaseApi { private HTable hTable ; private String tableName; public HbaseApi(String tableName) throws IOException { this.tableName = tableName; HBaseConfiguration hconfig = new HBaseConfiguration(); hconfig.set("hbase.zookeeper.property.clientPort","2181"); hconfig.set("hbase.zookeeper.quorum", "192.168.179.130"); hTable = new HTable(hconfig, this.tableName.getBytes()); } public void scan(String family, String qualifier) throws IOException { Scan scan = new Scan(); scan.addColumn(family.getBytes(), qualifier.getBytes()); ResultScanner scanner = hTable.getScanner(scan); for(Result result = scanner.next(); result != null; result = scanner.next() ) { } } public static void main(String[] args) { try { new HbaseApi("teacher").scan("teacher", "info");; } catch (IOException e) { e.printStackTrace(); } } }
执行的时候出现错误:从错误上看,应该是Hbase的一个类重载了protobuf-java的一个final方法导致的。我使用hadoop的版本是2.2.0, Hbase版本是0.96,protobuf的版本是2.5.如有大神遇到相同的问题,请提供以下解决方法,再此谢过
java.io.IOException: java.lang.reflect.InvocationTargetException at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:389) at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:366) at org.apache.hadoop.hbase.client.HConnectionManager.getConnection(HConnectionManager.java:247) at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:188) at org.apache.hadoop.hbase.client.HTable.<init>(HTable.java:165) at com.hadoop.study.hbase.HbaseApi.<init>(HbaseApi.java:19) at com.hadoop.study.hbase.HbaseApi.main(HbaseApi.java:34) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:387) ... 6 more Caused by: java.lang.VerifyError: class org.apache.hadoop.hbase.protobuf.generated.ClientProtos$Result overrides final method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet; at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:621) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at java.net.URLClassLoader.defineClass(URLClassLoader.java:260) at java.net.URLClassLoader.access$000(URLClassLoader.java:56) at java.net.URLClassLoader$1.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at org.apache.hadoop.hbase.protobuf.ProtobufUtil.<clinit>(ProtobufUtil.java:172) at org.apache.hadoop.hbase.ClusterId.parseFrom(ClusterId.java:64) at org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode(ZKClusterId.java:69) at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId(ZooKeeperRegistry.java:83) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.retrieveClusterId(HConnectionManager.java:801) at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.<init>(HConnectionManager.java:633) ... 11 more