概览

1.导入jar包
2.测试
3.异常处理

首先将HBase搭建完成,然后启动Zookeeper,Hadoop,HBase集群

1.导入jar包

准备:
1.CentOS7
2.Zookeeper集群
3.Hadoop2.7.3集群
4.hbase2.0.0集群
5.eclipse

在eclipse中建个java项目,项目中新建个lib文件夹用来存放jar包
将hbase目录下的lib下的所有jar包导入到项目中
Java代码实现对HBase的基本操作
Java代码实现对HBase的基本操作

2.测试

然后建个包,建个TestHbase类

package com.hd.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestHbase {
	private Configuration conf = null;
	private Connection conn = null;
	private Admin admin = null;
	private Table table = null;
	
	@Before
	public void createConf() throws IOException{
		//解决异常,可以不用
		System.setProperty("hadoop.home.dir","D:\\software\\01-软件资料\\hadoop-common-2.2.0-bin-master");
		conf = HBaseConfiguration.create();
		//设置zk集群地址,这里需要修改windows下的hosts文件
		conf.set("hbase.zookeeper.quorum","master:2181,slave1:2181,slave2:2181");
		//建立连接
		conn = ConnectionFactory.createConnection(conf);
	}
	

	@Test
	public void createTable() throws IOException{
		//获取表管理类
		admin = conn.getAdmin();
		//定义表
		HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("person"));
		//定义列族
		HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("info");
		//将列族添加到表中
		hTableDescriptor.addFamily(hColumnDescriptor);
		//执行建表操作
		admin.createTable(hTableDescriptor);
	
	}
	@Test
	public void put() throws IOException{

		//获取表对象
		table = conn.getTable(TableName.valueOf("person"));
		//创建put对象
		Put put = new Put("p1".getBytes());
		//添加列
		put.addColumn("info".getBytes(), "name".getBytes(), "haha".getBytes());
		//向表格中添加put对象
		table.put(put);
		
	}
	@Test
	public void get() throws IOException{
		//获取表对象
		table = conn.getTable(TableName.valueOf("person"));
		//用行键实例化get
		Get get = new Get("p1".getBytes());
		//增加列族名和列名条件
		get.addColumn("info".getBytes(), "name".getBytes());
		//执行,返回结果
		Result result = table.get(get);
		//取出结果
		String valStr = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
		System.out.println(valStr);
		
	}
	@Test
	public void scan() throws IOException{
		//获取表对象
		table = conn.getTable(TableName.valueOf("person"));
		//初始化scan示例
		Scan scan = new Scan();
		//增加过滤条件
		scan.addColumn("info".getBytes(), "name".getBytes());
		//返回结果
		ResultScanner rss = table.getScanner(scan);
		//迭代取出结果
		for (Result result : rss) {
			String valStr = Bytes.toString(result.getValue("info".getBytes(), "name".getBytes()));
			System.out.println(valStr);
		}
		
	}
	@Test
	public void del() throws IOException{
		//获取表对象
		table = conn.getTable(TableName.valueOf("person"));
		//用行键实例化Delete实例
		Delete del = new Delete("p1".getBytes());
		//执行删除
		table.delete(del);
	}
	@After
	public void close() throws IOException{
		//关闭连接
		if(admin!=null){
			admin.close();
		}
		if(table!=null){
			table.close();
		}
		if(conn!=null){
			conn.close();
		}		
	}
	
}

修改Windows下的hosts文件
找到这个路径C:\Windows\System32\drivers\etc
在hosts文件最后添加你的hbase集群的主机ip和主机名
Java代码实现对HBase的基本操作

然后分别双击方法名,右键run Junit测试每个方法

3.异常处理

其中

	System.setProperty("hadoop.home.dir","D:\\software\\01-软件资料\\hadoop-common-2.2.0-bin-master");

是为了解决找不到bin目录下winutils.exe文件的异常

2018-10-26 19:19:10,309 ERROR [main] util.Shell (Shell.java:getWinUtilsPath(400)) - Failed to locate the winutils binary in the hadoop binary path
java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
	at org.apache.hadoop.util.Shell.getQualifiedBinPath(Shell.java:382)
	at org.apache.hadoop.util.Shell.getWinUtilsPath(Shell.java:397)
	at org.apache.hadoop.util.Shell.<clinit>(Shell.java:390)
	at org.apache.hadoop.util.StringUtils.<clinit>(StringUtils.java:80)

其实这个异常就算不处理也是可以正常连接hbase操作的,但是最好还是要解决掉,因为我采用的hadoop2.7.3下的bin目录是没有这个文件的,所以我上网上找了一份hadoop2.2.0版本的,链接:https://pan.baidu.com/s/1koyq-8D5Z7u88DBtHGNe7g
提取码:4ut9

这个解压后只有bin目录将其路径填入System.setProperty()中即可.

相关文章:

  • 2021-12-01
  • 2022-12-23
  • 2021-11-23
  • 2021-10-07
  • 2021-09-11
  • 2021-11-18
  • 2021-11-27
猜你喜欢
  • 2021-12-12
  • 2022-01-18
  • 2022-01-12
  • 2021-11-29
  • 2021-08-21
  • 2021-10-06
  • 2021-08-01
相关资源
相似解决方案