package com.hbase;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
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.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.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseDemo {
// private static final String QUORUM = "w5bigdata009,w5bigdata010,w5bigdata011,w5bigdata007,w5bigdata008";
private static final String QUORUM = "10.152.254.202,10.152.254.203,10.152.254.204,10.152.254.210,10.152.254.211";
private static final String CLIENTPORT = "2181";
private static Configuration conf;
// private static HConnection conn;
static{
conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://bigdata/apps/hbase/data");
conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT);
//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.quorum", QUORUM);
}
public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
String tableName="OCDC:DR_UIP_USR_23G_LAC20170803";
String columnFamily="F";
// HbaseDemo.create(tableName, columnFamily);
// HbaseDemo.put(tableName, "row1", columnFamily, "cl1", "data");
// HbaseDemo.get(tableName, "0000005588142:54:70 13-70-7102",columnFamily,"imei");
// HbaseDemo.scan(tableName);
HbaseDemo.scan2(tableName);
// HbaseDemo.delete(tableName);
}
//创建一张表
public static void create(String tableName, String columnFamily) throws IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)) {
System.out.println("table exists!");
}else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFamily));
admin.createTable(tableDesc);
System.out.println("create table success!");
}
}
//添加一条记录
public static void put(String tableName, String row, String columnFamily, String column, String data) throws IOException{
HTable table = new HTable(conf, tableName);
Put p1 = new Put(Bytes.toBytes(row));
p1.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
table.put(p1);
System.out.println("put'"+row+"',"+columnFamily+":"+column+"','"+data+"'");
table.close();
}
//读取一条记录
public static void get(String tableName, String row,String columnFamily, String column) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
byte[] value = result.getValue(columnFamily.getBytes(),column.getBytes() );
System.out.println("Get: "+result+"\t"+new String(value));
table.close();
}
//显示所有数据
public static void scan(String tableName) throws IOException{
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("Scan: "+result);
}
table.close();
}
//显示所有数据2
public static void scan2(String tableName) throws IOException{
HTable table = new HTable(conf, tableName);
try {
Scan scan = new Scan();
scan.setStartRow("13431798266:20170803063508".getBytes());
scan.setStopRow("13431798266:20170803133508".getBytes());
// Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,
// new BinaryPrefixComparator("00000055881".getBytes()));
// Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL,
// new RegexStringComparator(".*13431798266:201708030[6-8][0-4][0-5]*"));
// scan.setFilter(filter);
ResultScanner scanner = table.getScanner(scan);
int num = 0;
for (Result result : scanner) {
num++;
System.out.println("获得到rowkey:" + new String(result.getRow()));
for (KeyValue keyValue : result.raw()) {
System.out.println("列columnFamily: " + new String(keyValue.getFamily())
+" ===Column: " + new String(keyValue.getQualifier())
+" ===value: " + new String(keyValue.getValue()));
}
if(num==5){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//删除表
public static void delete(String tableName) throws IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName)){
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
} catch (IOException e) {
e.printStackTrace();
System.out.println("Delete "+tableName+" 失败");
}
}
System.out.println("Delete "+tableName+" 成功");
}
//hbase操作必备
private static Configuration getConfiguration() {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");
//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.quorum", "hadoop0");
return conf;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java访问Hbase数据库Demo
共34个文件
jar:20个
xml:4个
java:1个
需积分: 50 77 下载量 36 浏览量
2017-11-23
11:32:21
上传
评论
收藏 12.66MB RAR 举报
温馨提示
java访问Hbase数据库实用小例子,增删改查创建表,教你轻松解决hbase
资源推荐
资源详情
资源评论
收起资源包目录
HbaseDemo.rar (34个子文件)
HbaseDemo
.project 1KB
.settings
.jsdtscope 503B
org.eclipse.wst.common.component 479B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.common.project.facet.core.xml 305B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
src
hbase-site.xml 4KB
com
hbase
HbaseDemo.java 6KB
build
classes
hbase-site.xml 4KB
com
hbase
HbaseDemo.class 6KB
WebContent
WEB-INF
web.xml 652B
lib
hbase-protocol-0.98.3-hadoop2.jar 3.23MB
guava-12.0.1.jar 1.71MB
commons-lang-2.6.jar 278KB
netty-3.6.6.Final.jar 1.15MB
htrace-core-2.04.jar 31KB
zookeeper-3.4.6.jar 774KB
commons-configuration-1.6.jar 292KB
slf4j-api-1.6.4.jar 25KB
hadoop-common-2.2.0.jar 2.61MB
log4j-1.2.17.jar 478KB
commons-codec-1.7.jar 254KB
hadoop-auth-2.2.0.jar 49KB
jackson-core-asl-1.8.8.jar 222KB
hbase-client-0.98.3-hadoop2.jar 902KB
protobuf-java-2.5.0.jar 521KB
hbase-common-0.98.3-hadoop2.jar 429KB
slf4j-log4j12-1.6.4.jar 10KB
commons-logging-1.1.1.jar 59KB
jackson-mapper-asl-1.8.8.jar 653KB
commons-collections-3.2.1.jar 562KB
META-INF
MANIFEST.MF 39B
.classpath 588B
共 34 条
- 1
资源评论
zhanjiall
- 粉丝: 0
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功