Hbase常用操作(增删改查)


HBase提供了java api来对HBase进行一系列的管理涉及到对表的管理、数据的操作等。常用的API操作有:

1、 对表的创建、删除、显示以及修改等,可以用HBaseAdmin,一旦创建了表,那么可以通过HTable的实例来访问表,每次可以往表里增加数据。
  2、 插入数据
    创建一个Put对象,在这个Put对象里可以指定要给哪个列增加数据,以及当前的时间戳等值,然后通过调用HTable.put(Put)来提交操作,在这里提请注意的是:在创建Put对象的时候,你必须指定一个行(Row)值,在构造Put对象的时候作为参数传入。
  3、 获取数据

 要获取数据,使用Get对象,Get对象同Put对象一样有好几个构造函数,通常在构造的时候传入行值,表示取第几行的数据,通过HTable.get(Get)来调用。

  4、 浏览每一行

    通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个,通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。Result是一个Key/Value的链表。

  5、 删除
    使用Delete来删除记录,通过调用HTable.delete(Delete)来执行删除操作。(注:删除这里有些特别,也就是删除并不是马上将数据从表中删除。)
  6、 锁
    新增、获取、删除在操作过程中会对所操作的行加一个锁,而浏览却不会。
  7、 簇的访问
    客户端代码通过ZooKeeper来访问找到簇,也就是说ZooKeeper quorum将被使用,那么相关的类(包)应该在客户端的类(classes)目录下,即客户端一定要找到文件hbase-site.xml。 

     新建一个类:

package com.jhl.hbase;
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.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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.util.Bytes;

public class OperateTable {
	private static Configuration conf = null;
	static {
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "master");// 使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.property.clientPort", "2181");

	}

	// 创建数据库表
	@SuppressWarnings("deprecation")
	public static void createTable(String tableName, String[] columnFamilys)
			throws Exception {
		HBaseAdmin hadmin = new HBaseAdmin(conf);
		if (hadmin.tableExists(tableName)) {
			System.out.println("表已存在");
			System.exit(0);
		} else {
			// 新建一个 表的描述
			HTableDescriptor tableDesc = new HTableDescriptor(tableName);
			// 在描述里添加列族
			for (String columnFamily : columnFamilys) {
				tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			}
			// 根据配置好的描述建表
			hadmin.createTable(tableDesc);
			System.out.println("创建表成功!");
		}

	}

	// 删除数据库表
	public static void deleteTable(String tableName) throws Exception {
		HBaseAdmin hadmin = new HBaseAdmin(conf);
		if (hadmin.tableExists(tableName)) {
			// 关闭表
			hadmin.disableTable(tableName);
			hadmin.deleteTable(tableName);
			System.out.println("删除表成功!");
		} else {
			System.out.println("删除的表不存在");
			System.exit(0);
		}

	}

	// 添加一条数据
	public static void addRow(String tableName, String row,
			String columnFamily, String column, String value) throws Exception {
		HTable hTable = new HTable(conf, tableName);
		Put put = new Put(Bytes.toBytes(row));
		// 参数出分别:列族、列、值
		put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
				Bytes.toBytes(value));
		hTable.put(put);

	}

	// 删除一条数据
	public static void delRow(String tableName, String row) throws Exception {
		HTable hTable = new HTable(conf, tableName);
		Delete delete = new Delete(Bytes.toBytes(row));
		hTable.delete(delete);

	}

	// 删除多条数据
	public static void delMultiRows(String tableName, String[] rows)
			throws Exception {
		HTable hTable = new HTable(conf, tableName);
		List<Delete> list = new ArrayList<Delete>();
		for (String row : rows) {
			Delete delete = new Delete(Bytes.toBytes(row));
			list.add(delete);
		}
		hTable.delete(list);
	}

	// get row
	public static void getRow(String tableName, String row) throws Exception {
		HTable hTable = new HTable(conf, tableName);
		Get get = new Get(Bytes.toBytes(row));
		Result result = hTable.get(get);
		for (KeyValue rowkv : result.raw()) {
			System.out.print("Row Name: " + new String(rowkv.getRow()) + " ");
			System.out.print("Timestamp: " + rowkv.getTimestamp() + " ");
			System.out.print("column Family: " + new String(rowkv.getFamily())
					+ " ");
			System.out.print("Row Name:  " + new String(rowkv.getQualifier())
					+ " ");
			System.out.println("Value: " + new String(rowkv.getValue()) + " ");
		}
	}
        	// get filter row
	public static void getFilterRow(String tableName, String row) throws Exception {
		HTable hTable = new HTable(conf, tableName);
		Get get = new Get(Bytes.toBytes(row));
		ByteArrayComparable qualifierComparator = new SubstringComparator("course");
		Filter filter = new QualifierFilter(CompareOp.LESS_OR_EQUAL, qualifierComparator);
		get.setFilter(filter);
		Result result = hTable.get(get);
		System.out.println("result= " + result);
		for (KeyValue rowkv : result.raw()) {
			System.out.print("Row Name: " + new String(rowkv.getRow()) + " ");
			System.out.print("Timestamp: " + rowkv.getTimestamp() + " ");
			System.out.print("column Family: " + new String(rowkv.getFamily())
					+ " ");
			System.out.print("Row Name:  " + new String(rowkv.getQualifier())
					+ " ");
			System.out.println("Value: " + new String(rowkv.getValue()) + " ");
		}
	}
	// get all records
	public static void getAllRows(String tableName) throws Exception {
		HTable hTable = new HTable(conf, tableName);
		Scan scan = new Scan();

		ResultScanner results = hTable.getScanner(scan);
		for (Result result : results) {
			for (KeyValue rowKV : result.raw()) {
				System.out.print("Row Name: " + new String(rowKV.getRow())
						+ " ");
				System.out.print("Timestamp: " + rowKV.getTimestamp() + " ");
				System.out.print("column Family: "
						+ new String(rowKV.getFamily()) + " ");
				System.out.print("Row Name:  "
						+ new String(rowKV.getQualifier()) + " ");
				System.out.println("Value: " + new String(rowKV.getValue())
						+ " ");
			}
		}
	}

	public static void main(String[] args) {
		String tableName = "score";
		String[] columnFamilys = { "info", "course" };
		try {
			// OperateTable.createTable(tableName, columnFamilys);

			// OperateTable.deleteTable(tableName);

			// 添加第一行数据
			// OperateTable.addRow(tableName, "tht", "info", "age", "20");
			// OperateTable.addRow(tableName, "tht", "info", "sex", "boy");
			// OperateTable.addRow(tableName, "tht", "course", "china", "97");
			// OperateTable.addRow(tableName, "tht", "course", "math", "128");
			// OperateTable.addRow(tableName, "tht", "course", "english", "85");
		     // 添加第二行数据
			// OperateTable.addRow(tableName, "xiaoxue", "info", "age", "19");
			// OperateTable.addRow(tableName, "xiaoxue", "info", "sex", "boy");
			// OperateTable.addRow(tableName, "xiaoxue", "course", "china", "90");
			// OperateTable.addRow(tableName, "xiaoxue", "course", "math", "120");
			// OperateTable.addRow(tableName, "xiaoxue", "course", "english", "90");
			 // 添加第三行数据
			// OperateTable.addRow(tableName, "qingqing", "info", "age", "18");
			// OperateTable.addRow(tableName, "qingqing", "info", "sex", "girl");
			// OperateTable.addRow(tableName, "qingqing", "course", "china", "100");
			// OperateTable.addRow(tableName, "qingqing", "course", "math","100");
			// OperateTable.addRow(tableName, "qingqing", "course", "english","99");

			// OperateTable.getRow(tableName, "xiaoxue");

			// OperateTable.getAllRows(tableName);

			// OperateTable.delRow(tableName, "tht");

			String[] rows = { "xiaoxue", "qingqing" };
			OperateTable.delMultiRows(tableName, rows);
			
			OperateTable.getFilterRow(tableName, "tht");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}


转载于:https://my.oschina.net/u/189445/blog/595280

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值