`
aaron-han
  • 浏览: 26235 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

HBase Basic API Demo Code

阅读更多
又这么久... 太懒了...  放个很久以前写的HBase基本API的示例程序代码吧,涉及crud操作和几个简单的filter

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
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.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.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseAPIDemoCode {

	private static final Configuration config;
	private static final HTablePool tablePool;
	private static HBaseAdmin admin;

	static {
		config = HBaseConfiguration.create();
		tablePool = new HTablePool(config, 10);
		config.set("hbase.zookeeper.quorum", "master");
	}

	public static HBaseAdmin getHBaseAdmin() {
		if (config == null) {
			throw new RuntimeException("config null");
		}
		if (admin == null) {
			try {
				admin = new HBaseAdmin(config);
			} catch (MasterNotRunningException e) {
				e.printStackTrace();
			} catch (ZooKeeperConnectionException e) {
				e.printStackTrace();
			}
		}
		return admin;
	}

	public static HTable getHTable(final String tableName) {
		return (HTable) tablePool.getTable(tableName);
	}

	public static void printResult(final Result result) {
		byte[] family = null;
		System.out.println("RowKey: " + Bytes.toString(result.getRow()));
		for (KeyValue kv : result.raw()) {
			family = kv.getFamily();
			System.out.println("Column: " + Bytes.toString(family) + ":"
					+ Bytes.toString(kv.getQualifier()));
			System.out.println("Value: " + Bytes.toString(kv.getValue()));
		}
	}

	public static void testCreateTable(final String tableName,
			final String[] columnFamily) {
		HBaseAdmin admin = getHBaseAdmin();
		// if exists,delete first.
		try {
			if (admin.tableExists(tableName)) {
				admin.disableTable(tableName);
				admin.deleteTable(tableName);
			}

			HTableDescriptor htd = new HTableDescriptor(tableName);
			for (String column : columnFamily) {
				htd.addFamily(new HColumnDescriptor(column));
			}
			admin.createTable(htd);
			System.out.println(tableName + " created!");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void testDelTable(final String tableName) {
		HBaseAdmin admin = getHBaseAdmin();
		try {
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println(tableName + " deleted!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testInsertData(final String tableName) {
		HTable table = getHTable(tableName);
		int rowIdStart = 10001;
		int rowIdEnd = 10010;
		int index = 1;
		// put 10 cookies
		List<Put> putList = new ArrayList<Put>();
		for (; rowIdStart <= rowIdEnd; rowIdStart++) {
			Put put = new Put(Bytes.toBytes(String.valueOf(rowIdStart)));
			put.add(Bytes.toBytes("product"), Bytes.toBytes("proID"),
					Bytes.toBytes(String.valueOf(index)));
			put.add(Bytes.toBytes("product"), Bytes.toBytes("proTime"),
					Bytes.toBytes(String.valueOf(new Date())));
			if (index % 2 == 0) {
				put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
						Bytes.toBytes("coco"));
			} else {
				put.add(Bytes.toBytes("product"), Bytes.toBytes("proType"),
						Bytes.toBytes("milk"));
			}
			putList.add(put);
			index++;
		}
		try {
			table.put(putList);
			table.close();
			System.out.println("cookie data inserted!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testQueryAll(final String tableName) {
		HTable table = getHTable(tableName);
		try {
			ResultScanner scanner = table.getScanner(new Scan());
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 删除rowKey不存在的记录,HBase会吞掉该错误。
	public static void testDelARow(final String tableName, final String rowKey) {
		HTable table = getHTable(tableName);
		Delete del = new Delete(rowKey.getBytes());
		try {
			table.delete(del);
			table.close();
			System.out.println("RowKey: " + rowKey + " deleted!");
			testQueryAll(tableName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetARow(final String tableName, final String rowKey) {
		HTable table = getHTable(tableName);
		Get get = new Get(rowKey.getBytes());
		try {
			Result result = table.get(get);
			List<KeyValue> resultList = result.list();
			if (resultList == null) {
				System.out.println(rowKey + " null!");
				return;
			}
			printResult(result);
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetRowsByCookieType(final String tableName,
			final String cookieType) {
		HTable table = getHTable(tableName);
		Filter vfilter = new ValueFilter(CompareOp.EQUAL,
				new SubstringComparator(cookieType));
		Scan scan = new Scan();
		scan.setFilter(vfilter);
		try {
			ResultScanner scanner = table.getScanner(scan);
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void testGetRowsBySingleColumn(final String tableName,
			final String family, final String column, final String columnValue) {
		HTable table = getHTable(tableName);
		Filter vfilter = new SingleColumnValueFilter(Bytes.toBytes(family),
				Bytes.toBytes(column), CompareOp.EQUAL,
				Bytes.toBytes(columnValue));
		Scan scan = new Scan();
		scan.setFilter(vfilter);
		try {
			ResultScanner scanner = table.getScanner(scan);
			for (Result result : scanner) {
				printResult(result);
			}
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		final String tableName = "cookie";
		// testCreateTable(tableName, new String[] { "product", "sale" });
		// testInsertData(tableName);
		// testDelTable(tableName);
		// testQueryAll(tableName);
		// testDelARow(tableName, "10010");
		// testGetARow(tableName, "10011");
		// testGetRowsByCookieType(tableName, "milk");
		testGetRowsBySingleColumn(tableName, "product", "proType", "coco");
	}
}


0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics