天天看點

java 調用 hbase

package com.hbase;

import java.io.IOException;
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.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.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 HbaseClient {

	private static Configuration conf = null;
	    
	/**
	 * hbase-0.90.3
	 * 初始化配置
	 */
	static {
		conf = HBaseConfiguration.create();
		conf.set("hbase.master", "192.168.27.12");
	}

	/**
	 * 建立表操作
	 * 
	 * @throws IOException
	 */
	public void createTable(String tablename, String[] cfs) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tablename)) {
			System.out.println("表已經存在!");
		} else {
			HTableDescriptor tableDesc = new HTableDescriptor(tablename);
			for (int i = 0; i < cfs.length; i++) {
				tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
			}
			admin.createTable(tableDesc);
			System.out.println(" 表建立成功!");
		}
	}

	/**
	 * 删除表操作
	 * 
	 * @param tablename
	 * @throws IOException
	 */
	public void deleteTable(String tablename) throws IOException {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			if (!admin.tableExists(tablename)) {
				System.out.println("表不存在, 無需進行删除操作!");
			}else{
				admin.disableTable(tablename);
				admin.deleteTable(tablename);
				System.out.println(" 表删除成功!");
			}
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		}
	}

	public void insertRow() throws Exception{
         HTable table = new HTable(conf, "test");
         Put put = new Put(Bytes.toBytes("row3"));
         put.add(Bytes.toBytes("cf"), Bytes.toBytes("444"), Bytes.toBytes("value444"));
         table.put(put);
	}
	
	/**
	 * 插入一行記錄
	 * 
	 * @param tablename
	 * @param cfs
	 */
	public void writeRow(String tablename, String[] cfs) {
		try {
			HTable table = new HTable(conf, tablename);
			Put put = new Put(Bytes.toBytes("rows3"));
			for (int j = 0; j < cfs.length; j++) {
				put.add(Bytes.toBytes(cfs[j]),
						Bytes.toBytes(cfs[j]+String.valueOf(1)),
						Bytes.toBytes(cfs[j]+"value"));
				
				table.put(put);
			}
			System.out.println("寫入成功!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//寫多條記錄
	public void writeMultRow(String tablename, String[][] cfs) {
		try {
			HTable table = new HTable(conf, tablename);
			List<Put> lists = new ArrayList<Put>();
			for (int i = 0; i < cfs.length; i++) {
				Put put = new Put(Bytes.toBytes(cfs[i][0]));
				put.add(Bytes.toBytes(cfs[i][1]),
						Bytes.toBytes(cfs[i][2]),
						Bytes.toBytes(cfs[i][3]));
				lists.add(put);
			}
			table.put(lists);
			System.out.println("寫入成功!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除一行記錄
	 * 
	 * @param tablename
	 * @param rowkey
	 * @throws IOException
	 */
	public void deleteRow(String tablename, String rowkey) throws IOException {
		HTable table = new HTable(conf, tablename);
		List<Delete> list = new ArrayList<Delete>();
		Delete d1 = new Delete(rowkey.getBytes());
		list.add(d1);
		table.delete(list);
		System.out.println("删除行成功!");
	}

	/**
	 * 查找一行記錄
	 * 
	 * @param tablename
	 * @param rowkey
	 */
	public static void selectRow(String tablename, String rowKey)
			throws IOException {
		HTable table = new HTable(conf, tablename);
		Get g = new Get(rowKey.getBytes());
//		g.addColumn(Bytes.toBytes("cf:1"));
		Result rs = table.get(g);
		for (KeyValue kv : rs.raw()) {
			System.out.print(new String(kv.getRow()) + "  ");
			System.out.print(new String(kv.getFamily()) + ":");
			System.out.print(new String(kv.getQualifier()) + "  ");
			System.out.print(kv.getTimestamp() + "  ");
			System.out.println(new String(kv.getValue()));
		}
	}

	/**
	 * 查詢表中所有行
	 * 
	 * @param tablename
	 */
	public void scaner(String tablename) {
		try {
			HTable table = new HTable(conf, tablename);
			Scan s = new Scan();
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					System.out.print(new String(kv[i].getRow()) + "  ");
					System.out.print(new String(kv[i].getFamily()) + ":");
					System.out.print(new String(kv[i].getQualifier()) + "  ");
					System.out.print(kv[i].getTimestamp() + "  ");
					System.out.println(new String(kv[i].getValue()));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		HbaseClient cli = new HbaseClient();
		
		//删除表
		cli.deleteTable("test");
		
		//建立表
		cli.createTable("test", new String[]{"cf"});
		
		
		//寫多條記錄
		cli.writeMultRow("test", new String[][]{{"rows1","cf","1","value1"},{"rows1","cf","2","value2"},{"rows2","cf","2","value2"}});
		
		//寫一條記錄
		cli.writeRow("test", new String[]{"cf"});

		System.out.println("\n查詢一個rows1:");
		selectRow("test", "rows1");
		
		System.out.println("\n查詢所有:");
		cli.scaner("test");
		
	}
}