Relive Hbaes deployment with Java Client access

Source: Internet
Author: User

For a long time did not put up the test, today to put together to use. The result is a lot of problems, the following one by one shows


First, the deployment

For simplicity's sake, I used the stand-alone mode. Download the extract directly, note the need to change the hbase-site.xml:

<configuration>        <property>                <name>hbase.rootdir</name>                <value>file:// /root/zhangtieying/hbase</value>        </property>        <property>                <name> hbase.zookeeper.property.datadir</name>                <value>/root/zhangtieying/zookeeper</value>        </property></configuration>

Also, pay attention to modifying/etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain410.1.102.104 ccf0410.20.14.16 dt1610.20.14.17 dt1710.20.14.18 dt1810.20.14.19 dt1910.20.14.20 dt2010.20.14.21 dt2110.20.14.22 dt2210.20.14.23 dt2310.20.14.24 dt2410.20.14.25 Dt25

Must NOTE: ccf04 is the hostname, and the actual ip10.1.102.104 corresponds to the

ccf04 Add as host name: Vim/etc/sysconfig/network:

Networking=yeshostname=ccf04


Second, the Java Client connection

Here is the code for my test (part of the code is copy)

/** * Created by Michael on 2015/6/20. */import Java.util.arraylist;import Java.util.list;import Org.apache.hadoop.conf.configuration;import Org.apache.hadoop.hbase.hbaseconfiguration;import Org.apache.hadoop.hbase.hcolumndescriptor;import Org.apache.hadoop.hbase.htabledescriptor;import Org.apache.hadoop.hbase.keyvalue;import Org.apache.hadoop.hbase.client.delete;import Org.apache.hadoop.hbase.client.get;import Org.apache.hadoop.hbase.client.hbaseadmin;import Org.apache.hadoop.hbase.client.htable;import Org.apache.hadoop.hbase.client.put;import Org.apache.hadoop.hbase.client.result;import Org.apache.hadoop.hbase.client.resultscanner;import Org.apache.hadoop.hbase.client.scan;import    Org.apache.hadoop.hbase.util.bytes;public class Testhbase {static Configuration conf=null; static{conf=hbaseconfiguration.create ();//hbase configuration information Conf.set ("Hbase.zookeeper.quorum", "10.1.102.104:2181"  );      Zookeeper address} public static void Main (string[] args) throws Exception {  Testhbase t=new testhbase ();        T.createtable ("Test-zty", New string[]{"name", "Age"});        T.insertrow ("Test-zty", "2", "Age", "myage", "100");        T.getonedatabyrowkey ("Test-zty", "2");    T.showall ("test");        }/*** * Create a table * and specify the column cluster * */public void createtable (String tablename,string cols[]) throws exception{ Hbaseadmin admin=new hbaseadmin (conf);//Client Management tool class if (Admin.tableexists (TableName)) {System.out.printl        N ("This table already exists ....");            }else{htabledescriptor table=new htabledescriptor (tableName); for (String c:cols) {hcolumndescriptor col=new hcolumndescriptor (c);//column Cluster name table.addfamily (col            );//Add to this table} admin.createtable (table);//Create a table admin.close ();        SYSTEM.OUT.PRINTLN ("CREATE TABLE succeeded!");  }}/** * Add data, * We recommend using bulk Add * @param tableName table name * @param row Row number * @param columnfamily column cluster *     @param column Columns* @param Value Specific values * * **/public void InsertRow (string tableName, String row, St        Ring columnfamily, String column, String value) throws Exception {htable table = new htable (conf, tableName);        Put put = new put (bytes.tobytes (row));        Parameters are: Column family, column, Value Put.add (Bytes.tobytes (columnfamily), bytes.tobytes (column), bytes.tobytes (value));        Table.put (Put);    Table.close ();//Close System.out.println ("Insert a data successfully!"); }/** * Delete a data * @param tableName table name * @param row Rowkey * **/public void deletebyrow (String table        Name,string Rowkey) throws exception{htable h=new htable (conf, tableName);        Delete d=new Delete (Bytes.tobytes (Rowkey));    H.delete (d);//delete a data h.close (); }/** * Delete multiple data * @param tableName table name * @param row Rowkey * **/public void deletebyrow (String table Name,string rowkey[]) throws exception{htable H=new htable (CONF, tableName);        List<delete> list=new arraylist<delete> ();            for (String k:rowkey) {Delete d=new delete (Bytes.tobytes (k));        List.add (d);  } h.delete (list);//delete H.close ();//Release resource}/** * Get a data * * @param tableName table name * @param Rowkey Line number * ***/public void Getonedatabyrowkey (String tablename,string rowkey) throws exception{htable H=n        EW htable (conf, tableName);        Get G=new Get (bytes.tobytes (Rowkey));        Result R=h.get (g);            For (KeyValue K:r.raw ()) {System.out.println ("line number:" +bytes.tostringbinary (K.getrow ()));            System.out.println ("Timestamp:" +k.gettimestamp ());            SYSTEM.OUT.PRINTLN ("Column cluster:" +bytes.tostringbinary (K.getfamily ()));            SYSTEM.OUT.PRINTLN ("Column:" +bytes.tostringbinary (K.getqualifier ())); if (Bytes.tostringbinary (K.getqualifier ()). Equals ("Myage")) {//System.out.println ("Value:" +bytes.toint (K.getva           Lue ())); }else{String ss= bytes.tostring (K.getvalue ());            System.out.println ("Value:" +SS);    }} h.close ();        /** * Scan all data or specific data * @param tableName * **/public void ShowAll (String tableName) throws exception{        Htable h=new htable (conf, tableName);        Scan scan=new scan ();        Scan specific interval//scan scan=new scan (bytes.tobytes ("Start line number"), Bytes.tobytes ("End line number");        Resultscanner Scanner=h.getscanner (scan);            for (Result R:scanner) {System.out.println ("==================================");                For (KeyValue K:r.raw ()) {System.out.println ("line number:" +bytes.tostringbinary (K.getrow ()));                System.out.println ("Timestamp:" +k.gettimestamp ());                SYSTEM.OUT.PRINTLN ("Column cluster:" +bytes.tostringbinary (K.getfamily ()));                SYSTEM.OUT.PRINTLN ("Column:" +bytes.tostringbinary (K.getqualifier ()));       if (Bytes.tostringbinary (K.getqualifier ()). Equals ("Myage")) {         System.out.println ("Value:" +bytes.toint (K.getvalue ()));                }else{String ss= bytes.tostring (K.getvalue ());                System.out.println ("Value:" +SS);    }}} h.close (); }}


The project uses MAVEN, whose pom.xml are as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http: Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>    Com.codesolid</groupid> <artifactId>HelloJUnit</artifactId> <version>1.0</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactid&        Gt;junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactid >log4j</artifactId> <version>1.2.17</version> <scope>compile</scope&gt        ; </dependency> &LT;DEPENDENCY&GT <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-core</artifactId> &L t;version>1.0.4</version> </dependency> <dependency> <groupid>org.apac He.hbase</groupid> <artifactId>hbase</artifactId> <version>0.94.5</versio N> </dependency> </dependencies></project>


Third, the problems encountered

This server was in the failed servers list:localhost/127.0.0.1:60718

This problem occurs because the host name and the real IP mapping are not configured on the server, so you need to add 10.1.102.104 ccf04 (ccf04 as the host name of the/etc/hosts)

At the same time, you need to add ccf04 mapping on the machine running the program, my Java program running on Windows, need to modify the Windows host file, the last line to join 10.1.102.104 CCF04 can

Relive Hbaes deployment with Java Client access

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.