`
cooliufang
  • 浏览: 127415 次
社区版块
存档分类
最新评论

【Hbase】Java对hbase的操作

阅读更多
HBase提供了Java API对其进行管理,包括对表的管理、数据的操作等。
1. HBaseAdmin —— 对表的创建、删除、显示以及修改等;
  
2. HTable —— 通过HTable的实例来访问表并进行数据的操作,获取表实例如下两种方法:
   方法一:直接获取
   HTable table = new HTable(config, tableName);
   方法二:用表连接池的方式
   HTablePool pool = new HTablePool(config,1000);
   HTable table = (HTable) pool.getTable(tableName);

3.插入数据:
创建一个Put对象,并指定参数为一个行(Row)值(即指定给哪一列增加数据以及当前的时间戳等值),然后调用HTable.put(Put)。

4.获取数据
创建一个Get对象,在构造的时候传入行值,表示取第几行的数据,然后调用HTable.get(Get)。

5.删除数据
创建一个Delete对象,参数为一个行(Row)值,然后调用HTable.delete(Delete)。

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

以下是一个java对hbase的基本操作实例(hadoop-1.0.3 && hbase-0.90.3)
package tool;

import java.io.IOException;
import java.util.Scanner;

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.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.util.Bytes;

public class Hbase {
	//initial
	static Configuration config = null;
	static {
		config = HBaseConfiguration.create();
		config.set("hbase.zookeeper.quorum", "192.168.21.20, 192.168.21.21, 192.168.21.22");
		config.set("hbase.zookeeper.property.clientPort", "2181");
	}
	
	//create a new table
	public void createTable(String tableName, String[] familys) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(config);
		if (admin.tableExists(tableName)) {
			System.out.println(tableName + " is already exists,Please create another table!");
		} else {
			HTableDescriptor  desc = new HTableDescriptor(tableName);
			for (int i = 0; i < familys.length; i++) {
				HColumnDescriptor family = new HColumnDescriptor(familys[i]);			
				desc.addFamily(family);
			}
			admin.createTable(desc);
			System.out.println("Create table \'" + tableName + "\' OK!");
		}
		
	}

	//delete a table
	public void deleteTable(String tableName) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(config);
		if (!admin.tableExists(tableName)) {
			System.out.println(tableName + " is not exists!");
		} else {
			Scanner s = new Scanner(System.in);
			System.out.print("Are you sure to delete(y/n)?");
			String str = s.nextLine();
			if (str.equals("y") || str.equals("Y")) {
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
				System.out.println(tableName + " is delete!");
			} else {
				System.out.println(tableName + " is not delete!");
			}
		}		
	}
	
	//Get table example
	public void getTable(String tableName) throws IOException {
		//Method I:
//		HTable table = new HTable(config, tableName);
		//Method II: better than I.
		HTablePool pool = new HTablePool(config,1000);
		@SuppressWarnings("unused")
		HTable table = (HTable) pool.getTable(tableName);
	}

	//add a data
	public void insertData(String tableName, String rowKey, String family, String qualifier, String value) {
		try {
			HTablePool pool = new HTablePool(config, 1000);
			HTable table = (HTable)pool.getTable(tableName);
			Put put = new Put(Bytes.toBytes(rowKey));
			put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
			table.put(put);
			System.out.println("insert a data successful!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//delete a data
	public void deleteData(String tableName, String rowKey) {
		try {
			HTablePool pool = new HTablePool(config, 1000);
			HTable table = (HTable)pool.getTable(tableName);
			Delete del = new Delete(Bytes.toBytes(rowKey));
			table.delete(del);
			System.out.println("delete a data successful");
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//Query all data
	public void queryAll(String tableName) {
		try {
			HTablePool pool = new HTablePool(config, 1000);
			HTable table = (HTable) pool.getTable(tableName);
			Scan scan = new Scan();
			ResultScanner scanner = table.getScanner(scan);

			for (Result row : scanner) {
				System.out.println("\nRowkey: " + new String(row.getRow()));
				for (KeyValue kv : row.raw()) {
					System.out.print(new String(kv.getRow()) + " ");	//same as above
					System.out.print(new String(kv.getFamily()) + ":");
					System.out.print(new String(kv.getQualifier()) + " = ");
					System.out.print(new String(kv.getValue()));
					System.out.print(" timestamp = " + kv.getTimestamp() + "\n");
				}
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//query by rowkey
	public void queryByRowKey(String tableName, String rowKey) {
		try {
			HTablePool pool = new HTablePool(config, 1000);
			HTable table = (HTable)pool.getTable(tableName);
			Get get = new Get(rowKey.getBytes());
			Result row = table.get(get);
			for (KeyValue kv : row.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(new String(kv.getValue()));
				System.out.print(" timestamp = " + kv.getTimestamp() + "\n");
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/************************************************************/
	public static void main(String[] args) throws IOException {
		Hbase hbase = new Hbase();
		//Create a new table
		String tableName = "test";
		System.out.println("=======delete table======");
		hbase.deleteTable(tableName);
		
		System.out.println("=======create table======");
		String[] familys = {"info", "scores"};		
		hbase.createTable(tableName, familys);
		
		System.out.println("=======insert data=======");
		//insert Jim
		hbase.insertData(tableName, "Jim", "info", "sex", "male");
		hbase.insertData(tableName, "Jim", "info", "age", "18");
		hbase.insertData(tableName, "Jim", "scores", "Chinese", "98");
		hbase.insertData(tableName, "Jim", "scores", "English", "90");
		hbase.insertData(tableName, "Jim", "scores", "Math", "100");
		//insert Ann
		hbase.insertData(tableName, "Ann", "info", "sex", "female");
		hbase.insertData(tableName, "Ann", "info", "age", "18");
		hbase.insertData(tableName, "Ann", "scores", "Chinese", "97");
		hbase.insertData(tableName, "Ann", "scores", "Math", "95");		

		System.out.println("=======query all data=======");
		hbase.queryAll(tableName);
		
		System.out.println("=======query by rowkey=======");
		String rowKey = "Ann";
		hbase.queryByRowKey(tableName, rowKey);
		
		System.out.println("=======delete a data=======");
		hbase.deleteData(tableName, rowKey);
		hbase.queryAll(tableName);		
	}
	
}




程序运行结果:
=======delete table======
Are you sure to delete(y/n)?y
test is delete!
=======create table======
Create table 'test' OK!
=======insert data=======
insert a data successful!
=======query all data=======
Rowkey: Ann
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160

Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052
=======query by rowkey=======
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160
=======delete a data=======
delete a data successful
Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052
分享到:
评论
1 楼 903855066 2014-07-11  
请问楼主,你输出的没有大量的zookeeper日志么

相关推荐

Global site tag (gtag.js) - Google Analytics