hbase的单机搭建
2016-07-21 11:49
302 查看
最近准备搭建个单机版的hbase服务器,在apache上下载了hbase的压缩文件,解压后,配置conf文件夹里的三个配置文件,hbase-env.sh;hbase-site.xml;regionservers这三个文件。
hbase-env.sh;
export JAVA_HOME=/usr/local/java/jdk/
# File naming hosts on which HRegionServers will run. $HBASE_HOME/conf/regionservers by default.
#export HBASE_REGIONSERVERS=/usr/local/hbase/conf/regionservers
# File naming hosts on which backup HMaster will run. $HBASE_HOME/conf/backup-masters by default.
#export HBASE_BACKUP_MASTERS=/usr/local/hbase/conf/backup-masters
# Tell HBase whether it should manage it's own instance of Zookeeper or not.
export HBASE_MANAGES_ZK=true
hbase-site.xml;
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.rootdir</name>
<value>/home/hbase</value>
</property>
</configuration>
regionservers
localhost
在/etc/profile里添加PATH环境变量
export HBASE=/usr/local/hbase
export PATH=$HBASE/bin:$PATH
修改 etc/hosts
添加ip和域名映射
127.0.0.1 localhost
start-hbase.sh
stop-hbase.sh
java
package com.dao.cn;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class mgr {
public static Configuration configuration = null;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.master", "127.0.0.1:60010");
configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
}
public void InsertRowKey(String tablename) throws IOException
{
HTable table=new HTable(configuration, tablename);
Put put = new Put(Bytes.toBytes("row4"));
put.add(Bytes.toBytes("col1"), Bytes.toBytes("d"), Bytes.toBytes("1234569"));
table.put(put);
table.close();
}
public void selectRowKey(String tablename, String rowKey)
throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectRowKeyFamily(String tablename, String rowKey,
String family) throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
g.addFamily(Bytes.toBytes(family));
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectRowKeyFamilyColumn(String tablename, String rowKey,
String family, String column) throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
g.addColumn(family.getBytes(), column.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectFilter(String tablename, List<String> arr)
throws IOException {
HTable table = new HTable(configuration, tablename);
Scan scan = new Scan();// 实例化一个遍历器
FilterList filterList = new FilterList(); // 过滤器List
for (String v : arr) { // 下标0为列簇,1为列名,3为条件
String[] wheres = v.split(",");
filterList.addFilter(new SingleColumnValueFilter(// 过滤器
wheres[0].getBytes(), wheres[1].getBytes(),
CompareOp.EQUAL,// 各个条件之间是" and "的关系
wheres[2].getBytes()));
}
scan.setFilter(filterList);
ResultScanner ResultScannerFilterList = table.getScanner(scan);
for (Result rs = ResultScannerFilterList.next(); rs != null; rs = ResultScannerFilterList
.next()) {
for (KeyValue kv : rs.list()) {
System.out.println("--------------------"
+ new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: "
+ new String(kv.getFamily()));
System.out.println("Column :"
+ new String(kv.getQualifier()));
System.out.println("value : "
+ new String(kv.getValue()));
}
}
table.close();
}
}
hbase-env.sh;
export JAVA_HOME=/usr/local/java/jdk/
# File naming hosts on which HRegionServers will run. $HBASE_HOME/conf/regionservers by default.
#export HBASE_REGIONSERVERS=/usr/local/hbase/conf/regionservers
# File naming hosts on which backup HMaster will run. $HBASE_HOME/conf/backup-masters by default.
#export HBASE_BACKUP_MASTERS=/usr/local/hbase/conf/backup-masters
# Tell HBase whether it should manage it's own instance of Zookeeper or not.
export HBASE_MANAGES_ZK=true
hbase-site.xml;
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>hbase.rootdir</name>
<value>/home/hbase</value>
</property>
</configuration>
regionservers
localhost
在/etc/profile里添加PATH环境变量
export HBASE=/usr/local/hbase
export PATH=$HBASE/bin:$PATH
修改 etc/hosts
添加ip和域名映射
127.0.0.1 localhost
start-hbase.sh
stop-hbase.sh
java
package com.dao.cn;
import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class mgr {
public static Configuration configuration = null;
static {
configuration = HBaseConfiguration.create();
configuration.set("hbase.master", "127.0.0.1:60010");
configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
}
public void InsertRowKey(String tablename) throws IOException
{
HTable table=new HTable(configuration, tablename);
Put put = new Put(Bytes.toBytes("row4"));
put.add(Bytes.toBytes("col1"), Bytes.toBytes("d"), Bytes.toBytes("1234569"));
table.put(put);
table.close();
}
public void selectRowKey(String tablename, String rowKey)
throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectRowKeyFamily(String tablename, String rowKey,
String family) throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
g.addFamily(Bytes.toBytes(family));
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectRowKeyFamilyColumn(String tablename, String rowKey,
String family, String column) throws IOException {
HTable table = new HTable(configuration, tablename);
Get g = new Get(rowKey.getBytes());
g.addColumn(family.getBytes(), column.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.println("--------------------" + new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: " + new String(kv.getFamily()));
System.out
.println("Column :" + new String(kv.getQualifier()));
System.out.println("value : " + new String(kv.getValue()));
}
table.close();
}
public void selectFilter(String tablename, List<String> arr)
throws IOException {
HTable table = new HTable(configuration, tablename);
Scan scan = new Scan();// 实例化一个遍历器
FilterList filterList = new FilterList(); // 过滤器List
for (String v : arr) { // 下标0为列簇,1为列名,3为条件
String[] wheres = v.split(",");
filterList.addFilter(new SingleColumnValueFilter(// 过滤器
wheres[0].getBytes(), wheres[1].getBytes(),
CompareOp.EQUAL,// 各个条件之间是" and "的关系
wheres[2].getBytes()));
}
scan.setFilter(filterList);
ResultScanner ResultScannerFilterList = table.getScanner(scan);
for (Result rs = ResultScannerFilterList.next(); rs != null; rs = ResultScannerFilterList
.next()) {
for (KeyValue kv : rs.list()) {
System.out.println("--------------------"
+ new String(kv.getRow())
+ "----------------------------");
System.out.println("Column Family: "
+ new String(kv.getFamily()));
System.out.println("Column :"
+ new String(kv.getQualifier()));
System.out.println("value : "
+ new String(kv.getValue()));
}
}
table.close();
}
}
相关文章推荐
- mingw QT5.5.1转移到MSVC2010 QT5.5.1问题集锦
- MonoBehaviour常用方法
- 结构体内存对齐
- iOS开发 runtime实现原理以及实际开发中的应用
- 需要掌握的es6特性(二)
- Java 生成器 freemarker+rapid-generator 配置
- mongodb简单命令
- Heritrix 在 Eclipse 中的配置
- java https请求
- 计算机无法访问,您可能没有权限使用网络资源的解决方法
- 随机数的扩展
- innodB的隐式锁
- 微信随机生成红包金额算法php版
- Linux 下安装 jdk-7u75-linux-x64.gz,jdk1.7.0_75,jdk1.7步骤:
- 常见的HTTP状态码
- PHP + plupload.js JS插件实现多图上传并显示进度条加删除实例。
- 关于位运算的操作以及不同进制数之间的比较
- ant批量打包混淆修改资源文件
- 基于ucenter的单点登录原理
- web服务启动jvm参数简单配置