用java API来操作HBase
发布日期:2021-06-30 08:00:24
浏览次数:2
分类:技术文章
本文共 2581 字,大约阅读时间需要 8 分钟。
我们在日常工作中,用eclipse来编写具体的程序,来操作HBase数据库,首先在eclispe中配置HBase环境
新建项目--》导入HBase中的jar包
下面直接贴代码
package com.hbase;import java.io.IOException;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.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;public class HBaseTest { public static final String TABLE_NAME = "table1"; public static final String FAMILY_NAME = "family1"; public static final String ROW_KEY = "rowkey1"; public static void main(String[] args) throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.rootdir", "hdfs://master.dragon.org:9000/hbase"); // 使用eclipse,必须使用这个配置定位 conf.set("hbase.zookeeper.quorum", "master.dragon.org"); // HBaseAdmin用于创建表和删除表 HBaseAdmin admin = new HBaseAdmin(conf); createTable(admin);//创建表 deleteTable(admin);//删除表 // HTable适用于添加记录,查看记录 HTable hTable = new HTable(conf, TABLE_NAME); addRecord(hTable);//添加记录 getRecord(hTable);//得到记录 scannerTable(hTable);//全表扫描 hTable.close(); } private static void scannerTable(HTable hTable) throws IOException { Scan scan = new Scan(); ResultScanner scanner = hTable.getScanner(scan); for (Result result : scanner) { byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes()); System.out.println(result + "\t" + new String(value)); } } private static void getRecord(HTable hTable) throws IOException { Get get = new Get(ROW_KEY.getBytes()); Result result = hTable.get(get); byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes()); System.out.println(result + "\t" + new String(value)); } private static void addRecord(HTable hTable) throws IOException { Put put = new Put(ROW_KEY.getBytes()); put.add(FAMILY_NAME.getBytes(), "info".getBytes(), "50".getBytes()); hTable.put(put); } private static void deleteTable(HBaseAdmin admin) throws IOException { admin.disableTable(TABLE_NAME); admin.deleteTable(TABLE_NAME); } private static void createTable(HBaseAdmin admin) throws IOException { if (!admin.tableExists(TABLE_NAME)) { HTableDescriptor desc = new HTableDescriptor(TABLE_NAME); HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME); desc.addFamily(family); admin.createTable(desc); } }}
转载地址:https://huosl.blog.csdn.net/article/details/51179262 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月10日 07时21分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
HDOJ 1005 Number Sequence规律题
2019-04-30
一位ACMer过来人的心得
2019-04-30
HDOJ 2057A + B Again十六进制问题
2019-04-30
HDOJ 2089 不要62(数位dp/打表)
2019-04-30
HDOJ 2091 空心三角形
2019-04-30
HDOJ 2098 分拆素数和
2019-04-30
HDOJ 1236 排名,C语言结构体版的
2019-04-30
HDOJ 2097 Sky数 进制转换的问题
2019-04-30
HDOJ 2095 Find your present (2) 异或运算
2019-04-30
HDOJ 2578 Dating with girls(1)
2019-04-30
HDOJ 2199 Can you solve this equation?
2019-04-30
uva725 - Division
2019-04-30
HDOJ1238 Substrings
2019-04-30
ACM路上的一大失误
2019-04-30
CodeForces 628A Tennis Tournament
2019-04-30
HDOJ2049 不容易系列之(4)——考新郎
2019-04-30
CodeForces 248B - Chilly Willy - 找规律
2019-04-30
C与C++中的运算符和常用函数的使用
2019-04-30