一、概念
很多收集统计信息的应用,例如在线广告的单击或查看统计,将这些数据收集到日志文件中用于后期的分析。
利用计数器提供的实时统计,从而放弃延时较高的批处理操作。
二、数据操作
3.java操作
public class HbaseDemo {
private static Configuration conf = HBaseConfiguration.create();
private static Admin admin;
static {
conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2,hadoop3");
try {
Connection connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Counter() throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Table table = connection.getTable(TableName.valueOf("counters"));
System.out.println("单计数器自增");
// 计数器+1
long cnt1 = table.incrementColumnValue("20110102".getBytes(),"daily".getBytes(),"hits".getBytes(),1);
long cnt2 = table.incrementColumnValue("20110102".getBytes(),"daily".getBytes(),"hits".getBytes(),1);
// 获取当前计数器的值
long current = table.incrementColumnValue("20110102".getBytes(),"daily".getBytes(),"hits".getBytes(),0);
// 计数器-1
long cnt3 = table.incrementColumnValue("20110102".getBytes(),"daily".getBytes(),"hits".getBytes(),-1);
System.out.println(cnt1);
System.out.println(cnt2);
System.out.println(current);
System.out.println(cnt3);
System.out.println("增加一行中的多个计数器的计数");
Increment increment1 = new Increment("20110104".getBytes());
increment1.addColumn("daily".getBytes(),"click".getBytes(),1);
increment1.addColumn("daily".getBytes(),"hits".getBytes(),1);
increment1.addColumn("daily".getBytes(),"click".getBytes(),10);
increment1.addColumn("daily".getBytes(),"hits".getBytes(),10);
Result result1 = table.increment(increment1);
for (KeyValue keyValue:result1.raw()){
System.out.println("KV: "+keyValue+" Value: "+ Bytes.toLong(keyValue.getValue()));
}
}
public static void main(String[] args) throws IOException {
HbaseDemo hbaseDemo = new HbaseDemo();
hbaseDemo.Counter();
}
}