Hbase Java API包括协处理器统计行数
package com.zy;
import java.io.IOException; import org.apache.commons.lang.time.StopWatch;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
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.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.client.coprocessor.AggregationClient;
import org.apache.hadoop.hbase.client.coprocessor.LongColumnInterpreter;
import org.apache.hadoop.hbase.util.Bytes; public class HbaseTable {
// 声明静态配置
private static Configuration conf = HBaseConfiguration.create();
// 创建表(tableName 表名; family 列族列表)
public static void createTable(String tableName, String[] familys)
throws IOException{
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tableName)){
System.out.println(tableName+" already exists!");
}
else {
HTableDescriptor descr = new HTableDescriptor(TableName.valueOf(tableName));
for (String family:familys) {
descr.addFamily(new HColumnDescriptor(family)); //添加列族
}
admin.createTable(descr); //建表
System.out.println(tableName+" created successfully!");
}
}
//插入数据(rowKey rowKey;tableName 表名;family 列族;qualifier 限定名;value 值)
public static void addData(String tableName, String rowKey, String familyName, String
columnName, String value)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));//HTable负责跟记录相关的操作如增删改查等//
Put put = new Put(Bytes.toBytes(rowKey));// 设置rowkey
put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));
table.put(put);
System.out.println("Add data successfully!rowKey:"+rowKey+", column:"+familyName+":"+columnName+", cell:"+value);
}
//遍历查询hbase表(tableName 表名)
public static void getResultScann(String tableName) throws IOException {
Scan scan = new Scan();
ResultScanner rs = null;
HTable table = new HTable(conf, Bytes.toBytes(tableName));
try {
rs = table.getScanner(scan);
for (Result r : rs) {
for (KeyValue kv : r.list()) {
System.out.println("row:" + Bytes.toString(kv.getRow()));
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
}
} finally {
rs.close();
}
}
//查询表中的某一列(
public static void getResultByColumn(String tableName, String rowKey, String familyName, String
columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName)); //获取指定列族和列修饰符对应的列
Result result = table.get(get);
for (KeyValue kv : result.list()) {
System.out.println("family:" + Bytes.toString(kv.getFamily()));
System.out.println("qualifier:" + Bytes.toString(kv.getQualifier()));
System.out.println("value:" + Bytes.toString(kv.getValue()));
System.out.println("Timestamp:" + kv.getTimestamp());
System.out.println("-------------------------------------------");
}
}
//更新表中的某一列(tableName 表名;rowKey rowKey;familyName 列族名;columnName 列名;value 更新后的值)
public static void updateTable(String tableName, String rowKey,
String familyName, String columnName, String value)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(familyName), Bytes.toBytes(columnName),
Bytes.toBytes(value));
table.put(put);
System.out.println("update table Success!");
}
//删除指定单元格
public static void deleteColumn(String tableName, String rowKey,
String familyName, String columnName) throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteColumn = new Delete(Bytes.toBytes(rowKey));
deleteColumn.deleteColumns(Bytes.toBytes(familyName),
Bytes.toBytes(columnName));
table.delete(deleteColumn);
System.out.println("rowkey:"+rowKey+",column:"+familyName+":"+columnName+" deleted!");
}
//删除指定的行
public static void deleteAllColumn(String tableName, String rowKey)
throws IOException {
HTable table = new HTable(conf, Bytes.toBytes(tableName));
Delete deleteAll = new Delete(Bytes.toBytes(rowKey));
table.delete(deleteAll);
System.out.println("rowkey:"+rowKey+" are all deleted!");
}
//删除表(tableName 表名)
public static void deleteTable(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + " is deleted!");
} //统计行数
public void RowCount(String tablename) throws Exception,Throwable{
//提前创建conf
HBaseAdmin admin = new HBaseAdmin(conf);
TableName name=TableName.valueOf(tablename);
//先disable表,添加协处理器后再enable表
admin.disableTable(name);
HTableDescriptor descriptor = admin.getTableDescriptor(name);
String coprocessorClass = "org.apache.hadoop.hbase.coprocessor.AggregateImplementation";
if (! descriptor.hasCoprocessor(coprocessorClass)) {
descriptor.addCoprocessor(coprocessorClass);
}
admin.modifyTable(name, descriptor);
admin.enableTable(name); //计时
StopWatch stopWatch = new StopWatch();
stopWatch.start(); //提高RPC通信时长
conf.setLong("hbase.rpc.timeout", 600000);
//设置Scan缓存
conf.setLong("hbase.client.scanner.caching", 1000);
Configuration configuration = HBaseConfiguration.create(conf);
AggregationClient aggregationClient = new AggregationClient(configuration);
Scan scan = new Scan();
long rowCount = aggregationClient.rowCount(name, new LongColumnInterpreter(), scan);
System.out.println(" rowcount is " + rowCount);
System.out.println("统计耗时:"+stopWatch.getTime());
} public static void main(String[] args) throws Exception {
// 创建表
String tableName = "test";
String[] family = { "f1", "f2" };
createTable(tableName, family);
// 为表插入数据
String[] rowKey = {"r1", "r2"};
String[] columnName = { "c1", "c2", "c3" };
String[] value = {"value1", "value2", "value3", "value4", "value5", "value6",};
addData(tableName,rowKey[0],family[0],columnName[0],value[0]);
addData(tableName,rowKey[0],family[0],columnName[1],value[1]);
addData(tableName,rowKey[0],family[1],columnName[2],value[2]);
addData(tableName,rowKey[1],family[0],columnName[0],value[3]);
addData(tableName,rowKey[1],family[0],columnName[1],value[4]);
addData(tableName,rowKey[1],family[1],columnName[2],value[5]);
// 扫描整张表
getResultScann(tableName);
// 更新指定单元格的值
updateTable(tableName, rowKey[0], family[0], columnName[0], "update value");
// 查询刚更新的列的值
getResultByColumn(tableName, rowKey[0], family[0], columnName[0]);
// 删除一列
deleteColumn(tableName, rowKey[0], family[0], columnName[1]);
// 再次扫描全表
getResultScann(tableName);
// 删除整行数据
deleteAllColumn(tableName, rowKey[0]);
// 再次扫描全表
getResultScann(tableName);
// 删除表
deleteTable(tableName);
} }
Hbase Java API包括协处理器统计行数的更多相关文章
- HBase 协处理器统计行数
环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...
- Java关于条件判断练习--统计一个src文件下的所有.java文件内的代码行数(注释行、空白行不统计在内)
要求:统计一个src文件下的所有.java文件内的代码行数(注释行.空白行不统计在内) 分析:先封装一个静态方法用于统计确定的.java文件的有效代码行数.使用字符缓冲流读取文件,首先判断是否是块注释 ...
- 《c程序设计语言》读书笔记--统计 行数、单词数、字符数
#include <stdio.h> int main() { int lin = 0,wor = 0,cha = 0; int flag = 0; int c; while((c = g ...
- shell 统计行数
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- linux、WINDOWS命令行下查找和统计行数
linux : 例子: netstat -an | grep TIME_WAIT | wc -l | 管道符 grep 查找命令 wc 统计命令 windows: 例子: netstat -an | ...
- wc 统计行数 字数
Linux统计文件行数 2011-07-17 17:32 by 依水间, 168255 阅读, 4 评论, 收藏, 编辑 语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数. ...
- SQL Server遍历所有表统计行数
DECLARE CountTableRecords CURSOR READ_ONLY FOR SELECT sst.name, Schema_name(sst.schema_id) FROM sys. ...
- Python,针对指定文件类型,过滤空行和注释,统计行数
参考网络上代码编辑而成,无技术含量,可自行定制: 目前亲测有效,若有待完善之处,还望指出! 强调:将此统计py脚本放置项目的根目录下执行即可. 1.遍历文件,递归遍历文件夹中的所有 def getFi ...
- C++->10.3.2-3,使用文件流类录入数据,并统计行数
题目:建立一个文本文件,从键盘录入一篇短文存放在该文件中短文由若干行构成,每行不超过80个字符,并统计行数. /* #include<iostream.h>#include<stdl ...
随机推荐
- Python 日志打印之logging.config.dictConfig使用总结
日志打印之logging.config.dictConfig使用总结 By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #函数说明 logging.confi ...
- 在md里画流程图
可以使用名为mermaid的代码块,即 ```mermaid``` 需要md解析器能解析mermaid mermaid使用详情参见
- js如何替换字符串中匹配到多处中某一指定节点?
抛出一个问题,如图,搜索关键字,匹配到四处,那我鼠标放在第二处,我想把它变个颜色,该怎么实现呢?回到文章的标题,js如何替换字符串中匹配到多处中某一指定节点? 字符串的替换,我们首先想到的一个属性是r ...
- LeetCode234 回文链表
请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶:你能否用 O(n) 时间复杂 ...
- LeetCode278 第一个错误的版本
你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个版本 [1, ...
- Redis实战篇(一)搭建Redis实例
今天是Redis实战系列的第一讲,先从如何搭建一个Redis实例开始. 下面介绍如何在Docker.Windows.Linux下安装. Docker下安装 1.查看可用的 Redis 版本 访问 Re ...
- 检查Mysql主从状态
.检查MySQL主从同步状态 #!/bin/bash USER=bak PASSWD=123456 IO_SQL_STATUS=$(mysql -u$USER -p$PASSWD -e show s ...
- Android 中使用 config.gradle
各位同学大家好,当然了如果不是同学,那么大家也同好.哈哈. 大家知道config.gradle 是什么吗?我也不知道.开个完笑,其实config.gradle 就是我们为了统一gradle 中的各种配 ...
- Java中的深浅拷贝问题,你清楚吗?
一.前言 拷贝这个词想必大家都很熟悉,在工作中经常需要拷贝一份文件作为副本.拷贝的好处也很明显,相较于新建来说,可以节省很大的工作量.在Java中,同样存在拷贝这个概念,拷贝的意义也是可以节省创建对象 ...
- windows中使用django时报错:A server error occurred. Please contact the administrator.
这是因为在视图函数中使用了get函数,获取了不存在的数据例如:数据库中不存在一条name为hello1的数据,使用如下语句访问message = Message.objects.get(name='h ...