hbase的常用操作

package com.wangl.hadoop.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.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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

public class HBaseUtils {
	private static Configuration conf = null;
	
	@Before
	public void init(){
		conf = HBaseConfiguration.create();
	}
	/**
	 * 刪除指定表
	 * @param tablename	表名
	 * @throws Exception
	 */
	@Test
	public void testDrop(String tablename) throws Exception{
	    //old api
// 		@SuppressWarnings("deprecation")
// 		HBaseAdmin admin = new HBaseAdmin(conf);API
        //new API
		Connection connection = ConnectionFactory.createConnection(conf);
		HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
		admin.disableTable(tablename);
		admin.deleteTable(tablename);
		admin.close();
	}
	/**
	 * 想制定表中指定列族指定列插入數據
	 * @param tablename	表名
	 * @param columnFamily	列族
	 * @param rk	行鍵
	 * @param columnName	列名
	 * @param value	值
	 * @throws Exception
	 */
	@Test
	public void testPut(String tablename, String columnFamily, String rk, String columnName, String value) throws Exception{
		if(tablename != null && columnFamily != null && rk != null && columnName != null && value != null){
			@SuppressWarnings("deprecation")
			HTable table = new HTable(conf, tablename);
			Put put1 = new Put(Bytes.toBytes(rk));
			put1.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes(value));
			List puts = new ArrayList();
			table.put(puts);
			table.close();
		}
	}
	/**
	 * 得到指定表中指定列族下指定列的值
	 * @param tablename
	 * @param columnFamily
	 * @param rk
	 * @param columnName
	 * @throws Exception
	 */
	@Test
	public void testGet(String tablename, String columnFamily, String rk, String columnName) throws Exception{
		if(tablename != null && columnFamily != null && rk != null && columnName != null){
			@SuppressWarnings("deprecation")
			HTable table = new HTable(conf, tablename);
			Get get = new Get(rk.getBytes());
			get.setMaxVersions(5);
			Result result = table.get(get);
//			List cells = result.listCells();
//			for(Cell c:cells){
//				System.out.println(new String(c.getFamilyArray()));
//				System.out.println(new String(c.getQualifierArray()));
//				System.out.println(new String(c.getValueArray()));
//			}
			result.getValue(columnFamily.getBytes(), columnName.getBytes());
			table.close();
		}
	}
	
	@Test
	public void testScan(String tablename, String startRk, String endRk) throws Exception{
		@SuppressWarnings("deprecation")
		HTable table = new HTable(conf, tablename);
		Scan scan = new Scan(startRk.getBytes(),endRk.getBytes());
		//前綴過濾器---針對的是行鍵
		Filter filter = new PrefixFilter("rk".getBytes());
		//行過濾器
		ByteArrayComparable rowComparator = new BinaryComparator("person_rk_0001".getBytes());
		RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);
		/**
		 * 假設rk 的格式爲 :創建日期_發佈日期_ID_TITLE
		 * 目標:查找 	發佈日期:“2016_10_27”的數據
		 */
		rf = new RowFilter(CompareOp.EQUAL, new SubstringComparator("_2016_10_27_"));
		//單值匹配過濾器1		完整匹配字節數組
		new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "wangl".getBytes());
		//單值匹配過濾器2	匹配正則表達式
		ByteArrayComparable comparator = new RegexStringComparator("zhan.*");
		new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, comparator);
		//單值匹配過濾器3	匹配是否包含給定字符,大小寫不敏感
		comparator = new SubstringComparator("wu");
		new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, comparator);
		//鍵值對元數據過濾------family過濾------字節數組完整匹配
		FamilyFilter ff = new FamilyFilter(CompareOp.EQUAL, new BinaryComparator("base_info".getBytes()));
		//鍵值對元數據過濾-----family過濾----字節數組前綴匹配	如果表中有inf開頭的列族,過濾結果爲該列族的所有行
		ff = new FamilyFilter(CompareOp.EQUAL, new BinaryPrefixComparator("inf".getBytes()));
		//鍵值對元數據過濾------family過濾------字節數組完整匹配
		QualifierFilter qf = new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("name".getBytes()));
		//鍵值對元數據過濾-----family過濾----字節數組前綴匹配	如果表中有name開頭的列,過濾結果爲所有行該列的數據
		qf = new QualifierFilter(CompareOp.EQUAL, new BinaryPrefixComparator("name".getBytes()));
		
		//基於列名(即Qualifier)前綴過濾數據的ColumnPrefixFilter
        filter = new ColumnPrefixFilter("na".getBytes());
        
        scan.setFilter(filter);
		
		ResultScanner rs = table.getScanner(scan);
		for(Result r:rs){
			String value = new String(r.getValue("base_info".getBytes(), "name".getBytes()));
			System.out.println(value);
		}
		
		table.close();
	}
	/**
	 * 刪除指定表中指定行,指定列族,指定列
	 * @param tableName
	 * @param rk
	 * @param columnName
	 * @param familyName
	 * @throws Exception
	 */
	@Test
	public void testDel(String tableName, String rk, String columnName,String familyName) throws Exception{
		@SuppressWarnings("deprecation")
		HTable table = new HTable(conf, tableName);
		Delete del = new Delete(rk.getBytes());
		del.addColumn(familyName.getBytes(), columnName.getBytes());
		table.delete(del);
		table.close();
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章