hbase的单机搭建

最近准备搭建个单机版的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;




 
    hbase.rootdir
    /home/hbase
 


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 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,大数据时代)