分布式安装
首先修改配置文件
命令——vi hbase-site.xml
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://mycluster/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node01,node02node03</value>
</property>
</configuration>conf/regionservers file
node-a.example.com
node-b.example.com
node-c.example.comconf/backup-masters file 备机
node-b.example.com
node-c.example.com首先启动hdfs,并连接,有三种方式
a. Add a pointer to your HADOOP_CONF_DIR to the HBASE_CLASSPATH environment variable in hbaseenv.sh.
b. Add a copy of hdfs-site.xml (or hadoop-site.xml) or, better, symlinks, under ${HBASE_HOME}/conf, or
c. if only a small set of HDFS client configurations, add them to hbase-site.xml.
1.配置hosts 在/opt/sxt/hbase/conf vi regionservers
2.vi backup-masters 制定备机
3.vi hbase-site.xml
4.vi hbase-env.sh防止伪和分的zookeeper冲突
5.拷贝到当前目录下 cp /opt/hadoop-2.6.5/etc/hadoop/hdfs-site.xml ./
单点完成
6.分发
在 /opt/sxt 下 scp -r habse/ node01:‘pwd’
在 /opt/sxt 下 scp -r habse/ node02:‘pwd’
在 /opt/sxt 下 scp -r habse/ node03:‘pwd’
7.配置环境变量
8依赖hdfs和zookeeper 所以优先开
通过eclips操作HBase
首先创建一个java project
需要一些jar包 包的来源于hbase-0.98.12.1-hadoop2下的lib
需要hb开头到htrac netty 通讯的协议
build path --》 JUnit 4 hbase315 hdoop
java端操作
创建于hbase数据库的连接
创建表
录入数据
显示数据
public class Demohb {
HBaseAdmin hba ;
HTable htable;
String tableName = "phone";
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
Configuration conf = new Configuration();//配置信息shezhi
conf.set("hbase.zookeeper.quorum", "node02,node03,node04");
hba = new HBaseAdmin(conf);
htable = new HTable(conf,tableName);
}
@Test//创建表格
public void createTable() throws IOException{
//给定创建表的表名称
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
//设定列族
HColumnDescriptor family = new HColumnDescriptor("cf");
family.setBlockCacheEnabled(true);//设置读缓存
family.setInMemory(true);//读缓存放在内存
desc.addFamily(family);
hba.createTable(desc);
}
//录入数据
public void insertDB() throws RetriesExhaustedWithDetailsException, InterruptedIOException{
byte[] rk = "111".getBytes();
Put put =new Put(rk);
put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "li si".getBytes());
put.add("cf".getBytes(), "name".getBytes(), "wang wu".getBytes());
htable.put(put);
}
//查询
public void getDB() throws IOException{
Get get = new Get("111".getBytes());;
get.addColumn("cf".getBytes(), "name".getBytes());
Result result = htable.get(get);
Cell cell = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
byte[] vlaue = cell.getValue();
System.out.println("姓名"+new String(vlaue));
}//scan
public void scan() throws IOException{
Scan scan = new Scan();
ResultScanner result = htable.getScanner(scan );
for(Result rs: result){
Cell cellname = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell cellage = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell cellsex = rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
byte[] name = CellUtil.cloneValue(cellname);
byte[] age= CellUtil.cloneValue(cellage);
byte[] sex = CellUtil.cloneValue(cellsex);
System.out.println(Bytes.toString(name)+Bytes.toString(age)+Bytes.toString(sex));
}
}
public void scan() throws IOException{
Scan scan = new Scan();
ResultScanner result = htable.getScanner(scan );
for(Result rs: result){
Cell cellname = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());
Cell cellage = rs.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell cellsex = rs.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
byte[] name = CellUtil.cloneValue(cellname);
byte[] age= CellUtil.cloneValue(cellage);
byte[] sex = CellUtil.cloneValue(cellsex);
System.out.println(Bytes.toString(name)+Bytes.toString(age)+Bytes.toString(sex));
}
}
//
@After
public void close(){
if(null!=hba){
try {
hba.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null!=hba){
try {
hba.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}//过滤
//具有连续性
scan.setStartRow(startRow);
scan.setStopRow(stopRow);
查询最近三个月的通话记录/账单
//需求 录入10个用户1年来1000条通话记录(每个用户100)
//通话记录的信息:通话时间 时长 主叫号码 被叫号码 主备叫类型
habse的设计
rk:按照查询需求设计 电话号码、时间
列族: 无所谓 cf
列 :通话时间 主叫号码 被叫号码 主备叫类型
rk的有序 但是需要反序, 用大数-通话时间
public void insert(){
String phone =
for(int i=0;i<100; i++){
}
}
不能使用where子句
FilterList 过滤链
PrefixFilter 前缀过滤器表设计
1、人员-角色
人员有多个角色 角色优先级
角色有多个人员
人员 删除添加角色
角色 可以添加删除人员
人员 角色 删除添加
小明:体育委员 > 劳动委员
小强:劳动委员 > 体育委员
小红:学习委员
体育文员: 小明 小强
劳动委员: 小明 小强
。。。。。
问题:几张表搞定
三张
人员表:psn
rowkey:pid cf info cf2 role
101 cf:name=小明 cf2:1=10;cf2:2 =9
cf:age=20
102 cf2:2=10;cf2:1 =9
103 cf2:3
角色表 role
rowkey:rid cf info cf2 psn
1 cf:name=体育委员 cf2:101; cf2:102
2
3
出现的问题:无法避免全表扫描,加强列之间的关联
人员角色关联表
rowkey pid-rid cf :priority 优先级
101-1 cf : prior=10
101-2 cf : prior=9
102-1 cf : prior=10
102-2 cf : prior=9
2、组织架构 部门-子部门
查询 顶级部门 通过rowkey,避免全量扫描,并且rowkey是连续的
查询 每个部门的所有子部门
部门 添加、删除子部门
部门 添加、删除
rk cf1 baseinfo cf2:子部门
0_101 老板部门 cf2:102;103;104
1_102 开发部门 cf2:105;106;107
1_103 财务部门
1_104 销售部门
1_105 java
1_106 pyphon
1_107 大数据
Hbase优化
下载grouplist
(下载并按组展示软件的列表)
yum grouplist
展示安装软件
yum groupinstall
想看某软件信息:
yum info softwarename
想看某组软件信息:
yum groupinfo softwarename
比如:
yum groupinfo Development tools
想看都有哪些组的信息:
yum grouplist
安装某个组的软件:
yum groupinstall groupname
比如:
yum groupinstall Development tools安装proto
tar -xf protxxx
cd home
执行 : ./configure --prefix=$INSTALL_DIR (选定安装目录)
make
make check
make install
or
make && make installprotobuf封装
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
高级应用
==============
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
optional string email = 3;
repeated PhoneNumber phone = 4;
message PhoneNumber {
required string number = 1;
}
}
我们自己的测试案例
==========================
package com.bjsxt.hb.test;
message dayPhoneList
{
repeated phoneDetail phone=1;
}
message phoneDetail
{
required string number= 1;
required string length= 2;
required string flag= 3;
required string date= 4;
}文档
HBase跟MapReduce的整合
public class Wreducer extends TableReducer<Text, IntWritable, ImmutableBytesWritable>{
//tkey: 单词
//vals:
@Override
protected void reduce(Text tkey, Iterable<IntWritable> vals,
Context context)
throws IOException, InterruptedException {
int sum = 0;//记录当前单词的总数
for (IntWritable val : vals) {
sum +=val.get();
}
Put put = new Put(tkey.getBytes());
put.add("cf".getBytes(), "count".getBytes(), (sum+"").getBytes());
context.write(null, put);
}
}
public class Wmapper extends Mapper<LongWritable, Text, Text, IntWritable>{
Text tkey = new Text();
IntWritable tval = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String word : words) {
tkey.set(word);
// tval.set(1);
context.write(tkey , tval );
}
}
}
public class WordCount {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
//zk
conf.set("hbase.zookeeper.quorum", "node01,node02,node03");
Job job = Job.getInstance(conf);
Path inPath = new Path("hdfs://node01:8020/wc/input");
//设置文件输入源1
FileInputFormat.addInputPath(job, inPath);
job.setMapperClass(Wmapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
TableMapReduceUtil.initTableReducerJob("wc", Wreducer.class, job);
boolean flag = job.waitForCompletion(true);
System.out.println(flag==true?"提交成功":"提交失败");
}
}HBase的优化
三个方面来优化:有详细技术文档
表设计
写表操作
读表操作
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3167441/ ,如需转载,请注明出处,否则将追究法律责任。