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.-hadoop2</version>
</dependency>
 package com.qunar.demo.hbase;

 import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
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.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.IOException;
import java.util.Iterator;
import java.util.List; /**
* author: 龚细军
* class-aim:
*/
public class CreateHBaseTable { private static Logger logger = LoggerFactory.getLogger(CreateHBaseTable.class); /*创建表单*/
public void createTable(HBaseAdmin admin, String tableName,
List<String> columnNames) throws IOException { Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName), "table name is not allowed null or empty !");
Preconditions.checkArgument((null != columnNames && columnNames.size() > 0), "colume is not allowed empty !");
if (null == admin) {
throw new IllegalStateException("admin is empty !");
} HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String colName : columnNames) {
hTableDescriptor.addFamily(new HColumnDescriptor(colName));
}
admin.createTable(hTableDescriptor);
} @Test
public void _createMain() throws IOException { Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
List list = Lists.newArrayList();
list.add("emp_col");
list.add("emp_col1");
list.add("emp_col2");
list.add("emp_col3");
this.createTable(admin, "emp", list);
} /*查询所有表单*/
public List<String> scanTables(HBaseAdmin admin) { HTableDescriptor[] hTableDescriptors = new HTableDescriptor[0];
if (null == admin) {
throw new IllegalStateException("admin is empty !");
}
try {
hTableDescriptors = admin.listTables();
} catch (IOException e) {
logger.error("获取表单异常", e);
}
List<String> tmpList = Lists.newArrayList();
for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
tmpList.add(String.valueOf(hTableDescriptor.getNameAsString()));
}
return tmpList;
} @Test
public void _scanTables() throws IOException {
Configuration conf = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(conf);
List<String> tableNames = this.scanTables(admin);
Iterator iterator = tableNames.iterator();
while (iterator.hasNext()) {
//日志被收集了,使用下面方式打出
//logger.info(String.valueOf(iterator.next()));
System.out.println(String.valueOf(iterator.next()));
}
} }

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

 package com.qunar.demo.hbase;

 import com.google.common.base.Strings;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.IOException; import static com.google.common.base.Preconditions.checkArgument; /**
* author: 龚细军
* class-aim: HBase客户端操作API使用
*/ public class HBaseClientDemo { private Logger logger = LoggerFactory.getLogger(HBaseClientDemo.class);
private Configuration configuration; /**
* 插入数据
*
* @param configuration
*/
public void InsertData(Configuration configuration,
String table_Name, String row,
String col, String col_name, String value) { checkArgument(configuration != null, "configuration is null ");
checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
checkArgument(!Strings.isNullOrEmpty(col_name), "col_name is empty");
checkArgument(!Strings.isNullOrEmpty(value), "value is empty");
HTable hTable = null; try {
hTable = new HTable(configuration, table_Name);
Put put = new Put(Bytes.toBytes(row));
put.add(Bytes.toBytes(col), Bytes.toBytes(col_name), Bytes.toBytes(value));
hTable.put(put);
} catch (IOException e1) {
logger.error("put data Exception", e1);
} finally {
try {
if (hTable != null) hTable.close();
} catch (IOException e) {
logger.error("close HTable Exception", e);
}
}
} /**
* 获取数据
*/
public Result getData(Configuration configuration,
String table_Name, String row) { checkArgument(configuration != null, "configuration is null ");
checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
HTable hTable = null;
Result result = null;
try {
hTable = new HTable(configuration, table_Name);
Get get = new Get(Bytes.toBytes(row));
result = hTable.get(get);
} catch (IOException e1) {
logger.error("put data Exception", e1);
} finally {
try {
if (hTable != null) hTable.close();
} catch (IOException e) {
logger.error("close HTable Exception", e);
}
}
return result;
} /**
* 获取数据
*/
public Result getData(Configuration configuration,
String table_Name, String row,
String col) { checkArgument(configuration != null, "configuration is null ");
checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
HTable hTable = null;
Result result = null;
try {
hTable = new HTable(configuration, table_Name);
Get get = new Get(Bytes.toBytes(row));
result = hTable.get(get.addFamily(Bytes.toBytes(col)));
} catch (IOException e1) {
logger.error("put data Exception", e1);
} finally {
try {
if (hTable != null) hTable.close();
} catch (IOException e) {
logger.error("close HTable Exception", e);
}
}
return result;
} /**
* 获取数据
*/
public Result getData(Configuration configuration,
String table_Name, String row,
String col, String col_name) { checkArgument(configuration != null, "configuration is null ");
checkArgument(!Strings.isNullOrEmpty(table_Name), "tableName is empty");
checkArgument(!Strings.isNullOrEmpty(row), "row is empty");
checkArgument(!Strings.isNullOrEmpty(col), "col is empty");
checkArgument(!Strings.isNullOrEmpty(col_name), "col_name is empty");
HTable hTable = null;
Result result = null;
try {
hTable = new HTable(configuration, table_Name);
Get get = new Get(Bytes.toBytes(row));
result = hTable.get(get.addColumn(Bytes.toBytes(col),
Bytes.toBytes(col_name)));
} catch (IOException e1) {
logger.error("put data Exception", e1);
} finally {
try {
if (hTable != null) hTable.close();
} catch (IOException e) {
logger.error("close HTable Exception", e);
}
}
return result;
} @Test
public void _testPut() {
configuration = HBaseConfiguration.create();
this.InsertData(configuration, "emp", "2", "emp_col1", "name", "gongxijun");
} @Test
public void _testGet() {
configuration = HBaseConfiguration.create();
System.out.println(this.getData(configuration, "emp", "1","emp_col","name"));
} }

结果:

 /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
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
keyvalues={1/emp_col:name/1467883522089/Put/vlen=8/mvcc=0} Process finished with exit code 0

Hbase关于Java常用API举例的更多相关文章

  1. Java常用API(Arrays类)

    Java常用API(Arrays类) 什么是Arrays类? java.util.Arrays 此类包含用来操作数组的各种方法,比如排序和搜索等.其所有方法均为静态方法,调用起来 非常简单. 这里我们 ...

  2. Java 常用API(二)

    目录 Java 常用API(二) 1. Object类 2. Date类 概述 构造方法和成员方法 3. DateFormat类 概述 SimpleDateFormat类 练习 4. Calendar ...

  3. Java 常用API(一)

    目录 Java 常用API(一) 1. Scanner类 引用类型的一般使用步骤 Scanner的使用步骤 例题 2. 匿名对象 概述 匿名对象作为方法的参数 匿名对象作为方法的返回值 3. Rand ...

  4. Java常用API(Math类)

    Java常用API(Math类) Math类的作用 java.lang.Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数.类似这样的工具 类,其所有方法均为静态方法,并且 ...

  5. Java常用API(String类)

    Java常用API(String类) 概述: java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 1. ...

  6. Java常用API(ArrayList类)

    Java常用API(ArrayList类) 我们为什么要使用ArrayList类? 为了更加方便的储存对象,因为使用普通的数组来存储对象太过麻烦了,因为数组的一个很大的弱点就是长度从一开始就固定了,所 ...

  7. Java常用API(Random类)

    Java常用API(Random类) Random:是一个用于生成随机数的类 构造方法 public Random() :创建一个新的随机数生成器. 返回随机数的方法 public int nextI ...

  8. Java常用API(Scanner类)

    Java常用API( Scanner类)1 1.Scanner类 首先给大家介绍一下什么是JavaAPI API(Application Programming Interface),应用程序编程接口 ...

  9. java基础3.0:Java常用API

    本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...

随机推荐

  1. JAVA join()方法

    转自:http://www.open-open.com/lib/view/open1371741636171.html 一.为什么要用join()方法 在很多情况下,主线程生成并起动了子线程,如果子线 ...

  2. mysql基本语句

    mysql -u root -p; 登录数据库 show databases; 展示数据库 show tables; 展示表 desc messages; 查看messages表的结构 drop da ...

  3. 结构体struts的长度

    在需要计算结构体大小的时候,涉及到的一个问题就是其对齐模数 计算机系统对基本类型数据在内存中存放的位置有限制,它们会要求这些数据的首地址的值是某个数k(通常它为4或8)的倍数,这就是所谓的内存对齐,而 ...

  4. 将光标放到input内容的最后

    在使用setSelectionRange方法的时候一定要加setTimeout,否则无效,代码 var num = $('#input_phone').val().length; setTimeout ...

  5. ListView的基础入门

    1.先在XML中定义一个ListView视图 2.获得ListView,在Mainactivity中声明 3.创建一个类继承适配器BaseAdapter,实现四个方法 public class MyL ...

  6. 20160929001 Guid生成

    全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.   using System; namespace GUID测试 ...

  7. iOS设计规范整理|汇总

    来源 UI中国

  8. HTML 链接 - href

    链接 在HTML的学习中,链接的标签发挥着很大的作用,HTML 使用超级链接与网络上的另一个文档相连.几乎可以在所有的网页中找到链接.点击链接可以从一张页面跳转到另一张页面. 比如说:实例 创建超级链 ...

  9. Sprite(精灵)&& 三个特殊的层Layer

    用来作为以后复习使用. 1 #include "ScenceScend.h" CCScene* ScenceScend::scene() { CCScene* s = CCScen ...

  10. python 学习笔记十二 CSS基础(进阶篇)

    1.CSS 简介 CSS 指层叠样式表 (Cascading Style Sheets) 样式定义如何显示 HTML 元素 样式通常存储在样式表中 把样式添加到 HTML 4.0 中,是为了解决内容与 ...