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