设为首页 收藏本站
查看: 784|回复: 0

[经验分享] HBase ZooKeeper Client编程

[复制链接]

尚未签到

发表于 2015-9-6 08:30:49 | 显示全部楼层 |阅读模式
  安装好HBase和ZooKeeper,当然要验证下是否可以正常工作了。在HBase Shell中测试是可以的,随着进行Java代码的验证。
  环境:
  OS:     Windows7
  IDE:    Eclipse
  Jars:    将ZooKeeper的一个jar包、HBase的两个jar包、Hadoop的jar包和在lib目录下的jar包包含在工程的环境变量中
  xml:    将hbase-site.xml放到工程的环境变量中
  hosts:   将C:\Windows\System32\drivers\etc\hosts的内容修改为:
  192.168.0.2 localhost
192.168.0.2 master
192.168.0.3 slave1
192.168.0.4 slave2
192.168.0.5 slave3
  如果不修改,会出现ZooKeeper连接错误。
  至此所需要的环境已经搭建完毕,只需要将开发机器和Hadoop集群连接到同一个网络中即可。以下是我个人写的测试程序和结果。



  1 package com.antl.hbase;
  2
  3 import org.apache.hadoop.conf.Configuration;
  4 import org.apache.hadoop.hbase.HBaseConfiguration;
  5 import org.apache.hadoop.hbase.HColumnDescriptor;
  6 import org.apache.hadoop.hbase.HTableDescriptor;
  7 import org.apache.hadoop.hbase.KeyValue;
  8 import org.apache.hadoop.hbase.client.Delete;
  9 import org.apache.hadoop.hbase.client.Get;
10 import org.apache.hadoop.hbase.client.HBaseAdmin;
11 import org.apache.hadoop.hbase.client.HTable;
12 import org.apache.hadoop.hbase.client.Put;
13 import org.apache.hadoop.hbase.client.Result;
14 import org.apache.hadoop.hbase.client.ResultScanner;
15 import org.apache.hadoop.hbase.client.Scan;
16 import org.apache.hadoop.hbase.util.Bytes;
17
18 public class HBaseClient {
19
20     private Configuration conf = null;
21     private HBaseAdmin admin = null;
22     
23     public HBaseClient() throws Exception{
24         conf = HBaseConfiguration.create();
25         admin = new HBaseAdmin(conf);
26     }
27     
28     /**
29      * @param tablename
30      * @param cfs
31      * @throws Exception
32      */
33     public void createTable(String tablename, String[] cfs) throws Exception{
34         if(admin.tableExists(tablename)){
35             System.out.println(tablename + " exists.");
36         }else{
37             HTableDescriptor tableDesc = new HTableDescriptor(tablename);
38             for(String cf : cfs){
39                 tableDesc.addFamily(new HColumnDescriptor(cf));
40             }
41             admin.createTable(tableDesc);
42             System.out.println("crate table " + tablename + " sucessfully.");
43         }
44     }
45     
46     public void deleteTable(String tablename) throws Exception{
47         admin.disableTable(tablename);
48         admin.deleteTable(tablename);
49         
50         System.out.println("delete " + tablename + " successfully.");
51     }
52     
53     /**
54      * 新建一个如下的表格
55      * +---------------------------------------------------+
56      * |         |                grade          | level   |
57      * |         | Chinese |  Math    | English  |         |
58      * +---------------------------------------------------+
59      * |  John   |  80     |   80     |    80    |  good   |            
60      * |   Tom   |  90     |   90     |    90    |excellent|   
61      * +---------------------------------------------------+
62      * @param tablename
63      * @throws Exception
64      */
65     public void addRows(String tablename) throws Exception{
66         HTable table = new HTable(conf, tablename);
67         Put put = new Put(Bytes.toBytesBinary("John"));
68         put.add(Bytes.toBytes("grade"), Bytes.toBytes("Chinese"), Bytes.toBytes("80"));
69         put.add(Bytes.toBytes("grade"), Bytes.toBytes("Math"), Bytes.toBytes("80"));
70         put.add(Bytes.toBytes("grade"), Bytes.toBytes("English"), Bytes.toBytes("80"));
71         put.add(Bytes.toBytes("level"), null, Bytes.toBytes("good"));
72         table.put(put);
73         
74         
75         put = new Put(Bytes.toBytesBinary("Tom"));
76         put.add(Bytes.toBytes("grade"), Bytes.toBytes("Chinese"), Bytes.toBytes("90"));
77         put.add(Bytes.toBytes("grade"), Bytes.toBytes("Math"), Bytes.toBytes("90"));
78         put.add(Bytes.toBytes("grade"), Bytes.toBytes("English"), Bytes.toBytes("90"));
79         put.add(Bytes.toBytes("level"), null, Bytes.toBytes("excellent"));
80         table.put(put);
81         
82         System.out.println("add John and Tom rows successfully.");
83     }
84     
85     public void delRow(String tablename, String rowkey) throws Exception{
86         HTable table = new HTable(conf, tablename);
87         Delete dl = new Delete(Bytes.toBytes(rowkey));
88         table.delete(dl);
89         System.out.println("delete " + rowkey + " successfully.");
90     }
91     
92     public void getRow(String tablename, String _rowkey) throws Exception{
93         HTable table = new HTable(conf, tablename);
94         Get get = new Get(Bytes.toBytes(_rowkey));
95         Result rs = table.get(get);
96         
97         for(KeyValue kv : rs.raw()){
98             String rowkey = Bytes.toString(kv.getRow());
99             String family = Bytes.toString(kv.getFamily());
100             String qualifier = Bytes.toString(kv.getQualifier());
101             long timestamp = kv.getTimestamp();
102             String value = Bytes.toString(kv.getValue());
103            
104             System.out.print(rowkey + "\t");
105             System.out.print(family + "\t");
106             System.out.print(qualifier + "\t");
107             System.out.print(timestamp + "\t");
108             System.out.println(value);
109         }
110     }
111     
112     public void list(String tablename) throws Exception{
113         HTable table = new HTable(conf, tablename);
114         Scan scan = new Scan();
115         ResultScanner rs = table.getScanner(scan);
116         if(rs == null){
117             System.out.println("the table does not exist");
118         }
119         for(Result r : rs){
120             KeyValue[] kvs = r.raw();
121             for(KeyValue kv : kvs){
122                 String rowkey = Bytes.toString(kv.getRow());
123                 String family = Bytes.toString(kv.getFamily());
124                 String qualifier = Bytes.toString(kv.getQualifier());
125                 long timestamp = kv.getTimestamp();
126                 String value = Bytes.toString(kv.getValue());
127                 
128                 System.out.print(rowkey + "\t");
129                 System.out.print(family + "\t");
130                 System.out.print(qualifier + "\t");
131                 System.out.print(timestamp + "\t");
132                 System.out.println(value);
133             }
134             System.out.println();
135         }
136     }
137     
138     public void test() throws Exception{
139         String tablename = "report";
140         
141         this.createTable(tablename, new String[]{"grade","level"});//create a report table with grade,level columns.
142         this.addRows(tablename);
143         this.list(tablename);
144         this.delRow(tablename, "John");
145         this.list(tablename);
146     }
147     
148     public static void main(String[] args) throws Exception{
149         HBaseClient client = new HBaseClient();
150         client.test();
151         
152 //        client.deleteTable("report");
153     }
154     
155 }
  
  结果如下:



12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.3.5-1301095, built on 03/15/2012 19:48 GMT
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:host.name=Eric-PC
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.version=1.6.0_32
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Sun Microsystems Inc.
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.home=D:\Java\jre6
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.class.path=E:\hadoop workspace\HBaseClient\bin;E:\hadoop workspace\HBaseClient\jars\asm-3.2.jar;E:\hadoop workspace\HBaseClient\jars\aspectjrt-1.6.5.jar;E:\hadoop workspace\HBaseClient\jars\aspectjtools-1.6.5.jar;E:\hadoop workspace\HBaseClient\jars\commons-beanutils-1.7.0.jar;E:\hadoop workspace\HBaseClient\jars\commons-beanutils-core-1.8.0.jar;E:\hadoop workspace\HBaseClient\jars\commons-cli-1.2.jar;E:\hadoop workspace\HBaseClient\jars\commons-codec-1.4.jar;E:\hadoop workspace\HBaseClient\jars\commons-collections-3.2.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-configuration-1.6.jar;E:\hadoop workspace\HBaseClient\jars\commons-daemon-1.0.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-digester-1.8.jar;E:\hadoop workspace\HBaseClient\jars\commons-el-1.0.jar;E:\hadoop workspace\HBaseClient\jars\commons-httpclient-3.0.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-io-2.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-lang-2.4.jar;E:\hadoop workspace\HBaseClient\jars\commons-logging-1.1.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-logging-api-1.0.4.jar;E:\hadoop workspace\HBaseClient\jars\commons-math-2.1.jar;E:\hadoop workspace\HBaseClient\jars\commons-net-1.4.1.jar;E:\hadoop workspace\HBaseClient\jars\core-3.1.1.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-ant-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-capacity-scheduler-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-client-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-core-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-examples-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-fairscheduler-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-minicluster-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-test-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-thriftfs-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hadoop-tools-1.0.3.jar;E:\hadoop workspace\HBaseClient\jars\hbase-0.92.1-tests.jar;E:\hadoop workspace\HBaseClient\jars\hbase-0.92.1.jar;E:\hadoop workspace\HBaseClient\jars\hsqldb-1.8.0.10.jar;E:\hadoop workspace\HBaseClient\jars\jackson-core-asl-1.8.8.jar;E:\hadoop workspace\HBaseClient\jars\jackson-mapper-asl-1.8.8.jar;E:\hadoop workspace\HBaseClient\jars\jasper-compiler-5.5.12.jar;E:\hadoop workspace\HBaseClient\jars\jasper-runtime-5.5.12.jar;E:\hadoop workspace\HBaseClient\jars\jdeb-0.8.jar;E:\hadoop workspace\HBaseClient\jars\jersey-core-1.8.jar;E:\hadoop workspace\HBaseClient\jars\jersey-json-1.8.jar;E:\hadoop workspace\HBaseClient\jars\jersey-server-1.8.jar;E:\hadoop workspace\HBaseClient\jars\jets3t-0.6.1.jar;E:\hadoop workspace\HBaseClient\jars\jetty-6.1.26.jar;E:\hadoop workspace\HBaseClient\jars\jetty-util-6.1.26.jar;E:\hadoop workspace\HBaseClient\jars\jsch-0.1.42.jar;E:\hadoop workspace\HBaseClient\jars\junit-4.5.jar;E:\hadoop workspace\HBaseClient\jars\kfs-0.2.2.jar;E:\hadoop workspace\HBaseClient\jars\log4j-1.2.15.jar;E:\hadoop workspace\HBaseClient\jars\mockito-all-1.8.5.jar;E:\hadoop workspace\HBaseClient\jars\oro-2.0.8.jar;E:\hadoop workspace\HBaseClient\jars\servlet-api-2.5-20081211.jar;E:\hadoop workspace\HBaseClient\jars\slf4j-api-1.4.3.jar;E:\hadoop workspace\HBaseClient\jars\slf4j-log4j12-1.4.3.jar;E:\hadoop workspace\HBaseClient\jars\xmlenc-0.52.jar;E:\hadoop workspace\HBaseClient\jars\zookeeper-3.3.5.jar
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.library.path=D:\Java\jre6\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Java/jre6/bin/client;D:/Java/jre6/bin;D:/Java/jre6/lib/i386;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Java\jdk1.6.0_32\bin;D:\Java\jdk1.6.0_32\jre\bin;D:\Program Files\MySQL\MySQL Server 5.5\bin;D:\Program Files\TortoiseGit\bin;D:\Program Files\TortoiseSVN\bin;D:\Program Files\Tencent\QQPCMgr\6.8.2377.202;D:\cygwin\bin;D:\cygwin\usr\sbin;D:\apache-ant-1.8.0\bin;C:\Program Files\Windows Live\Shared;D:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;D:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;D:\Program Files\Microsoft Visual Studio\Common\Tools;D:\Program Files\Microsoft Visual Studio\VC98\bin;D:\Program Files\Tencent\QQPCMgr\6.8.2377.202;E:\hadoop eclipse;;.
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=C:\Users\Eric\AppData\Local\Temp\
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:os.name=Windows 7
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:os.arch=x86
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:os.version=6.1
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:user.name=hadoop
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:user.home=C:\Users\Eric
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Client environment:user.dir=E:\hadoop workspace\HBaseClient
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=hconnection
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/192.168.0.2:2181
12/07/29 03:51:34 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 8832@Eric-PC
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Socket connection established to localhost/192.168.0.2:2181, initiating session
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/192.168.0.2:2181, sessionid = 0x138ce5c180b0006, negotiated timeout = 40000
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=localhost:2181 sessionTimeout=180000 watcher=catalogtracker-on-org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation@1386918
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Opening socket connection to server localhost/192.168.0.2:2181
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Socket connection established to localhost/192.168.0.2:2181, initiating session
12/07/29 03:51:34 INFO zookeeper.RecoverableZooKeeper: The identifier of this process is 8832@Eric-PC
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: Session establishment complete on server localhost/192.168.0.2:2181, sessionid = 0x138ce5c180b0007, negotiated timeout = 40000
12/07/29 03:51:34 INFO zookeeper.ZooKeeper: Session: 0x138ce5c180b0007 closed
12/07/29 03:51:34 INFO zookeeper.ClientCnxn: EventThread shut down
crate table report sucessfully.
add John and Tom rows successfully.
John    grade    Chinese    1343505147961    80
John    grade    English    1343505147961    80
John    grade    Math       1343505147961    80
John    level               1343505147961    good
Tom    grade    Chinese    1343505147969    90
Tom    grade    English    1343505147969    90
Tom    grade    Math       1343505147969    90
Tom    level               1343505147969    excellent
delete John successfully.
Tom    grade    Chinese    1343505147969    90
Tom    grade    English    1343505147969    90
Tom    grade    Math       1343505147969    90
Tom    level               1343505147969    excellent

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-109879-1-1.html 上篇帖子: Zookeeper C API 指南三(回调函数) 下篇帖子: zookeeper适用场景:配置文件同步
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表