java操作Hbase

1.下载hadoop,本文以hadoop-2.7.6为例
安装教程参考:https://blog.csdn.net/csdn_fzs/article/details/78985586
在这里插入图片描述
2.windows 搭建 hbase
https://blog.csdn.net/wm6752062/article/details/80381704
启动:bin \ start-hbase.cmd
进入hbaseshell脚本:bin目录cmd hbase shell
list:查看所有表
在这里插入图片描述
scan’表名’:查看该表数据、数据结构对比(左hbase,右边mysql)
在这里插入图片描述
代码
jar包是Hbase下lib目录导入到eclipse项目中lib下

package com.fh.MyHBase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.client.*;

/**
 * hbase操作 创建表 1.通过HBaseConfiguration.create() :获取配置 conf 2.conf.set()
 * :设置zk等参数(kerberos认证等) 3.ConnectionFactory.createConnection(configuration)
 * :获取连接conn 4.通过conn.getAdmin()来获取Admin :表相关操作的类 (HBaseAdmin已过期)
 * 5.创建TableName:描述表名称的 : TableName tname = TableName.valueOf(tablename);
 * 6.创建表描述信息类: HTableDescriptor tDescriptor = new HTableDescriptor(tname);
 * 7.添加表列簇描述信息类:HColumnDescriptor famliy = new HColumnDescriptor(cf);
 * 8.将表列簇描述信息类添加到表描述信息类:tDescriptor.addFamily(famliy);
 * 9.调用admin创建表:admin.createTable(tDescriptor);
 * 
 * hbase操作 添加数据 1.通过HBaseConfiguration.create() :获取配置 conf 2.conf.set()
 * :设置zk等参数(kerberos认证等) 3.ConnectionFactory.createConnection(configuration)
 * :获取连接conn 4.创建TableName:描述表名称的 : TableName tname =
 * TableName.valueOf(tablename); 5.通过conn连接获得表 对象 :Table table =
 * connection.getTable(tableName); 6.1.单挑插入table.put(Put)
 * 6.2.批量插入数据,先用list封装put对象:List<Put> batPut = new ArrayList<Put>(); Put put =
 * new Put(Bytes.toBytes("rowkey_"+i)); //插入的rowkey
 * put.addColumn(Bytes.toBytes("i"), Bytes.toBytes("username"),
 * Bytes.toBytes("un_"+i)); //列簇,列,值 batPut.add(put) table.put(batPut)
 * 
 * 
 * hbase操作 获取数据 1.通过HBaseConfiguration.create() :获取配置 conf 2.conf.set()
 * :设置zk等参数(kerberos认证等) 3.ConnectionFactory.createConnection(configuration)
 * :获取连接conn 4.创建TableName:描述表名称的 : TableName tname =
 * TableName.valueOf(tablename); 5.通过conn连接获得表 对象 :Table table =
 * connection.getTable(tableName); 6.List<Get> gets = new ArrayList<Get>();
 * //批量封装请求信息 Get get = new Get(Bytes.toBytes("rowkey_"+i)); //查询的rowkey
 * gets.add(get); 7.Result[] results = table.get(gets); //通过Result[]接收数据
 * 8.使用CellScanner cellScanner = result.cellScanner(); 获取cell
 * while(cellScanner.advance()){ Cell cell = cellScanner.current();
 * //从单元格cell中把数据获取并输出 //使用 CellUtil工具类,从cell中把数据获取出来 String famliy =
 * Bytes.toString(CellUtil.cloneFamily(cell)); String qualify =
 * Bytes.toString(CellUtil.cloneQualifier(cell)); String rowkey =
 * Bytes.toString(CellUtil.cloneRow(cell)); String value =
 * Bytes.toString(CellUtil.cloneValue(cell));
 * System.out.println("rowkey:"+rowkey+",columnfamily:"+famliy+",qualify:"+
 * qualify+",value:"+value); }
 * 
 * @author jiangtao
 *
 */
public class HBaseTest {
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;

	public static void main(String[] args) throws IOException {
		// 建表
		// createTable("chinese", new String[] { "name", "xinxi" });
		// 查看已存在的表
		// listTables();
		// 增加数据
		// 表名 tableName, 主键 rowkey, 列族 colFamily, 列 col, 值 val
//		 insterRow("chinese", "row2", "name", "name", "xixi");
//		 insterRow("chinese", "row2", "name", "age", "8");
//		 insterRow("chinese", "row2", "xinxi", "aihao", "mengnan");
		// insterRowTest("test0306", "row2", "cf1", "name", "xiaoYuan");
		//查看表所有数据
//		scanData("chinese");
		// 条件查询:表名,主键,列族,列
//		getData("chinese", "row1", null, null);
		 
		// 删除数据
//		 deleRow("chinese", "row2", "xinxi", "mengnan");
		// deleteTable("chinese");
	}

	public static void insterRowTest(String tableName) throws IOException {
		init();
		List<Put> putList = new ArrayList<Put>();
		Table table = connection.getTable(TableName.valueOf(tableName));
		for (int i = 0; i < 100; i++) {
			Put put = new Put(Bytes.toBytes(i));// rowKey
			put.addColumn(Bytes.toBytes("name"), Bytes.toBytes("personName"), Bytes.toBytes("小红花" + i));
			put.addColumn(Bytes.toBytes("name"), Bytes.toBytes("age"), Bytes.toBytes(10));
			put.addColumn(Bytes.toBytes("xinxi"), Bytes.toBytes("address"), Bytes.toBytes("北京呀" + i));
			put.addColumn(Bytes.toBytes("xinxi"), Bytes.toBytes("aihao"), Bytes.toBytes("钱呀" + i));
			putList.add(put);
			// table.put(put);
		}
		table.put(putList);
		table.close();
		close();
	}

	public static void insterRowTest(String tableName, String rowkey, String colFamily, String col, String val)
			throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(Bytes.toBytes(rowkey));
		put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
		put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes("age"), Bytes.toBytes("15"));
		put.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("aihao"), Bytes.toBytes("money"));
		table.put(put);

		// 批量插入
		/*
		 * List<Put> putList = new ArrayList<Put>(); puts.add(put);
		 * table.put(putList);
		 */
		table.close();
		close();
	}

	// 初始化链接
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.zookeeper.quorum", "127.0.0.1");
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
		configuration.set("zookeeper.znode.parent", "/hbase");

		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 关闭连接
	public static void close() {
		try {
			if (null != admin)
				admin.close();
			if (null != connection)
				connection.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	// 建表
	public static void createTable(String tableNmae, String[] cols) throws IOException {

		init();
		TableName tableName = TableName.valueOf(tableNmae);

		if (admin.tableExists(tableName)) {
			System.out.println("talbe is exists!");
		} else {
			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
			for (String col : cols) {
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
		}
		close();
	}

	// 删表
	public static void deleteTable(String tableName) throws IOException {
		init();
		TableName tn = TableName.valueOf(tableName);
		if (admin.tableExists(tn)) {
			admin.disableTable(tn);
			admin.deleteTable(tn);
		}
		close();
	}

	// 查看已有表
	public static void listTables() throws IOException {
		init();
		HTableDescriptor hTableDescriptors[] = admin.listTables();
		for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
			System.out.println(hTableDescriptor.getNameAsString());
		}
		close();
	}

	// 插入数据
	public static void insterRow(String tableName, String rowkey, String colFamily, String col, String val)
			throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(Bytes.toBytes(rowkey));
		put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val));
		table.put(put);

		// 批量插入
		/*
		 * List<Put> putList = new ArrayList<Put>(); puts.add(put);
		 * table.put(putList);
		 */
		table.close();
		close();
	}

	// 删除数据
	public static void deleRow(String tableName, String rowkey, String colFamily, String col) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Delete delete = new Delete(Bytes.toBytes(rowkey));
		// 删除指定列族
		// delete.addFamily(Bytes.toBytes(colFamily));
		// 删除指定列
		// delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col));
		table.delete(delete);
		// 批量删除
		/*
		 * List<Delete> deleteList = new ArrayList<Delete>();
		 * deleteList.add(delete); table.delete(deleteList);
		 */
		table.close();
		close();
	}

	// 根据rowkey查找数据
	public static void getData(String tableName, String rowkey, String colFamily, String col) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowkey));
		// 获取指定列族数据
		if (colFamily != null) {
			get.addFamily(Bytes.toBytes(colFamily));
		}
		// 获取指定列数据
		if (col != null) {
			get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
		}
		Result result = table.get(get);

		showCellLikeMysql(result);
		table.close();
		close();
	}

	// 格式化输出
	public static void showCellLikeMysql(Result result) {
		Cell[] cells = result.rawCells();
		Boolean b = true;
		for (Cell cell : cells) {
			if (b) {
				System.out.print(Bytes.toInt(CellUtil.cloneRow(cell)) + "——> ");
				b = false;
			}
			System.out.print(new String(CellUtil.cloneQualifier(cell)) + ":");
			System.out.print(BytesToStr(CellUtil.cloneValue(cell)) + " 、");
		}
		System.out.println();

	}

	private static String BytesToStr(byte[] cloneValue) {
		String ss = Bytes.toString(cloneValue);
		if ("".equals(ss.trim())) {
			ss = Bytes.toInt(cloneValue) + "";
		}
		return ss;
	}

	// 批量查找数据
	public static void scanData(String tableName) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Scan scan = new Scan();
		// scan.setStartRow(Bytes.toBytes(startRow));
		// scan.setStopRow(Bytes.toBytes(stopRow));
		ResultScanner resultScanner = table.getScanner(scan);
		for (Result result : resultScanner) {
			showCellLikeMysql(result);
		}
		table.close();
		close();
	}
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值