HBase应用
几个column family比较合适呢
几个column family比较合适呢
Table Schema的设计
RowKey的设计一
RowKey的设计二
RowKey的设计三
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.atomic.AtomicInteger;
- public class KeySalter {
- private AtomicInteger index = new AtomicInteger(0);
- private String[] prefixes = {"a", "b", "c", "d"};
- public String getRowKey(String originalKey) {
- StringBuilder sb = new StringBuilder(prefixes[index.incrementAndGet() % 4]);
- sb.append("-").append(originalKey);
- return sb.toString();
- }
- public List<String> getAllRowKeys(String originalKey) {
- List<String> allKeys = new ArrayList<>();
- for (String prefix : prefixes) {
- StringBuilder sb = new StringBuilder(prefix);
- sb.append("-").append(originalKey);
- allKeys.add(sb.toString());
- }
- //a-boo0001
- //b-boo0001
- //c-boo0001
- //d-boo0001
- return allKeys;
- }
- }
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.Connection;
- import org.apache.hadoop.hbase.client.ConnectionFactory;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Table;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class SaltingTest {
- public static void main(String[] args) throws IOException {
- Configuration config = HBaseConfiguration.create();
- try (Connection connection = ConnectionFactory.createConnection(config);
- Table table = connection.getTable(TableName.valueOf("test_salt"))) {
- KeySalter keySalter = new KeySalter();
- List<String> rowkeys = Arrays.asList("boo0001", "boo0002", "boo0003", "boo0004");
- List<Put> puts = new ArrayList<>();
- for (String key : rowkeys) {
- Put put = new Put(Bytes.toBytes(keySalter.getRowKey(key)));
- put.addColumn(Bytes.toBytes("f"), null, Bytes.toBytes("value" + key));
- puts.add(put);
- }
- table.put(puts);
- }
- }
- }
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class SaltingGetter {
- public static void main(String[] args) throws IOException {
- Configuration config = HBaseConfiguration.create();
- try (Connection connection = ConnectionFactory.createConnection(config);
- Table table = connection.getTable(TableName.valueOf("test_salt"))) {
- KeySalter keySalter = new KeySalter();
- List<String> allKeys = keySalter.getAllRowKeys("boo0001"); //读取boo001
- List<Get> gets = new ArrayList<>();
- for (String key : allKeys) {
- Get get = new Get(Bytes.toBytes(key));
- gets.add(get);
- }
- Result[] results = table.get(gets);
- for (Result result : results) {
- if (result != null) {
- //do something
- }
- }
- }
- }
- }
RowKey的设计三
- import org.apache.hadoop.hbase.util.MD5Hash;
- public class KeyHasher {
- public static String getRowKey(String originalKey) {
- return MD5Hash.getMD5AsHex(originalKey.getBytes());
- }
- }
- package com.twq.hbase.rowkey.hash;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.Connection;
- import org.apache.hadoop.hbase.client.ConnectionFactory;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Table;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class HashingTest {
- public static void main(String[] args) throws IOException {
- Configuration config = HBaseConfiguration.create();
- try (Connection connection = ConnectionFactory.createConnection(config);
- Table table = connection.getTable(TableName.valueOf("test_hash"))) {
- List<String> rowkeys = Arrays.asList("boo0001", "boo0002", "boo0003", "boo0004");
- List<Put> puts = new ArrayList<>();
- for (String key : rowkeys) {
- Put put = new Put(Bytes.toBytes(KeyHasher.getRowKey(key)));
- put.addColumn(Bytes.toBytes("f"), null, Bytes.toBytes("value" + key));
- puts.add(put);
- }
- table.put(puts);
- }
- }
- }
- import com.twq.hbase.rowkey.salt.KeySalter;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.hbase.Cell;
- import org.apache.hadoop.hbase.CellUtil;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class HashingGetter {
- public static void main(String[] args) throws IOException {
- Configuration config = HBaseConfiguration.create();
- try (Connection connection = ConnectionFactory.createConnection(config);
- Table table = connection.getTable(TableName.valueOf("test_hash"))) {
- Get get = new Get(Bytes.toBytes(KeyHasher.getRowKey("boo0001")));
- Result results = table.get(get);
- // process result...
- for (Cell cell : results.listCells()) {
- System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "===> " +
- Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
- Bytes.toString(CellUtil.cloneQualifier(cell)) + "{" +
- Bytes.toString(CellUtil.cloneValue(cell)) + "}");
- }
- }
- }
- }
RowKey的设计三

- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.hbase.Cell;
- import org.apache.hadoop.hbase.CellUtil;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.*;
- import org.apache.hadoop.hbase.filter.*;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.IOException;
- public class DataFilter {
- public static void main(String[] args) throws IOException {
- Configuration config = HBaseConfiguration.create();
- //Add any necessary configuration files (hbase-site.xml, core-site.xml)
- config.addResource(new Path("src/main/resources/hbase-site.xml"));
- config.addResource(new Path("src/main/resources/core-site.xml"));
- try(Connection connection = ConnectionFactory.createConnection(config)) {
- Table table = connection.getTable(TableName.valueOf("sound"));
- Scan scan = new Scan();
- scan.setStartRow(Bytes.toBytes("00000120120901"));
- scan.setStopRow(Bytes.toBytes("00000120121001"));
- SingleColumnValueFilter nameFilter = new SingleColumnValueFilter(Bytes.toBytes("f"), Bytes.toBytes("n"),
- CompareFilter.CompareOp.EQUAL, new SubstringComparator("中国好声音"));
- SingleColumnValueFilter categoryFilter = new SingleColumnValueFilter(Bytes.toBytes("f"), Bytes.toBytes("c"),
- CompareFilter.CompareOp.EQUAL, new SubstringComparator("综艺"));
- FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ALL);
- filterList.addFilter(nameFilter);
- filterList.addFilter(categoryFilter);
- scan.setFilter(filterList);
- ResultScanner rs = table.getScanner(scan);
- try {
- for (Result r = rs.next(); r != null; r = rs.next()) {
- // process result...
- for (Cell cell : r.listCells()) {
- System.out.println(Bytes.toString(CellUtil.cloneRow(cell)) + "===> " +
- Bytes.toString(CellUtil.cloneFamily(cell)) + ":" +
- Bytes.toString(CellUtil.cloneQualifier(cell)) + "{" +
- Bytes.toString(CellUtil.cloneValue(cell)) + "}");
- }
- }
- } finally {
- rs.close(); // always close the ResultScanner!
- }
- }
- }
- }
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.hbase.HBaseConfiguration;
- import org.apache.hadoop.hbase.TableName;
- import org.apache.hadoop.hbase.client.Connection;
- import org.apache.hadoop.hbase.client.ConnectionFactory;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Table;
- import org.apache.hadoop.hbase.util.Bytes;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * create 'sound',
- */
- public class DataPrepare {
- public static void main(String[] args) throws IOException {
- InputStream ins = DataPrepare.class.getClassLoader().getResourceAsStream("sound.txt");
- BufferedReader br = new BufferedReader(new InputStreamReader(ins));
- List<SoundInfo> soundInfos = new ArrayList<>();
- String line = null;
- while ((line = br.readLine()) != null) {
- SoundInfo soundInfo = new SoundInfo();
- String[] arr = line.split("\\|");
- String rowkey = format(arr[4], 6) + arr[1] + format(arr[0], 6);
- soundInfo.setRowkey(rowkey);
- soundInfo.setName(arr[2]);
- soundInfo.setCategory(arr[3]);
- soundInfos.add(soundInfo);
- }
- Configuration config = HBaseConfiguration.create();
- //Add any necessary configuration files (hbase-site.xml, core-site.xml)
- config.addResource(new Path("src/main/resources/hbase-site.xml"));
- config.addResource(new Path("src/main/resources/core-site.xml"));
- try (Connection connection = ConnectionFactory.createConnection(config)) {
- Table table = connection.getTable(TableName.valueOf("sound"));
- List<Put> puts = new ArrayList<>();
- for (SoundInfo soundInfo : soundInfos) {
- Put put = new Put(Bytes.toBytes(soundInfo.getRowkey()));
- put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("n"), Bytes.toBytes(soundInfo.getName()));
- put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("c"), Bytes.toBytes(soundInfo.getCategory()));
- puts.add(put);
- }
- table.put(puts);
- }
- }
- public static String format(String str, int num) {
- return String.format("%0" + num + "d", Integer.parseInt(str));
- }
- }
HBase应用的更多相关文章
- Mapreduce的文件和hbase共同输入
Mapreduce的文件和hbase共同输入 package duogemap; import java.io.IOException; import org.apache.hadoop.co ...
- Redis/HBase/Tair比较
KV系统对比表 对比维度 Redis Redis Cluster Medis Hbase Tair 访问模式 支持Value大小 理论上不超过1GB(建议不超过1MB) 理论上可配置(默认配置1 ...
- Hbase的伪分布式安装
Hbase安装模式介绍 单机模式 1> Hbase不使用HDFS,仅使用本地文件系统 2> ZooKeeper与Hbase运行在同一个JVM中 分布式模式– 伪分布式模式1> 所有进 ...
- Spark踩坑记——数据库(Hbase+Mysql)
[TOC] 前言 在使用Spark Streaming的过程中对于计算产生结果的进行持久化时,我们往往需要操作数据库,去统计或者改变一些值.最近一个实时消费者处理任务,在使用spark streami ...
- Spark读写Hbase的二种方式对比
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 一.传统方式 这种方式就是常用的TableInputFormat和TableOutputForm ...
- 深入学习HBase架构原理
HBase定义 HBase 是一个高可靠.高性能.面向列.可伸缩的分布式存储系统,利用Hbase技术可在廉价PC Server上搭建 大规模结构化存储集群. HBase 是Google Bigtabl ...
- hbase协处理器编码实例
Observer协处理器通常在一个特定的事件(诸如Get或Put)之前或之后发生,相当于RDBMS中的触发器.Endpoint协处理器则类似于RDBMS中的存储过程,因为它可以让你在RegionSer ...
- hbase集群安装与部署
1.相关环境 centos7 hadoop2.6.5 zookeeper3.4.9 jdk1.8 hbase1.2.4 本篇文章仅涉及hbase集群的搭建,关于hadoop与zookeeper的相关部 ...
- 从零自学Hadoop(22):HBase协处理器
阅读目录 序 介绍 Observer操作 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Sour ...
- Hbase安装和错误
集群规划情况: djt1 active Hmaster djt2 standby Hmaster djt3 HRegionServer 搭建步骤: 第一步:配置conf/regionservers d ...
随机推荐
- dd命令的使用
1.dd命令的使用 dd命令用于复制文件并对源文件的内容进行转换和格式化处理,在有需要的时候可以使用dd命令对物理磁盘进行操作,使用dd对磁盘操作时,最好使用块设备文件. (1)命令语法 dd (选项 ...
- [转帖]Java升级那么快,多个版本如何灵活切换和管理?
Java升级那么快,多个版本如何灵活切换和管理? https://segmentfault.com/a/1190000021037771 前言 近两年,Java 版本升级频繁,感觉刚刚掌握 Java8 ...
- MNIST机器学习入门(二)
在前一个博客中,我们已经对MNIST 数据集和TensorFlow 中MNIST 数据集的载入有了基本的了解.本节将真正以TensorFlow 为工具,写一个手写体数字识别程序,使用的机器学习方法是S ...
- C#月份和日期转大写和C#集合分组
//日转化为大写 private static string DaytoUpper(int day, string type) { if (day < 20) { return MonthtoU ...
- mysql中length与char_length字符长度函数使用方法
在mysql中length是计算字段的长度一个汉字是算三个字符,一个数字或字母算一个字符了,与char_length是有一点区别,本文章重点介绍第一个函数. mysql里面的length函数是一个用来 ...
- Win10家庭版升级到企业版的方法
一.家庭版升级企业版 1.右键单击[此电脑]——>属性 2.点击更改产品密钥 3.输入密钥:NPPR9-FWDCX-D2C8J-H872K-2YT43 4.点击下一步,验证结束后点击开始升级,然 ...
- jsonpath_rw操作json
from jsonpath_rw import parse def get_key_from_data(key,data): # 定义匹配规则 json_expr=parse(key) result= ...
- 纯css实现省略号,兼容火狐,IE9,chrome
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 激活windows去掉右下角水印
激活windows去掉右下角水印 //需要隔一段时间执行一次 // 卸载已有的激活产品slmgr.vbs /upk // 重新按照激活产品slmgr /ipk NPPR9-FWDCX-D2C8J-H ...
- java实现在线预览--poi实现word、excel、ppt转html
java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...