代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
Java操作HBase示例
作者:
Jandison
/ 发布于
2014/12/30
/
462
package com.hbase.test; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; 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.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HConnection; import org.apache.hadoop.hbase.client.HConnectionManager; 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.util.Bytes; public class HBaseUtil { private static Configuration conf = null; private static HConnection pool = null; private static HTable hTable = null; static { conf = HBaseConfiguration.create(); try { pool = HConnectionManager.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } private static Configuration getConfiguration(){ return pool.getConfiguration(); } /* * 生成HTable实例时,每个实例都需要扫描.META表,检查该表是否存在,是否可用等 * 比较耗性能 */ private static HTable getHTable(String tableName){ if(hTable == null){ try { hTable = new HTable(getConfiguration(),tableName); } catch (IOException e) { e.printStackTrace(); } } return hTable; } /* * 将byte[]转换成String * UTF8编码 */ private static String changeByteToString(byte[] b) throws UnsupportedEncodingException{ String ret = null; if(b.length > 0){ ret = new String(b,"UTF8"); } return ret; } public static void createTable(String tabName,String[] families) throws MasterNotRunningException, ZooKeeperConnectionException, IOException{ HBaseAdmin admin = new HBaseAdmin(getConfiguration()); if(admin.tableExists(tabName)){ System.out.println("创建表失败;表名【" + tabName + "】已经存在"); return; }else{ HTableDescriptor descriptor = new HTableDescriptor(tabName); for(int i = 0; i < families.length; i++){ descriptor.addFamily(new HColumnDescriptor(families[i])); } admin.createTable(descriptor); System.out.println("创建表成功:表【" + tabName + "】创建成功"); } } public static void deleteTable(String tabName){ if(tabName != null && tabName.length() > 0){ try{ HBaseAdmin admin = new HBaseAdmin(getConfiguration()); /* * HBase在删除表步骤: * 1,disable tableName * 2,drop tableName */ admin.disableTable(tabName); admin.deleteTable(tabName); System.out.println("表【" + tabName + "】删除成功"); }catch(IOException e){ System.out.println("表删除失败:【" + tabName + "】 删除失败"); } }else{ System.out.println("表删除失败:表名【" + tabName + "】不存在"); return; } } /* * HTable是线程不安全的 */ public static void addRecord(String tableName,String rowKey,String family,String qulifier,String value){ HTable hTable = null; try{ hTable = getHTable(tableName); Put put = new Put(Bytes.toBytes(rowKey)); put.add(Bytes.toBytes(family), Bytes.toBytes(qulifier), Bytes.toBytes(value)); hTable.put(put); System.out.println("新增记录:" + rowKey + "到表:【" + tableName + "】 info:【" + family + "," + value + "】"); }catch(IOException e){ e.printStackTrace(); } } public static void delRecord(String tableName,String rowKey){ HTable hTable = null; try{ hTable = getHTable(tableName); List list = new ArrayList(); Delete delete = new Delete(rowKey.getBytes()); list.add(delete); hTable.delete(list); System.out.println("删除记录:rowKey【" + rowKey + "】从表【" + tableName + "】成功"); }catch(IOException e){ System.out.println("删除记录:rowKey【" + rowKey + "】从表【" + tableName + "】失败" + e.getMessage()); } } @SuppressWarnings("deprecation") public static void getRecordByRowKey(String tableName,String rowKey){ try { HTable hTable = getHTable(tableName); Get get = new Get(rowKey.getBytes()); Result rs = hTable.get(get); StringBuilder sb = new StringBuilder(); for(KeyValue kv : rs.raw()){ sb.append("rowKey【" + changeByteToString(kv.getRow())+ "】 "); sb.append("Family【" + changeByteToString(kv.getFamily()) + "】"); sb.append("Qualifier【" + changeByteToString(kv.getQualifier()) + "】 "); sb.append("value【" + changeByteToString(kv.getValue()) + "】"); sb.append("\n"); } System.out.println("通过rowkey查询:\n{\n" + sb.toString() + "}\n"); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("deprecation") public static void getAllRecord(String tableName){ try{ HTable table = getHTable(tableName); Scan scan = new Scan(); ResultScanner rscan = table.getScanner(scan); StringBuilder sb = new StringBuilder(); for(Result rs : rscan){ for(KeyValue kv : rs.raw()){ sb.append("rowKey【" + changeByteToString(kv.getRow())+ "】 "); sb.append("Family【" + changeByteToString(kv.getFamily()) + "】"); sb.append("Qualifier【" + changeByteToString(kv.getQualifier()) + "】 "); sb.append("value【" + changeByteToString(kv.getValue()) + "】"); sb.append("\n"); } } System.out.println("通过表名查询:\n{\n" + sb.toString() + "}"); }catch(IOException e){ System.out.println("结果为【null】 " + e.getMessage()); } } }
试试其它关键字
HBase
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Jandison
贡献的其它代码
(
1
)
.
Java操作HBase示例
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3