搭建hbase的环境,选择hbase的版本为0.92.0,解压hbase在{Hbase_Home}/lib文件夹下面可以看到这个habse对应的需要的hadoop的版本(hadoop-core-1.0.0.jar)和zookeeper的版本(zookeeper-core-3.4.2.jar).
1\搭建hadoop-1.0.0的环境
只要搭建过一次之后再次搭建就十分简单了,解压缩,配置那几个hdfs-site.xml hadoop-core.xml mapser-site.xml
最后就是启动的时候那个hadoop-env.sh 添加javahome的配置就可以了,最后配置master和salvas。
配置完成之后再来一个scp -r 的机器之间的copy。启动bin/start-all.sh
2\搭建zookeeper-3.4.2的环境
同样的不是很难网上教程很多,解压,把目录conf下的zoo-example.cfg 改为 zoo-cfg 然后去掉几个注释就可以了。
启动zookeeper . bin/zkServer.sh start bin/zkServer.sh status bin/zkServer.sh stop
3\搭建hadoop-0.92.0环境
解压进行简单的配置,网上的教程很多,进行配置,基于hadoop-1.0.0和zookeeper-3.4.2的配置
启动hbase bin/hbase bin/hbase shell list creare disable drop scan
4\在xp下就可以通过eclipse编程访问了(把hbase目录下的jar文件和lib目录下的jar文件拷贝的工程的目录并引入到工程里面)
========================================工具类========================================
package com.hbase.util;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
public class HBaseUtil {
private static Configuration configuration;
private static HBaseAdmin hBaseAdmin;
private static HTablePool hTablePool;
private static HTable hTable;
//singleton for lazy
private static HBaseUtil Instance=new HBaseUtil();
private HBaseUtil(){
try{
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.property.clientPort", "2181");
configuration.set("hbase.zookeeper.quorum", "pdhun,pdhum");
configuration.set("hbase.master", "pdhun:600000");
hBaseAdmin = new HBaseAdmin(configuration);
hTablePool= new HTablePool(configuration, 1000);
}catch(Exception e){
e.printStackTrace();
}
}
public static HBaseUtil getInstance(){
return Instance;
}
public HBaseAdmin getHBaseAdmin() {
return hBaseAdmin;
}
public HTablePool gethTablePool() {
return hTablePool;
}
public HTable gethTable(String tableName) {
try {
hTable=new HTable(configuration, tableName);
} catch (Exception e) {
e.printStackTrace();
}
return hTable;
}
}
===========================================创建表=====================================
package com.hbase.dao;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import com.hbase.util.HBaseUtil;
public class HBaseCreateTable {
public static void main(String[] args) {
createTableByName("t_user");
}
/**
* create table by tableName
* @param tableName
*/
public static void createTableByName(String tableName) {
System.out.println("----> begin create table "+tableName+" !");
HBaseAdmin hBaseAdmin=HBaseUtil.getInstance().getHBaseAdmin();
try {
//if the table is exist , first disable it then drop it
if (hBaseAdmin.tableExists(tableName)) {
hBaseAdmin.disableTable(tableName);//disable 'test'
hBaseAdmin.deleteTable(tableName); //drop 'test'
System.out.println("----> "+ tableName + " is exist,detele....");
}
//HTableDescriptor contains the details about an HBase table
//such as the descriptors of all the column families
HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
tableDescriptor.addFamily(new HColumnDescriptor("id"));
tableDescriptor.addFamily(new HColumnDescriptor("username"));
tableDescriptor.addFamily(new HColumnDescriptor("password"));
tableDescriptor.addFamily(new HColumnDescriptor("email"));
hBaseAdmin.createTable(tableDescriptor);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("----> create table "+tableName+" success !");
}
}
=======================================插入数据=========================================
package com.hbase.dao;
import java.io.IOException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import com.hbase.util.HBaseUtil;
public class HBaseInsertTable {
public static void main(String[] args) {
String tableName="t_user";
//Put object with the row to insert to and for each column to be inserted.
Put put = new Put("1".getBytes());
//add(byte[] family, byte[] qualifier, byte[] value)
put.add("id".getBytes(), null, "1".getBytes());
put.add("username".getBytes(), null, "admin".getBytes());
put.add("password".getBytes(), null, "111111".getBytes());
insertTableByNameData(tableName,put);
put = new Put("2".getBytes());
//add(byte[] family, byte[] qualifier, byte[] value)
put.add("id".getBytes(), null, "2".getBytes());
put.add("username".getBytes(), null, "guest".getBytes());
put.add("password".getBytes(), null, "donghui123".getBytes());
insertTableByNameData(tableName,put);
System.out.println("----> insert into table "+tableName+" two record success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static void insertTableByNameData(String tableName, Put put) {
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
try {
hTable.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
}
======================================查询所有==========================================
package com.hbase.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import com.hbase.entity.User;
import com.hbase.util.HBaseUtil;
public class HBasekQueryAll {
public static void main(String[] args) {
String tableName="t_user";
List<User> listuser=queryAllByTableName(tableName);
System.out.println(listuser+"\n\n----> queryAll from table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static List<User> queryAllByTableName(String tableName) {
List<User> listuser=new ArrayList<User>();
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
try {
ResultScanner rows = hTable.getScanner(new Scan());
User user=null;
for (Result row : rows) {
user=new User();
System.out.println("----> row-key:" + new String(row.getRow()));
for (KeyValue keyValue : row.raw()) {
String familly=new String(keyValue.getFamily());
String value=new String(keyValue.getValue());
if("id".equals(familly)){
user.setId(value);
}else if("username".equals(familly)){
user.setUsername(value);
}else if("password".equals(familly)){
user.setPassword(value);
}else if("email".equals(familly)){
user.setEmail(value);
}
}
listuser.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return listuser;
}
}
============================================主键查询====================================
package com.hbase.dao;
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.Result;
import com.hbase.entity.User;
import com.hbase.util.HBaseUtil;
public class HBaselQueryByRowKey {
public static void main(String[] args) {
String tableName="t_user";
String rowkey="1";
User user=queryByTableNameRowKey(tableName,rowkey);
System.out.println(user+"\n\n----> queryByRowkey from table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static User queryByTableNameRowKey(String tableName,String rowkey) {
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
User user=null;
try {
Get scan = new Get("1".getBytes());
Result row= hTable.get(scan);
user=new User();
for (KeyValue keyValue : row.raw()) {
String familly=new String(keyValue.getFamily());
String value=new String(keyValue.getValue());
if("id".equals(familly)){
user.setId(value);
}else if("username".equals(familly)){
user.setUsername(value);
}else if("password".equals(familly)){
user.setPassword(value);
}else if("email".equals(familly)){
user.setEmail(value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
=========================================rowkey主键查询=======================================
package com.hbase.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import com.hbase.entity.User;
import com.hbase.util.HBaseUtil;
public class HBasemQueryFilter {
public static void main(String[] args) {
String tableName="t_user";
List<User> listuser=queryAllByTableName(tableName);
System.out.println(listuser+"\n\n----> queryAll from table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static List<User> queryAllByTableName(String tableName) {
List<User> listuser=new ArrayList<User>();
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
try {
Filter filter = new SingleColumnValueFilter(Bytes.toBytes("username"), null, CompareOp.EQUAL, Bytes.toBytes("guest"));
ResultScanner rows = hTable.getScanner(new Scan().setFilter(filter));
User user=null;
for (Result row : rows) {
user=new User();
System.out.println("----> row-key:" + new String(row.getRow()));
for (KeyValue keyValue : row.raw()) {
String familly=new String(keyValue.getFamily());
String value=new String(keyValue.getValue());
if("id".equals(familly)){
user.setId(value);
}else if("username".equals(familly)){
user.setUsername(value);
}else if("password".equals(familly)){
user.setPassword(value);
}else if("email".equals(familly)){
user.setEmail(value);
}
}
listuser.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return listuser;
}
}
========================================组合条件查询========================================
package com.hbase.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
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.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.util.Bytes;
import com.hbase.entity.User;
import com.hbase.util.HBaseUtil;
public class HBasemQueryFilters {
public static void main(String[] args) {
String tableName="t_user";
List<User> listuser=queryAllByTableName(tableName);
System.out.println(listuser+"\n\n----> queryAll from table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static List<User> queryAllByTableName(String tableName) {
List<User> listuser=new ArrayList<User>();
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
try {
List<Filter> filters = new ArrayList<Filter>();
//usernamefilter
Filter usernamefilter = new SingleColumnValueFilter(Bytes.toBytes("username"), null, CompareOp.EQUAL, Bytes.toBytes("guest"));
filters.add(usernamefilter);
//emailfilter
Filter emailfilter = new SingleColumnValueFilter(Bytes.toBytes("email"), null, CompareOp.EQUAL, Bytes.toBytes("[email protected]"));
filters.add(emailfilter);
//package filters
FilterList filterList = new FilterList(filters);
ResultScanner rows = hTable.getScanner(new Scan().setFilter(filterList));
User user=null;
for (Result row : rows) {
user=new User();
System.out.println("----> row-key:" + new String(row.getRow()));
for (KeyValue keyValue : row.raw()) {
String familly=new String(keyValue.getFamily());
String value=new String(keyValue.getValue());
if("id".equals(familly)){
user.setId(value);
}else if("username".equals(familly)){
user.setUsername(value);
}else if("password".equals(familly)){
user.setPassword(value);
}else if("email".equals(familly)){
user.setEmail(value);
}
}
listuser.add(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return listuser;
}
}
=======================================根据rowkey删除=========================================
package com.hbase.dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import com.hbase.entity.User;
import com.hbase.util.HBaseUtil;
public class HBasenDeleteByRowKey {
public static void main(String[] args) {
String tableName="t_user";
String rowkey="2";
deleteByTableNameRowKey(tableName,rowkey);
System.out.println("\n\n----> deleteByTableNameRowKey from table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static User deleteByTableNameRowKey(String tableName,String rowkey) {
HTable hTable = HBaseUtil.getInstance().gethTable(tableName);
User user=null;
try {
List<Delete> list = new ArrayList<Delete>();
Delete delete = new Delete(rowkey.getBytes());
list.add(delete);
hTable.delete(list);
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
========================================删除表========================================
package com.hbase.dao;
import java.io.IOException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import com.hbase.util.HBaseUtil;
public class HBasepDeleteTable {
public static void main(String[] args) {
String tableName="t_user";
dropTableByName(tableName);
System.out.println("----> drop table "+tableName+" success !");
}
/**
* insert to table data
* @param tableName
* @param put
*/
private static void dropTableByName(String tableName) {
HBaseAdmin hBaseAdmin=HBaseUtil.getInstance().getHBaseAdmin();
try {
if (hBaseAdmin.tableExists(tableName)) {
hBaseAdmin.disableTable(tableName);//disable 'test'
hBaseAdmin.deleteTable(tableName); //drop 'test'
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
================================================================================