Problems encountered by HBase's Java API and how to resolve them

Source: Internet
Author: User
Tags zookeeper

Brief introduction

With the HBase version updated, there are some dependencies that will follow the update, today encountering a dependency on the order of the package reference! The source code below, in the Windows side directly run the error, but in the way of the jar package in the cluster can be run! The error prompt will also be pasted below.

Operating Source:
 PackageCom.iie.Hbase_demo;Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.hbase.HBaseConfiguration;ImportOrg.apache.hadoop.hbase.HColumnDescriptor;ImportOrg.apache.hadoop.hbase.HTableDescriptor;ImportOrg.apache.hadoop.hbase.TableName;Importorg.apache.hadoop.hbase.client.*;Importorg.apache.hadoop.hbase.util.Bytes;Importjava.io.IOException;ImportJava.sql.DriverManager;Import Staticorg.apache.hadoop.hbase.client.ConnectionFactory.createConnection; Public classHbaseput { Public StaticConfiguration config =NULL; Static{config=hbaseconfiguration.create (); Config.set ("Hbase.zookeeper.quorum", "10.199.33.12:2181"); System.out.println ("Successfully connected to ZK"); }    /*** CREATE TABLE * *@paramtableName Table name *@paramFamily Column Family*/     Public Static voidcreatetable (String tableName, string[] family) {htabledescriptor table=NewHtabledescriptor (tablename.valueof (TableName)); Try(Connection Connection =connectionfactory.createconnection (config)) {            Try(Admin admin =connection.getadmin ()) {                 for(inti = 0; i < family.length; i++) {table.addfamily (NewHcolumndescriptor (Family[i])); }                if(Admin.tableexists (tablename.valueof (TableName))) {System.out.println ("Table exists!!"); System.exit (0); } Else{admin.createtable (table); System.out.println (The Create Table Success!!! Table Name: ["+ TableName +"] "); }            }        } Catch(IOException e) {e.printstacktrace (); }    }    //connection to the database     Public StaticConnection getconnection ()throwsexception{String URL= "Jdbc:mysql://10.199.33.13:3306/test"; String User= "Root"; String Password= "111111"; Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn=(Connection) drivermanager.getconnection (URL, user, password); returnConn; }    /*** Add Data * *@paramRowKey RowKey *@paramtableName Table name *@paramColumn Name *@paramValue Values*/     Public Static voidaddData (String RowKey, String tableName, string[] column, string[] value) {Try(Connection Connection =createconnection (config); Table Table=connection.gettable (tablename.valueof (TableName))) {Put put=NewPut (Bytes.tobytes (RowKey));//converted to a byte array when stored in HBasehcolumndescriptor[] Columnfamilies =table.gettabledescriptor (). Getcolumnfamilies ();  for(inti = 0; i < columnfamilies.length; i++) {String familyname=columnfamilies[i].getnameasstring (); if(Familyname.equals ("UserInfo")) {                     for(intj = 0; J < Column.length; J + +) {Put.addcolumn (Bytes.tobytes (familyname), Bytes.tobytes (Column[j]), Bytes.tobytes (Value[j]))                    ;                }} table.put (Put); System.out.println ("ADD Data Success!!! -"); }        } Catch(IOException e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args)throwsIOException {string[] family= {"UserInfo"}; Hbaseput.createtable ("ZXF", family); string[] Column= {"Name", "Age", "email", "phone"}; String[] Value={"Zengxuefeng", "$", "[email protected]", "18463101815"}; Hbaseput.adddata ("User", "ZXF", Column,value); }}
Error message:
successfully connected ZKjava.io.IOException:java.lang.reflect.InvocationTargetException at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:240) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:218) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:119) at Com.iie.Hbase_demo. Hbaseput.createtable (Hbaseput.java:35) at Com.iie.Hbase_demo. Hbaseput.main (Hbaseput.java:94) caused by:java.lang.reflect.InvocationTargetException at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (Nativeconstructoraccessorimpl.java:62) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Delegatingconstructoraccessorimpl.java:45) at Java.lang.reflect.Constructor.newInstance (Constructor.java:422) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:238)    ... 4morecaused By:java.lang.VerifyError:classOrg.apache.hadoop.hbase.protobuf.generated.clientprotos$result OverridesFinalMethod Getunknownfields. () lcom/google/protobuf/Unknownfieldset; At Java.lang.ClassLoader.defineClass1 (Native Method) at Java.lang.ClassLoader.defineClass (Classloader.java:760) at Java.security.SecureClassLoader.defineClass (Secureclassloader.java:142) at Java.net.URLClassLoader.defineClass (URLClassLoader.java:467) at java.net.urlclassloader.access$(urlclassloader.java:73) at java.net.urlclassloader$1.run (urlclassloader.java:368) at java.net.urlclassloader$1.run (urlclassloader.java:362) at java.security.AccessController.doPrivileged (Native Method) at Java.net.URLClassLoader.findClass (Urlclasslo Ader.java:361) at Java.lang.ClassLoader.loadClass (Classloader.java:424) at Sun.misc.launcher$appclassloader.loadclass (Launcher.java:331) at Java.lang.ClassLoader.loadClass (Classloader.java:357) at Org.apache.hadoop.hbase.protobuf.ProtobufUtil.<clinit> (protobufutil.java:210) at Org.apache.hadoop.hbase.ClusterId.parseFrom (Clusterid.java:64) at Org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode (Zkclusterid.java:75) at Org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId (Zookeeperregistry.java:105) at Org.apache.hadoop.hbase.client.connectionmanager$hconnectionimplementation.retrieveclusterid ( Connectionmanager.java:919) at org.apache.hadoop.hbase.client.connectionmanager$hconnectionimplementation.<init> (connectionmanager.java:657)    ... 9Morejava.io.IOException:java.lang.reflect.InvocationTargetException at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:240) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:218) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:119) at Com.iie.Hbase_demo. Hbaseput.adddata (Hbaseput.java:73) at Com.iie.Hbase_demo. Hbaseput.main (Hbaseput.java:97) caused by:java.lang.reflect.InvocationTargetException at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (Nativeconstructoraccessorimpl.java:62) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Delegatingconstructoraccessorimpl.java:45) at Java.lang.reflect.Constructor.newInstance (Constructor.java:422) at Org.apache.hadoop.hbase.client.ConnectionFactory.createConnection (Connectionfactory.java:238)    ... 4morecaused by:java.lang.NoClassDefFoundError:Could not initializeclassOrg.apache.hadoop.hbase.protobuf.ProtobufUtil at Org.apache.hadoop.hbase.ClusterId.parseFrom (Clusterid.java: 64) at Org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode (Zkclusterid.java:75) at Org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId (Zookeeperregistry.java:105) at Org.apache.hadoop.hbase.client.connectionmanager$hconnectionimplementation.retrieveclusterid ( Connectionmanager.java:919) at org.apache.hadoop.hbase.client.connectionmanager$hconnectionimplementation.<init> (connectionmanager.java:657)    ... 9Moreprocess finished with exit code0
Workaround:

The cause of this error is: PROTOBUF This JAR package reference order problem, he referred to hive, after the final definition of hive no longer allow hbase reference caused, this error has been looking for a long time!

Change the order of the above two references in the Pom file. As shown below:

And then run the program is the perfect solution to this problem!!!

Hope to be of help to everyone.

Problems encountered by HBase's Java API and how to resolve them

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.