码迷,mamicode.com
首页 > 编程语言 > 详细

Hbase关于Java常用API举例

时间:2016-07-07 23:50:32      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

1. HBase相关对Admin操作的的API封装在HBaseAdmin中,封装了HBase常用操作的API

使用方法:

    pom.xml

  <!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
      <dependency>
          <groupId>org.apache.hbase</groupId>
          <artifactId>hbase-client</artifactId>
          <version>0.98.5-hadoop2</version>
      </dependency>

 

 1 package com.qunar.demo.hbase;
 2 
 3 import com.google.common.base.Preconditions;
 4 import com.google.common.base.Strings;
 5 import com.google.common.collect.Lists;
 6 import org.apache.hadoop.conf.Configuration;
 7 import org.apache.hadoop.hbase.HBaseConfiguration;
 8 import org.apache.hadoop.hbase.HColumnDescriptor;
 9 import org.apache.hadoop.hbase.HTableDescriptor;
10 import org.apache.hadoop.hbase.TableName;
11 import org.apache.hadoop.hbase.client.HBaseAdmin;
12 import org.junit.Test;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 
16 import java.io.IOException;
17 import java.util.Iterator;
18 import java.util.List;
19 
20 /**
21  * author: 龚细军
22  * class-aim:
23  */
24 public class CreateHBaseTable {
25 
26     private static Logger logger = LoggerFactory.getLogger(CreateHBaseTable.class);
27 
28     /*创建表单*/
29     public void createTable(HBaseAdmin admin, String tableName,
30                             List<String> columnNames) throws IOException {
31 
32 
33         Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName), "table name is not allowed null or empty !");
34         Preconditions.checkArgument((null != columnNames && columnNames.size() > 0), "colume is not allowed empty !");
35         if (null == admin) {
36             throw new IllegalStateException("admin is empty !");
37         }
38 
39         HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
40         for (String colName : columnNames) {
41             hTableDescriptor.addFamily(new HColumnDescriptor(colName));
42         }
43         admin.createTable(hTableDescriptor);
44     }
45 
46     @Test
47     public void _createMain() throws IOException {
48 
49         Configuration conf = HBaseConfiguration.create();
50         HBaseAdmin admin = new HBaseAdmin(conf);
51         List list = Lists.newArrayList();
52         list.add("emp_col");
53         list.add("emp_col1");
54         list.add("emp_col2");
55         list.add("emp_col3");
56         this.createTable(admin, "emp", list);
57     }
58 
59     /*查询所有表单*/
60     public List<String> scanTables(HBaseAdmin admin) {
61 
62         HTableDescriptor[] hTableDescriptors = new HTableDescriptor[0];
63         if (null == admin) {
64             throw new IllegalStateException("admin is empty !");
65         }
66         try {
67             hTableDescriptors = admin.listTables();
68         } catch (IOException e) {
69             logger.error("获取表单异常", e);
70         }
71         List<String> tmpList = Lists.newArrayList();
72         for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
73             tmpList.add(String.valueOf(hTableDescriptor.getNameAsString()));
74         }
75         return tmpList;
76     }
77 
78     @Test
79     public void _scanTables() throws IOException {
80         Configuration conf = HBaseConfiguration.create();
81         HBaseAdmin admin = new HBaseAdmin(conf);
82         List<String> tableNames = this.scanTables(admin);
83         Iterator iterator = tableNames.iterator();
84         while (iterator.hasNext()) {
85             //日志被收集了,使用下面方式打出
86             //logger.info(String.valueOf(iterator.next()));
87             System.out.println(String.valueOf(iterator.next()));
88         }
89     }
90 
91 
92 }

 

HBase对于客户端封装在client调用:

  1 package com.qunar.demo.hbase;
  2 
  3 import com.google.common.base.Strings;
  4 import org.apache.hadoop.conf.Configuration;
  5 import org.apache.hadoop.hbase.HBaseConfiguration;
  6 import org.apache.hadoop.hbase.client.Get;
  7 import org.apache.hadoop.hbase.client.HTable;
  8 import org.apache.hadoop.hbase.client.Put;
  9 import org.apache.hadoop.hbase.client.Result;
 10 import org.apache.hadoop.hbase.util.Bytes;
 11 import org.junit.Test;
 12 import org.slf4j.Logger;
 13 import org.slf4j.LoggerFactory;
 14 
 15 import java.io.IOException;
 16 
 17 import static com.google.common.base.Preconditions.checkArgument;
 18 
 19 /**
 20  * author: 龚细军
 21  * class-aim: HBase客户端操作API使用
 22  */
 23 
 24 public class HBaseClientDemo {
 25 
 26     private Logger logger = LoggerFactory.getLogger(HBaseClientDemo.class);
 27     private Configuration configuration;
 28 
 29     /**
 30      * 插入数据
 31      *
 32      * @param configuration
 33      */
 34     public void InsertData(Configuration configuration,
 35                            String table_Name, String row,
 36                            String col, String col_name, String value) {
 37 
 38         checkArgument(configuration != null, "configuration is null ");
 39         checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
 40         checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
 41         checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
 42         checkArgument(!Strings.isNullOrEmpty(col_name), "col_name is empty");
 43         checkArgument(!Strings.isNullOrEmpty(value), "value is empty");
 44         HTable hTable = null;
 45 
 46         try {
 47             hTable = new HTable(configuration, table_Name);
 48             Put put = new Put(Bytes.toBytes(row));
 49             put.add(Bytes.toBytes(col), Bytes.toBytes(col_name), Bytes.toBytes(value));
 50             hTable.put(put);
 51         } catch (IOException e1) {
 52             logger.error("put data Exception", e1);
 53         } finally {
 54             try {
 55                 if (hTable != null) hTable.close();
 56             } catch (IOException e) {
 57                 logger.error("close HTable Exception", e);
 58             }
 59         }
 60     }
 61 
 62     /**
 63      * 获取数据
 64      */
 65     public Result getData(Configuration configuration,
 66                           String table_Name, String row) {
 67 
 68         checkArgument(configuration != null, "configuration is null ");
 69         checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
 70         checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
 71         HTable hTable = null;
 72         Result result = null;
 73         try {
 74             hTable = new HTable(configuration, table_Name);
 75             Get get = new Get(Bytes.toBytes(row));
 76             result = hTable.get(get);
 77         } catch (IOException e1) {
 78             logger.error("put data Exception", e1);
 79         } finally {
 80             try {
 81                 if (hTable != null) hTable.close();
 82             } catch (IOException e) {
 83                 logger.error("close HTable Exception", e);
 84             }
 85         }
 86         return result;
 87     }
 88 
 89     /**
 90      * 获取数据
 91      */
 92     public Result getData(Configuration configuration,
 93                           String table_Name, String row,
 94                           String col) {
 95 
 96         checkArgument(configuration != null, "configuration is null ");
 97         checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
 98         checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
 99         checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
100         HTable hTable = null;
101         Result result = null;
102         try {
103             hTable = new HTable(configuration, table_Name);
104             Get get = new Get(Bytes.toBytes(row));
105             result = hTable.get(get.addFamily(Bytes.toBytes(col)));
106         } catch (IOException e1) {
107             logger.error("put data Exception", e1);
108         } finally {
109             try {
110                 if (hTable != null) hTable.close();
111             } catch (IOException e) {
112                 logger.error("close HTable Exception", e);
113             }
114         }
115         return result;
116     }
117 
118 
119     /**
120      * 获取数据
121      */
122     public Result getData(Configuration configuration,
123                           String table_Name, String row,
124                           String col, String col_name) {
125 
126         checkArgument(configuration != null, "configuration is null ");
127         checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
128         checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
129         checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
130         checkArgument(!Strings.isNullOrEmpty(col_name), "col_name is empty");
131         HTable hTable = null;
132         Result result = null;
133         try {
134             hTable = new HTable(configuration, table_Name);
135             Get get = new Get(Bytes.toBytes(row));
136             result = hTable.get(get.addColumn(Bytes.toBytes(col),
137                     Bytes.toBytes(col_name)));
138         } catch (IOException e1) {
139             logger.error("put data Exception", e1);
140         } finally {
141             try {
142                 if (hTable != null) hTable.close();
143             } catch (IOException e) {
144                 logger.error("close HTable Exception", e);
145             }
146         }
147         return result;
148     }
149 
150 
151     @Test
152     public void _testPut() {
153         configuration = HBaseConfiguration.create();
154         this.InsertData(configuration, "emp", "2", "emp_col1", "name", "gongxijun");
155     }
156 
157     @Test
158     public void _testGet() {
159         configuration = HBaseConfiguration.create();
160         System.out.println(this.getData(configuration, "emp", "1","emp_col","name"));
161     }
162 
163 }

 

结果:

1 /home/gongxijun/java/jdk1.7.0_40/bin/java -ea -Didea.launcher.port=7535 -Didea.launcher.bin.path=/home/gongxijun/Qunar/idea-IU-139.1117.1/bin -Dfile.encoding=UTF-8 -classpath /home/gongxijun/Qunar/idea-IU-139.1117.1/lib/idea_rt.jar:/home/gongxijun/Qunar/idea-IU-139.1117.1/plugins/junit/lib/junit-rt.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/jfxrt.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/jce.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/jsse.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/javaws.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/rt.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/deploy.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/resources.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/management-agent.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/jfr.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/plugin.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/charsets.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/sunec.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/zipfs.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/localedata.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/sunjce_provider.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/dnsns.jar:/home/gongxijun/java/jdk1.7.0_40/jre/lib/ext/sunpkcs11.jar:/home/gongxijun/gitlab/flume/target/classes:/home/gongxijun/.m2/repository/org/apache/flume/flume-ng-sdk/1.5.0.1/flume-ng-sdk-1.5.0.1.jar:/home/gongxijun/.m2/repository/org/apache/avro/avro/1.7.3/avro-1.7.3.jar:/home/gongxijun/.m2/repository/org/codehaus/jackson/jackson-core-asl/1.8.8/jackson-core-asl-1.8.8.jar:/home/gongxijun/.m2/repository/com/thoughtworks/paranamer/paranamer/2.3/paranamer-2.3.jar:/home/gongxijun/.m2/repository/org/xerial/snappy/snappy-java/1.0.4.1/snappy-java-1.0.4.1.jar:/home/gongxijun/.m2/repository/org/apache/avro/avro-ipc/1.7.3/avro-ipc-1.7.3.jar:/home/gongxijun/.m2/repository/org/mortbay/jetty/jetty/6.1.26/jetty-6.1.26.jar:/home/gongxijun/.m2/repository/org/mortbay/jetty/jetty-util/6.1.26/jetty-util-6.1.26.jar:/home/gongxijun/.m2/repository/org/apache/velocity/velocity/1.7/velocity-1.7.jar:/home/gongxijun/.m2/repository/io/netty/netty/3.5.12.Final/netty-3.5.12.Final.jar:/home/gongxijun/.m2/repository/org/apache/thrift/libthrift/0.7.0/libthrift-0.7.0.jar:/home/gongxijun/.m2/repository/org/apache/flume/flume-ng-sinks/flume-ng-elasticsearch-sink/1.5.0/flume-ng-elasticsearch-sink-1.5.0.jar:/home/gongxijun/.m2/repository/org/apache/flume/flume-ng-core/1.5.0/flume-ng-core-1.5.0.jar:/home/gongxijun/.m2/repository/org/apache/flume/flume-ng-configuration/1.5.0/flume-ng-configuration-1.5.0.jar:/home/gongxijun/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar:/home/gongxijun/.m2/repository/commons-cli/commons-cli/1.2/commons-cli-1.2.jar:/home/gongxijun/.m2/repository/joda-time/joda-time/2.1/joda-time-2.1.jar:/home/gongxijun/.m2/repository/org/mortbay/jetty/servlet-api/2.5-20110124/servlet-api-2.5-20110124.jar:/home/gongxijun/.m2/repository/com/google/code/gson/gson/2.2.2/gson-2.2.2.jar:/home/gongxijun/.m2/repository/org/apache/mina/mina-core/2.0.4/mina-core-2.0.4.jar:/home/gongxijun/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar:/home/gongxijun/.m2/repository/org/apache/httpcomponents/httpclient/4.2.1/httpclient-4.2.1.jar:/home/gongxijun/.m2/repository/org/apache/httpcomponents/httpcore/4.2.1/httpcore-4.2.1.jar:/home/gongxijun/.m2/repository/commons-lang/commons-lang/2.5/commons-lang-2.5.jar:/home/gongxijun/.m2/repository/org/apache/hbase/hbase-client/0.98.5-hadoop2/hbase-client-0.98.5-hadoop2.jar:/home/gongxijun/.m2/repository/org/apache/hbase/hbase-common/0.98.5-hadoop2/hbase-common-0.98.5-hadoop2.jar:/home/gongxijun/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar:/home/gongxijun/.m2/repository/org/apache/hbase/hbase-protocol/0.98.5-hadoop2/hbase-protocol-0.98.5-hadoop2.jar:/home/gongxijun/.m2/repository/commons-codec/commons-codec/1.7/commons-codec-1.7.jar:/home/gongxijun/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/home/gongxijun/.m2/repository/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar:/home/gongxijun/.m2/repository/com/google/protobuf/protobuf-java/2.5.0/protobuf-java-2.5.0.jar:/home/gongxijun/.m2/repository/org/apache/zookeeper/zookeeper/3.4.6/zookeeper-3.4.6.jar:/home/gongxijun/.m2/repository/org/cloudera/htrace/htrace-core/2.04/htrace-core-2.04.jar:/home/gongxijun/.m2/repository/org/codehaus/jackson/jackson-mapper-asl/1.8.8/jackson-mapper-asl-1.8.8.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-common/2.2.0/hadoop-common-2.2.0.jar:/home/gongxijun/.m2/repository/org/apache/commons/commons-math/2.1/commons-math-2.1.jar:/home/gongxijun/.m2/repository/xmlenc/xmlenc/0.52/xmlenc-0.52.jar:/home/gongxijun/.m2/repository/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar:/home/gongxijun/.m2/repository/commons-net/commons-net/3.1/commons-net-3.1.jar:/home/gongxijun/.m2/repository/com/sun/jersey/jersey-core/1.9/jersey-core-1.9.jar:/home/gongxijun/.m2/repository/com/sun/jersey/jersey-json/1.9/jersey-json-1.9.jar:/home/gongxijun/.m2/repository/org/codehaus/jettison/jettison/1.1/jettison-1.1.jar:/home/gongxijun/.m2/repository/stax/stax-api/1.0.1/stax-api-1.0.1.jar:/home/gongxijun/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.3-1/jaxb-impl-2.2.3-1.jar:/home/gongxijun/.m2/repository/javax/xml/bind/jaxb-api/2.2.2/jaxb-api-2.2.2.jar:/home/gongxijun/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar:/home/gongxijun/.m2/repository/org/codehaus/jackson/jackson-jaxrs/1.8.3/jackson-jaxrs-1.8.3.jar:/home/gongxijun/.m2/repository/org/codehaus/jackson/jackson-xc/1.8.3/jackson-xc-1.8.3.jar:/home/gongxijun/.m2/repository/commons-el/commons-el/1.0/commons-el-1.0.jar:/home/gongxijun/.m2/repository/net/java/dev/jets3t/jets3t/0.6.1/jets3t-0.6.1.jar:/home/gongxijun/.m2/repository/commons-configuration/commons-configuration/1.6/commons-configuration-1.6.jar:/home/gongxijun/.m2/repository/commons-digester/commons-digester/1.8/commons-digester-1.8.jar:/home/gongxijun/.m2/repository/commons-beanutils/commons-beanutils/1.7.0/commons-beanutils-1.7.0.jar:/home/gongxijun/.m2/repository/commons-beanutils/commons-beanutils-core/1.8.0/commons-beanutils-core-1.8.0.jar:/home/gongxijun/.m2/repository/com/jcraft/jsch/0.1.42/jsch-0.1.42.jar:/home/gongxijun/.m2/repository/org/apache/commons/commons-compress/1.4.1/commons-compress-1.4.1.jar:/home/gongxijun/.m2/repository/org/tukaani/xz/1.0/xz-1.0.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-auth/2.2.0/hadoop-auth-2.2.0.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-mapreduce-client-core/2.2.0/hadoop-mapreduce-client-core-2.2.0.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-yarn-common/2.2.0/hadoop-yarn-common-2.2.0.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-yarn-api/2.2.0/hadoop-yarn-api-2.2.0.jar:/home/gongxijun/.m2/repository/com/google/inject/guice/3.0/guice-3.0.jar:/home/gongxijun/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar:/home/gongxijun/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:/home/gongxijun/.m2/repository/com/sun/jersey/jersey-server/1.9/jersey-server-1.9.jar:/home/gongxijun/.m2/repository/asm/asm/3.1/asm-3.1.jar:/home/gongxijun/.m2/repository/com/sun/jersey/contribs/jersey-guice/1.9/jersey-guice-1.9.jar:/home/gongxijun/.m2/repository/com/google/inject/extensions/guice-servlet/3.0/guice-servlet-3.0.jar:/home/gongxijun/.m2/repository/org/apache/hadoop/hadoop-annotations/2.2.0/hadoop-annotations-2.2.0.jar:/home/gongxijun/java/jdk1.7.0_40/lib/tools.jar:/home/gongxijun/.m2/repository/com/github/stephenc/findbugs/findbugs-annotations/1.3.9-1/findbugs-annotations-1.3.9-1.jar:/home/gongxijun/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar:/home/gongxijun/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/home/gongxijun/Qunar/idea-IU-139.1117.1/lib/junit-4.11.jar:/home/gongxijun/Qunar/idea-IU-139.1117.1/lib/hamcrest-core-1.3.jar:/home/gongxijun/Qunar/idea-IU-139.1117.1/lib/hamcrest-library-1.3.jar com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.qunar.demo.hbase.HBaseClientDemo,_testGet
2 log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
3 log4j:WARN Please initialize the log4j system properly.
4 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
5 keyvalues={1/emp_col:name/1467883522089/Put/vlen=8/mvcc=0}
6 
7 Process finished with exit code 0

 

Hbase关于Java常用API举例

标签:

原文地址:http://www.cnblogs.com/gongxijun/p/5651834.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!