公司最近在搞一个hbase删除数据,由于在建表的时候是通过region来对每日的数据进行存储的,所以要求在删除的时候直接通过删除region的来删除数据
(最好的方案是只删除region中的数据,不把region删掉,但是百度了很久没找到只删除region中数据的解决方法,实在遗憾,最终也就通过删除region来删除数据了
这样的弊端是在hbase 中执行scan全表的时候 会报错,找不到某某region,只能通过rowkey来查询别的数据 真的很烦~~~ 以后有时间在来研究这个region吧)
  // 声明静态配置
static Configuration conf = null;
static {
conf = HBaseConfiguration.create();
//conf.set("hbase.zookeeper.quorum", "172.16.71.171");
conf.set("hbase.zookeeper.quorum", "com23.authentication,com22.authentication,com21.authentication");
conf.set("hbase.zookeeper.property.clientPort", "2181");
conf.set("hbase.zookeeper.quorum", "172.16.71.171");
conf.set("hbase.master", "172.16.71.171:600000");
conf.set("hbase.client.keyvalue.maxsize", "524288000");// 最大500m
conf.set("hbase.rootdir", "hdfs://h1.hadoop:8020/hbase");// 最大500m
}

 1 @SuppressWarnings("deprecation")
public static boolean deletByRegions(String tableName,String startKey) {
boolean flag = true;
Connection conn = null;
Table meta_table =null;
HTable table = null;
try {
conn = ConnectionFactory.createConnection(conf);
meta_table = conn.getTable(TableName.META_TABLE_NAME);
table = new HTable(conf, Bytes.toBytes(tableName)); //HTabel负责跟记录相关的操作如增删改查
HBaseAdmin admin = new HBaseAdmin(conf); //HBaseAdmin负责跟表相关的操作如create,drop等
HRegionInfo regionInfo = table.getRegionLocation(startKey).getRegionInfo();
String tableNameDataDir = "/data/default/" + tableName;
FileSystem fs = FileSystem.get(HdfsUtils.conn());
Path rootDir = new Path(conf.get("hbase.rootdir") + tableNameDataDir);
HRegionFileSystem.deleteRegionFromFileSystem(conf, fs, rootDir, regionInfo); String regionNameAsString = regionInfo.getRegionNameAsString();
HConnection connection = HConnectionManager.getConnection(conf);
//获取region的regionServerName
HRegionLocation locateRegion = connection.locateRegion(Bytes.toBytes(regionNameAsString));
ServerName serverName_temp = locateRegion.getServerName();
admin.closeRegion(serverName_temp,regionInfo);
List list = new ArrayList();
Delete d1 = new Delete(regionNameAsString.getBytes());
list.add(d1);
meta_table.delete(list);
meta_table.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
} finally {
if ( table != null) {
try {
table.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
}
}
return flag;
}

通过删除hbase表中的region来达到删除表中数据的更多相关文章

  1. 为SQL Server表中的列添加/修改/删除注释属性(sp_addextendedproperty、sp_updateextendedproperty、sp_dropextendedproperty)

    本篇基本完全参考:sql--sp_addextendedproperty和sp_updateextendedproperty (Transact-SQL) 三个存储过程用法一样,以sp_addexte ...

  2. VS中批量删除cs代码中的#region和#endregion

    Visual Studio中如何批量删除cs代码中的#region和#endregion,不删除它们中间的代码,只删除这两个标记及标记的注解的方法.Vs中提供了很强大的文本查找与替换功能,简单的替换只 ...

  3. 使用PreparedStatement向数据表中插入、修改、删除、获取Blob类型的数据

    使用PreparedStatement向数据表中插入.修改.删除.获取Blob类型的数据 2014-09-07 20:17 Blob介绍 BLOB类型的字段用于存储二进制数据 MySQL中,BLOB是 ...

  4. 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除。(C语言)

    /* 正整数构成的线性表存放在单链表中,编写算法将表中的所有的奇数删除 */ #include <stdio.h> #include <stdlib.h> typedef st ...

  5. [SQL] 如何在SQL Server2005数据库中检查一个表是否存在,如存在就删除表记录,如不存在就建表.

    . 检索 dbo.sysobjects表, select count(*) from dbo.sysobjects where xtype='U' and Name = '你的表名' . 根据返回的结 ...

  6. hbase操作(shell 命令,如建表,清空表,增删改查)以及 hbase表存储结构和原理

    两篇讲的不错文章 http://www.cnblogs.com/nexiyi/p/hbase_shell.html http://blog.csdn.net/u010967382/article/de ...

  7. hbase(一)region

    前言 文章不含源码,只是一些官方资料的整理和个人理解 架构总览 这张图在大街小巷里都能看到,感觉是hbase架构中最详细最清晰的一张,稍微再补充几点. 1) Hlog是低版本hbase术语,现在称为W ...

  8. HBase单个RegionServer的region数目上限

    前言 RegionServer维护Master分配给它的region,处理对这些region的IO请求,负责切分在运行过程中变得过大的region, 由于集群性能( 分配的内存和磁盘是有限的 )有限的 ...

  9. hbase 各个概念,region,storefile

    HBase中有两张特殊的Table,-ROOT-和.META. .META.:记录了用户表的Region信息,它可以有多高region(这的意思是说.META.表可以分 裂成多个region,和用户表 ...

随机推荐

  1. python配置文件读取

    在代码实现的过程中,我们经常选择将一些固定的参数值写入到一个单独的配置文件中.在python中读取配置文件官方提供了configParser方法. 主要有如下方法(找官文):   (这家伙很懒,直接复 ...

  2. this.$http & vue

    this.$http & vue https://github.com/pagekit/vue-resource Alias axios to Vue.prototype.$http http ...

  3. Android四大组件之Service(续2)

    1.HttpRequest package com.joyfulmath.android4example.http; import java.io.BufferedReader; import jav ...

  4. BZOJ4998 星球联盟(LCT+双连通分量+并查集)

    即要求动态维护边双.出现环时将路径上的点合并即可.LCT维护.具体地,加边成环时makeroot+access+splay一套把这段路径提出来,暴力dfs修改并查集祖先,并将这部分与根断开,视为删除这 ...

  5. CF521D Shop 贪心

    题意: \(n\)个数,有\(m\)个操作,形如: 1,将\(x_i\)​改成\(val_i\)​ 2,将\(x_i\)加上\(val_i\)​ 3,将\(x_i\)​乘上\(val_i\) 其中第\ ...

  6. BeanUtils介绍及其使用

    BeanUtils工具由Apache软件基金组织编写,提供给我们使用,主要解决的问题是:把对象的属性数据封装到对象中.在整个J2EE的编程过程中,我们经常会从各种配置文件中读取相应的数据,需要明白的一 ...

  7. elasticsearch 第四篇(API约定)

    对多个indices进行操作 es中大多resetapi支持请求多个index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 还可以使用+,-来包含 ...

  8. Last Position of Target

    For a given sorted array (ascending order) and a target number, find the first index of this number ...

  9. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  10. Hadoop1重新格式化HDFS

    注意:原来的数据全部被清空了.产生了一个新的hdfs 1. 删除已有目录 1.1 查看hdfs-site.xml 将 dfs.name.dir和dfs.data.dir所指定的目录删除 vim hdf ...