JavaAPI訪問Hadoop2.2HA的配置下訪問Hbase0.96.2

1、確保Hadoop和Hbase服務已經正常啓動了

2、把hbase-site.xml,core-site.xml,hdfs-site.xml配置文件放到Java工程的src目錄下


3、引入相關的依賴包

4、Java Client測試訪問Hbase集羣

package com.hbase.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;

public class TestHBase {

	/**
	 * @param args
	 */
	public static Configuration configuration ;
	
	/**
	 * 初始化參配置信息
	 */
	static {
		configuration = HBaseConfiguration.create() ;
		//配置文件加載方式一
//		configuration.set("hbase.master", "10.58.51.78:60000") ;
//		configuration.set("hbase.zookeeper.quorum", "S1SF001,S1SF002,S1SF003,S1SF004,S1SF005") ;
		//配置文件加載方式二
		String filePath = "hbase-site.xml" ;
		Path path = new Path(filePath);
		configuration.addResource(path);
	}
	
	/**
	 * 根據給定的表名創建表
	 * @param tableName 表名
	 * @throws Exception 
	 */
	public static void createTable(String tableName) throws Exception {
		HBaseAdmin admin = new HBaseAdmin(configuration);
		if (admin.tableExists(tableName)) {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println("表已存在,先刪除...");
		}
		
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
		tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
		admin.createTable(tableDescriptor) ;
		admin.close();
	}
	
	/**
	 * 初始化數據
	 * @param tableName
	 * @throws Exception
	 */
	public static void initData(String tableName) throws Exception{
		HTable table = new HTable(configuration, tableName) ;
		for(int i=10;i<22;i++){
			String ii = String.valueOf(i);
			Put put = new Put(ii.getBytes()) ;
			put.add("cf1".getBytes(), "column1".getBytes(), "the first column".getBytes()) ;
			put.add("cf1".getBytes(), "column2".getBytes(), "the second column".getBytes()) ;
			put.add("cf1".getBytes(), "column3".getBytes(), "the third column".getBytes()) ;
			table.put(put) ;
		}
		table.close();
	}
	
	/**
	 * 刪除一行數據
	 * @param tableName 表名
	 * @param rowKey rowkey
	 * @throws Exception
	 */
	public static void deleteRow(String tableName,String rowKey) throws Exception{
		HTable table = new HTable(configuration,tableName);
		Delete delete = new Delete(rowKey.getBytes());
		table.delete(delete);
	}
	
	/**
	 *  刪除rowkey列表
	 * @param tableName
	 * @param rowKeys
	 * @throws Exception
	 */
	public static void deleteRowKeys(String tableName, List<String> rowKeys) throws Exception
	{
		HTable table = new HTable(configuration, tableName) ;
		List<Delete> deletes = new ArrayList<Delete>();
		for(String rowKey:rowKeys){
			Delete delete = new Delete(rowKey.getBytes());
			deletes.add(delete);
		}
		table.delete(deletes);
		table.close();
	}
	
	/**
	 * 根據rowkey獲取所有column值
	 * @param tableName
	 * @param rowKey
	 * @throws Exception
	 */
	public static void get(String tableName,String rowKey) throws Exception{
		HTable table = new HTable(configuration, tableName) ;
		Get get = new Get(rowKey.getBytes());
		Result result = table.get(get);
		for(KeyValue kv:result.raw()){
			System.out.println("cf="+new String(kv.getFamily())+", columnName="+new String(kv.getQualifier())+", value="+new String(kv.getValue()));
		}
	}
	
	
	
	/**
	 * 批量查詢
	 * @param tableName
	 * @param startRow
	 * @param stopRow
	 * @throws Exception
	 * select column1,column2,column3 from test_table where id between ... and
	 */
	public static void scan(String tableName, String startRow, String stopRow) throws Exception {
		HTable table = new HTable(configuration, tableName);
		Scan scan = new Scan () ;
		scan.addColumn("cf1".getBytes(), "column1".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column2".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column3".getBytes()) ;
		
		//rowkey>=a && rowkey<b
		scan.setStartRow(startRow.getBytes());
		scan.setStopRow(stopRow.getBytes());
		ResultScanner scanner = table.getScanner(scan) ;
		
		for(Result result : scanner)
		{
			for(KeyValue keyValue : result.raw())
			{
				System.out.println(new String(keyValue.getFamily())+":"+new String(keyValue.getQualifier())+"="+new String(keyValue.getValue()));
			}
		}
		
	}
	
	/**
	 * 單條件查詢:測試SingleColumnValueFilter過濾器
	 * @param tableName
	 * @param columnValue
	 * @throws Exception
	 * LESS  <
		LESS_OR_EQUAL <=
		EQUAL =
		NOT_EQUAL <>
		GREATER_OR_EQUAL >=
		GREATER >
		NO_OP no operation
	 */
	public static void testSingleColumnValueFilter(String tableName, String columnValue) throws Exception
	{
		HTable table = new HTable(configuration, tableName) ;
		Scan scan = new Scan () ;
		scan.addColumn("cf1".getBytes(), "column1".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column2".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column3".getBytes()) ;
		//根據rowkey查詢
		//RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator("1"));
		//通過column查詢
		Filter filter = new SingleColumnValueFilter("cf1".getBytes(),"column1".getBytes(), CompareFilter.CompareOp.GREATER,columnValue.getBytes());
		scan.setFilter(filter);
		ResultScanner scanner = table.getScanner(scan) ;
		
		for(Result result : scanner)
		{
			for(KeyValue keyValue : result.raw())
			{
				System.out.println("第 "+new String(keyValue.getRow())+" 行 ,"+new String(keyValue.getFamily())+":"+new String(keyValue.getQualifier())+"="+new String(keyValue.getValue()));
			}
			System.out.println();
		}
		
	}
	
	/**
	 * 模糊匹配rowkey
	 * @param tableName
	 * @param rowKeyRegex
	 * @throws Exception
	 */
	public static void fuzzyQueryByRowkey(String tableName, String rowKeyRegex) throws Exception
	{
		HTable table = new HTable(configuration, tableName) ;
		RowFilter filter = new RowFilter(CompareOp.EQUAL, new RegexStringComparator(rowKeyRegex)) ;
//		PrefixFilter filter = new PrefixFilter(rowKeyRegex.getBytes());
		Scan scan = new Scan();
		scan.addColumn("cf1".getBytes(), "column1".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column2".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column3".getBytes()) ;
		scan.setFilter(filter);
		
		ResultScanner scanner = table.getScanner(scan);
		int num=0;
		for(Result result : scanner)
		{
			num ++ ;
			
			System.out.println("rowKey:"+new String(result.getRow()));
			for(KeyValue keyValue : result.raw())
			{
				System.out.println(new String(keyValue.getFamily())+":"+new String(keyValue.getQualifier())+"="+new String(keyValue.getValue()));
			}
			System.out.println();
		}
		System.out.println(num);
	}
	
	
	/**
	 * 組合條件查詢
	 * @param tableName
	 * @param column1
	 * @param column2
	 * @param column3
	 * @throws Exception
	 */
	public static void multiConditionQuery(String tableName, String column1, String column2,String column3) throws Exception
	{
		HTable table = new HTable(configuration, tableName) ;
		Scan scan = new Scan();
		scan.addColumn("cf1".getBytes(), "column1".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column2".getBytes()) ;
		scan.addColumn("cf1".getBytes(), "column3".getBytes()) ;
		
		SingleColumnValueFilter filter1 = new SingleColumnValueFilter("cf1".getBytes(),"column1".getBytes(), CompareFilter.CompareOp.EQUAL,column1.getBytes());
		SingleColumnValueFilter filter2 = new SingleColumnValueFilter("cf1".getBytes(),"column2".getBytes(), CompareFilter.CompareOp.EQUAL,column2.getBytes());
		SingleColumnValueFilter filter3 = new SingleColumnValueFilter("cf1".getBytes(),"column3".getBytes(), CompareFilter.CompareOp.EQUAL,column3.getBytes());
		
		FilterList filterAll = new FilterList();
		filterAll.addFilter(filter1) ;
		
		//與sql查詢的in (?,?)一樣的效果
		FilterList filterList = new FilterList(FilterList.Operator.MUST_PASS_ONE);
		filterList.addFilter(filter2);
		filterList.addFilter(filter3);
		
		filterAll.addFilter(filterList);
		scan.setFilter(filterAll);
		
		ResultScanner scanner = table.getScanner(scan);
		for(Result result : scanner)
		{
			System.out.println("rowKey:"+new String(result.getRow()));
			for(KeyValue keyValue : result.raw())
			{
				System.out.println(new String(keyValue.getFamily())+":"+new String(keyValue.getQualifier())+"="+new String(keyValue.getValue()));
			}
			System.out.println();
		}
		
	}
	
	
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		createTable("panguoyuan_test_table-1") ;
		initData("panguoyuan_test_table-1");
//		deleteRow("test_table","1");
//		List list =  new ArrayList();
//		list.add("1");
//		list.add("2");
//		deleteRowKeys("test_table",list);
		
//		get("test_table","1");
//		scan("test_table","1","4");
//		testSingleColumnValueFilter("test_table","11111");
//		fuzzyQueryByRowkey("test_table","1");
//		multiConditionQuery("test_table", "the first column", "the second column", "the third column");
//		fuzzyQueryByRowkey("test_table", "1");
//		fuzzyQueryBycolumn("test_table", "ee*");
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章