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);
} }
如果想要在本地成功运行上述的API Demo,必须满足如下几个条件:
1. 新建项目
本小节使用Intellij IDEA作为HBase的开发环境。安装好工具后需新建一个名为 hbase-test 的maven项
目,并在项目目录下的 ~/src/main/java/ 目录下将新建一个 HtableTest.java 文件,内容为上述
的API Demo。
2. 导入jar包
将上一章节中获取的jar包下载到本地,并将上一步新建的项目 hbase-test 与其建立依赖,也就是设定
新建项目 hbase-test 的 classpath ,用于API运行时查找jar包和配置文件。
3. 导入配置文件
若您要在本地进行开发还需要 hbase-site.xml 文件,将配置文件移入resources目录下。这个文件在集群中任意一台服务器上的
/etc/hbase/conf/ 目录下。
12. HBase API运行教程
本地的 hbase-site.xml 文件应放在上一步中与项目 hbase-test 建立了依赖的路径
下。
满足上述条件后,你就可以运行上述的API Demo了。

Hbase Java API包括协处理器统计行数的更多相关文章

  1. HBase 协处理器统计行数

    环境:cdh5.1.0 启用协处理器方法1. 启用协处理器 Aggregation(Enable Coprocessor Aggregation) 我们有两个方法:1.启动全局aggregation, ...

  2. Java关于条件判断练习--统计一个src文件下的所有.java文件内的代码行数(注释行、空白行不统计在内)

    要求:统计一个src文件下的所有.java文件内的代码行数(注释行.空白行不统计在内) 分析:先封装一个静态方法用于统计确定的.java文件的有效代码行数.使用字符缓冲流读取文件,首先判断是否是块注释 ...

  3. 《c程序设计语言》读书笔记--统计 行数、单词数、字符数

    #include <stdio.h> int main() { int lin = 0,wor = 0,cha = 0; int flag = 0; int c; while((c = g ...

  4. shell 统计行数

    语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...

  5. linux、WINDOWS命令行下查找和统计行数

    linux : 例子: netstat -an | grep TIME_WAIT | wc -l |  管道符 grep 查找命令 wc 统计命令 windows: 例子: netstat -an | ...

  6. wc 统计行数 字数

    Linux统计文件行数 2011-07-17 17:32 by 依水间, 168255 阅读, 4 评论, 收藏, 编辑 语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数. ...

  7. SQL Server遍历所有表统计行数

    DECLARE CountTableRecords CURSOR READ_ONLY FOR SELECT sst.name, Schema_name(sst.schema_id) FROM sys. ...

  8. Python,针对指定文件类型,过滤空行和注释,统计行数

    参考网络上代码编辑而成,无技术含量,可自行定制: 目前亲测有效,若有待完善之处,还望指出! 强调:将此统计py脚本放置项目的根目录下执行即可. 1.遍历文件,递归遍历文件夹中的所有 def getFi ...

  9. C++->10.3.2-3,使用文件流类录入数据,并统计行数

    题目:建立一个文本文件,从键盘录入一篇短文存放在该文件中短文由若干行构成,每行不超过80个字符,并统计行数. /* #include<iostream.h>#include<stdl ...

随机推荐

  1. 地图开发笔记(一):百度地图介绍、使用和Qt内嵌地图Demo

    前言   Qt在地图方面的研发.   百度地图 介绍   百度的地图分为多个开发,都是在线的(离线的需要自己提取,本篇解说在线地图).  百度地图JavaScript API支持HTTP和HTTPS, ...

  2. JMeter如何设置语言为中文

    一.现象 JMeter安装后,默认语言为英文,如下图所示: 对于英文水平一般的人来说,刚开始使用起来比较费劲(比如我),影响我工作效率.那么,怎么将英文改为中文呢? 二.解决方法 1.修改设置 点击菜 ...

  3. uber_go_guide解析(三)(规范)

    前言 一主要讲的是容易忽略的错误,可能在build时都不会体现出来但是在使用时出现问题 二主要讲的是一些可以提高代码效率的用法 本篇则讲解一些规范,不是强制的但是根据规范会提高代码的可读性, 减少BU ...

  4. node.js中使用http-proxy-middleware请求转发给其它服务器

    var express = require('express');var proxy = require('http-proxy-middleware'); var app = express(); ...

  5. PAT练习num3-跟奥巴马一起学编程

    美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝"计算机科学教育周"正式启动,奥巴马编写了很简单的计算机 ...

  6. 特斯拉Toolbox诊断检测仪工具Tesla诊断电脑 Tesla Toolbox

    Tesla特斯拉Toolbox诊断工具Tesla诊断电脑检测仪 Tesla Toolbox, Tesla Toolbox Diagnostic Tester.Language: English,Deu ...

  7. JavaScript中eval的替代方法

    引自:https://www.cnblogs.com/lxg0/p/7805266.html 通常我们在使用ajax获取到后台返回的json数据时,需要使用 eval 这个方法将json字符串转换成对 ...

  8. 3、wait和waitpid

    1. 函数介绍 wait函数:调用该函数使进程阻塞,直到任意一个子进程结束,或者该进程接收到了一个信号为止,如果该进程没有子进程或该进程的子进程已经结束,wait函数立即返回. waitpid函数:与 ...

  9. 01. struts2介绍

    struts2优点 与Servlet API 耦合性低.无侵入式设计 提供了拦截器,利用拦截器可以进行AOP编程,实现如权限拦截等功能 支持多种表现层技术,如:JSP.freeMarker.veloc ...

  10. circus reload

    circus reload Configuration - Circus 0.15.0 documentation https://circus.readthedocs.io/en/latest/fo ...