Hbase配置java客戶端

1.修改windows配置文件java

C:\WINDOWS\system32\drivers\etc\hosts
apache

將遠程hbase和zookeeper主機的IP地址加進去windows

54.0.88.53      HADOOP1
54.0.88.54      HADOOP2
eclipse

2.加入jar包oop


 


 


3.加載配置文件spa

問題:網上不少都是自定義配置文件,根據hbase-site.xml裏的參數以下配置:code

configuration = HBaseConfiguration.create();orm

configuration.set("hbase.zookeeper.property.clientPort", "2181");  xml

configuration.set("hbase.zookeeper.quorum", "HADOOP1,HADOOP2,HADOOP3,HADOOP4,HADOOP5");  blog

configuration.set("hbase.master", "HADOOP3:60000"); 

可是在運行時出現如下錯誤:

 

Exception in thread "main" org.apache.hadoop.hbase.client.RetriesExhaustedException: Failed after attempts=35, exceptions:
Wed Sep 24 08:47:46 CST 2014, org.apache.hadoop.hbase.client.RpcRetryingCaller@191f801, java.io.IOException: Failed to find location, tableName=hbase:meta, row=hbasekang,,, reload=false
Wed Sep 24 08:48:56 CST 2014, org.apache.hadoop.hbase.client.RpcRetryingCaller@191f801, java.io.IOException: Failed to find location, tableName=hbase:meta, row=hbasekang,,, reload=true

 

參考這裏的方法,可是尚未很好的解決,我猜多是configuration訪問的不止這三個參數選項。

因此最好加載配置文件的方式,zookeeper的配置項和hbase的配置也能夠保持一致。

 

   static {
        config = HBaseConfiguration.create();
        config
                .addResource(new Path(
                        "D://eclipse/myeclipse/workspace/Hive/hbaselib/hbase-site.xml"));
    }

 

 記得這個hbase-site.xml和當前的hbase環境下的是一致的,要否則會報錯:

Caused by: java.io.IOException: Unable to determine ZooKeeper ensemble

順便普及一下:

Hbase的客戶端API中,configuration至關於提供對配置參數的訪問途徑,任何操做都要先建立HBaseConfiguration實例。而配置文件在configuration中被當作一個個資源(Resource),也就是一組以XML格式存在的name/value對,以此來提供訪問配置信息的接口,還提供了set/get方法用於讀寫。經過addResource方法加載xml配置文件,能夠容許hadoop其餘子項目和用戶自定義的配置參數被加載使用。

 


4.貼代碼

/**
 * 
 */
package com.util.hbase;

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

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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.HTablePool;
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.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;

import com.ccb.entity.Detail3;

/**
 * @author kangxuedan
 * 
 */
public class HbaseUtil {
	public static Configuration config;
	static {
		config = HBaseConfiguration.create();
		config
				.addResource(new Path(
						"D://eclipse/myeclipse/workspace/Hive/hbaselib/hbase-site.xml"));
	}

	/**
	 * 建立表
	 * 
	 * @throws IOException
	 * @param tableName
	 *            表名
	 * @param columns
	 *            列族
	 */
	public static void createTable(String tableName, String[] columns)
			throws IOException {
		HBaseAdmin Hbaseadmin = new HBaseAdmin(config);
		if (Hbaseadmin.tableExists(tableName)) {
			System.out.println("表已經存在!");
		} else {
			HTableDescriptor desc = new HTableDescriptor(tableName);
			for (String column : columns) {
				desc.addFamily(new HColumnDescriptor(column));
			}
			Hbaseadmin.createTable(desc);
			System.out.println("表建立成功!");
		}
	}

	/**
	 * 插入數據
	 */
	// insert data
	// String newRowKey = "ffvs";String[] familys ={"cf1"};String[] values
	// ={"fdxs"};
	// insert(tableName,newRowKey,familys,values);

	public static void insert(String tableName, String newRowKey,
			String[] familys, String[] values) {
		System.out.println("************start insert ************");
		HTablePool pool = new HTablePool(config, 1000);
		Put put = new Put(newRowKey.getBytes());// 插入一行數據,傳入rowKey
		put.add(familys[0].getBytes(), null, values[0].getBytes());// 本行數據的第三列
		try {
			pool.getTable(tableName).put(put);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("************end insert************");
	}

	/**
	 * 根據 rowkey刪除一條記錄
	 * 
	 * @param tablename
	 * @param rowkey
	 */
	public static void deleteRow(String tablename, String rowkey) {
		try {
			HTable table = new HTable(config, tablename);
			List list = new ArrayList();
			Delete d1 = new Delete(rowkey.getBytes());
			list.add(d1);
			table.delete(list);
			System.out.println("刪除行成功!");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 刪除表
	 * 
	 * @param tableName
	 */
	public static void dropTable(String tableName) {
		try {
			HBaseAdmin Hbaseadmin = new HBaseAdmin(config);
			Hbaseadmin.disableTable(tableName);
			Hbaseadmin.deleteTable(tableName);
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 修改表信息
	 * 
	 * @param tableName
	 */

	public static void modifyTable(String tableName) {
		HBaseAdmin Hbaseadmin;
		try {
			Hbaseadmin = new HBaseAdmin(config);
			Hbaseadmin.disableTable(tableName);
			// modifying existing ColumnFamily addColumn, modifyColumn,
			// removeColumn
			Hbaseadmin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
			Hbaseadmin.enableTable(tableName);
		} catch (MasterNotRunningException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 查詢表,返回全部記錄
	 * 
	 * @param tableName
	 */
	public static void QueryAll(String tableName) {
		HTablePool pool = new HTablePool(config, 1000);
		try {
			ResultScanner rs = pool.getTable(tableName).getScanner(new Scan());
			for (Result r : rs) {
				System.out.println("rowkey: " + new String(r.getRow()));
				for (KeyValue keyValue : r.raw()) {
					System.out.println("列:" + new String(keyValue.getFamily())
							+ "====值:" + new String(keyValue.getValue()));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 單條件查詢,根據rowkey查詢惟一一條記錄
	 * 
	 * @param tableName
	 * @return
	 */
	public static List<Detail3> QuerySingle(String tableName, String rowKey) {

		HTablePool pool = new HTablePool(config, 1000);
		String results = "";
		Detail3 detail3 = new Detail3();
		List<Detail3> resultList = new ArrayList<Detail3>();
		try {
			Get scan = new Get(rowKey.getBytes());// 根據rowkey查詢
			Result r = pool.getTable(tableName).get(scan);
			System.out.println("得到到rowkey:" + new String(r.getRow()));
			for (KeyValue keyValue : r.raw()) {
				String result = new String(keyValue.getValue(), "utf-8");
				System.out.println("列:" + new String(keyValue.getFamily())
						+ "====值:" + new String(keyValue.getValue(), "utf-8"));
				StringTokenizer st = new StringTokenizer(results, ",");
				String[] temp = result.split("\\|");
				detail3.setCust_no(temp[0]);
				detail3.setSa_tx_dt(temp[1]);
				detail3.setTx_log_no(temp[2]);
				detail3.setSa_tx_tm(temp[3]);
				detail3.setTemp(temp[4]);
				detail3.setCust_acct_no(temp[5]);
				detail3.setSa_tx_crd_no(temp[6]);
				detail3.setCr_tx_amt(temp[7]);
				detail3.setAcct_bal(temp[8]);
				detail3.setF_fare(temp[9]);
				detail3.setDr_cr_cod(temp[10]);
				detail3.setTran_cd(temp[11]);
				detail3.setTx_type(temp[12]);
				detail3.setXt_op_trl(temp[13]);
				detail3.setXt_op_trl2(temp[14]);
				detail3.setBus_inst_no(temp[15]);
				detail3.setCanal(temp[16]);
				detail3.setSa_op_acct_no_32(temp[17]);
				detail3.setSa_op_cust_name(temp[18]);
				detail3.setSa_op_bank_no(temp[19]);
				detail3.setCr_cust_docag_stno(temp[20]);
				detail3.setSa_otx_flg(temp[21]);
				detail3.setSa_rmrk(temp[22]);
				detail3.setOther(temp[23]);
				detail3.setTlr_no(temp[24]);
				resultList.add(detail3);
				// resultList = java.util.Arrays.asList(temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultList;
	}

	/**
	 * rowkey 範圍查詢
	 * 
	 * @param tableName
	 * @return
	 */
	public static List<Detail3> QueryRange(String tableName, String cust_no,String starttime,String endtime) {
		String startRow = cust_no + starttime;
		String endRow = cust_no + endtime;
		String results = "";
		Detail3 detail3 = new Detail3();
		List<Detail3> resultList = new ArrayList<Detail3>();
		HTablePool pool = new HTablePool(config, 1000);
		try {
			Scan scan = new Scan();
			scan.setStartRow(Bytes.toBytes(startRow));
			scan.setStopRow(Bytes.toBytes(endRow));
			scan.setCacheBlocks(true);
			scan.setCaching(30000);
			ResultScanner rs = pool.getTable(tableName).getScanner(scan);
			for (Result r : rs) {
				for (KeyValue kv : r.raw()) {
					System.out.println(String.format( "key:%s",
							 Bytes.toString(kv
									.getRow())));
					System.out.println(String.format( "value:%s",
							 Bytes.toString(kv
									.getValue())));
					String result = Bytes.toString(kv.getValue());
					 String[] temp = result.split("\\|");
					 detail3.setCust_no(temp[0]);
					 detail3.setSa_tx_dt(temp[1]);
					 detail3.setTx_log_no(temp[2]);
					 detail3.setSa_tx_tm(temp[3]);
					 detail3.setTemp(temp[4]);
					 detail3.setCust_acct_no(temp[5]);
					 detail3.setSa_tx_crd_no(temp[6]);
					 detail3.setCr_tx_amt(temp[7]);
					 detail3.setAcct_bal(temp[8]);
					 detail3.setF_fare(temp[9]);
					 detail3.setDr_cr_cod(temp[10]);
					 detail3.setTran_cd(temp[11]);
					 detail3.setTx_type(temp[12]);
					 detail3.setXt_op_trl(temp[13]);
					 detail3.setXt_op_trl2(temp[14]);
					 detail3.setBus_inst_no(temp[15]);
					 detail3.setCanal(temp[16]);
					 detail3.setSa_op_acct_no_32(temp[17]);
					 detail3.setSa_op_cust_name(temp[18]);
					 detail3.setSa_op_bank_no(temp[19]);
					 detail3.setCr_cust_docag_stno(temp[20]);
					 detail3.setSa_otx_flg(temp[21]);
					 detail3.setSa_rmrk(temp[22]);
					 detail3.setOther(temp[23]);
					 detail3.setTlr_no(temp[24]);
					 resultList.add(detail3);				
				}
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return resultList;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String tableName = "detail3";
		String rowKey = "442000801K750005487";
		String cust_no = "A432502";String starttime ="2014-06-01";
		String endtime = "2014-08-01";	
		QueryRange(tableName, cust_no,starttime,endtime);}

}
相關文章
相關標籤/搜索