使用java api 访问 hbase (三)

单机环境(非集群):
linux fedora14 64位
java 1.8
hbase-1.1.2   下载地址:http://archive.apache.org/dist/hbase/1.1.2/hbase-1.1.2-bin.tar.gz  如果是src的就需要编译
下载好直接解压 tar xzvf hbase-1.1.2-bin.tar.gz

一,hbase 配置 conf/hbase-site.xml
    不依赖于 hdfs ,想使用本地文件系统存储数据, 配置中必须有 hbase.unsafe.stream.capability.enforce 设置 false





	
		hbase.rootdir
		file:///home/hbase/hbasedir
	
	
		hbase.zookeeper.quorum
		192.168.1.36
	
	
		hbase.zookeeper.property.dataDir
		/home/hbase/zookeeper
	
	
		hbase.unsafe.stream.capability.enforce
		false
		
			Controls whether HBase will check for stream capabilities (hflush/hsync).

			Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
			with the 'file://' scheme, but be mindful of the NOTE below.

			WARNING: Setting this to false blinds you to potential data loss and
			inconsistent system state in the event of process and/or node failures. If
			HBase is complaining of an inability to use hsync or hflush it's most
			likely not a false positive.
		
	

 

二,修改主机名称:/etc/sysconfig/network:


三,修改主机/etc/hosts:
添加一行 你的ip master
 
重启机器才会生效,可以使用 hostname master  来修改,可以不重启机器,当前shell有效
四,启动hbase:
hbase目录 下 bin/start-hbase.sh

浏览器访问看是否成功:

java 客户端pom.xml:


	        org.apache.hbase
	        hbase-client
	        1.1.2
	    
	   
	     
            org.apache.hadoop
            hadoop-hdfs
            2.5.1
        

客户端机器host 设置 (Windows )  C:\Windows\System32\Drivers\etc\hosts 添加一行
192.168.1.36    master

客户端代码:

package com.hull.hbase.test;

import java.io.IOException;

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.Get;
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.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class AppStarter {
	public static void main(String[] args) throws IOException {

		System.setProperty("hadoop.home.dir", "D:\\hadoop\\hadoop-2.5.1");
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.property.clientPort", "2181");
		conf.set("hbase.zookeeper.quorum", "192.168.1.36");
		conf.set("zookeeper.znode.parent", "/hbase");

		Connection connection = ConnectionFactory.createConnection(conf);
		try {

			Table table = connection.getTable(TableName.valueOf("user"));
			try {
				Put p = new Put(Bytes.toBytes("u003"));
				p.add(Bytes.toBytes("cf"), Bytes.toBytes("name"),
						Bytes.toBytes("小试牛刀"));
				table.put(p);
				Get g = new Get(Bytes.toBytes("u003"));
				Result r = table.get(g);
				byte[] value = r.getValue(Bytes.toBytes("cf"),
						Bytes.toBytes("name"));
				String valueStr = Bytes.toString(value);
				System.out.println("GET: " + valueStr);
				Scan s = new Scan();
				s.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"));
				ResultScanner scanner = table.getScanner(s);
				try {
					for (Result rr = scanner.next(); rr != null; rr = scanner
							.next()) {

						System.out.println("Found row: " + rr);
					}

				} finally {
					scanner.close();
				}

			} finally {
				if (table != null)
					table.close();
			}
		} finally {
			connection.close();
		}
	}
}

//本地要下载 hadoop-2.5.1 
        //若提示缺少 winutils.exe  需要去这里下载  https://github.com/srccodes/hadoop-common-2.2.0-bin
        //解压后将   winutils.exe  hadoop.dll 放入hadoop目录的bin目录中

编译运行:

问题总结:
1,hbase 0.9.x  启动后,zookeeper中 原表为  root-region-server, hbase-1.x.x 为 meat-region-server
2,zookeeper 默认为hbase自己内置,自己管理启动与停止,无需独立下载运行,也可独立部署
3,hbase-client 为开发机  hbase 单机部署在另一台 linux上, Windows 需要通过 hosts 文件配置映射  ip  master
4,配置中的所有ip配置不要使用 127.0.0.1 尽量使用机器真实对外IP,以免造成hbase-client客户端不能访问 
5,jdk.tools.jar 缺少,项目不能编译生成class  从安装目录 拷贝一个Java\jdk1.8.0_91\lib\tools.jar  放入maven 依赖jar的目录,重命名为 jdk.tools.jar
 

你可能感兴趣的:(HBase)