Hadoop之——Java操作HBase,hadoopjavahbase


转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46463617

不多说,直接上代码,大家都懂得

package hbase;

import java.io.IOException;

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.TableDescriptors;
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;
/**
 * Java操作HBase
 * @author liuyazhuang
 */
public class HBaseApp {
	//创建表、插入记录、查询一条记录、遍历所有的记录、删除表
	public static final String TABLE_NAME = "table1";
	public static final String FAMILY_NAME = "family1";
	public static final String ROW_KEY = "rowkey1";
	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://hadoop0:9000/hbase");
		//使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.quorum", "hadoop0");
		//创建表、删除表使用HBaseAdmin
		final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
		createTable(hBaseAdmin);
		
		//插入记录、查询一条记录、遍历所有的记录HTable
		final HTable hTable = new HTable(conf, TABLE_NAME);
		//putRecord(hTable);
		//getRecord(hTable);
		//scanTable(hTable);
		hTable.close();
		
		//deleteTable(hBaseAdmin);
	}
	//扫描表记录
	private static void scanTable(final HTable hTable) throws IOException {
		Scan scan = new Scan();
		final ResultScanner scanner = hTable.getScanner(scan);
		for (Result result : scanner) {
			final byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());
			System.out.println(result+"\t"+new String(value));
		}
	}
	//获取记录
	private static void getRecord(final HTable hTable) throws IOException {
		Get get = new Get(ROW_KEY.getBytes());
		final Result result = hTable.get(get);
		final byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());
		System.out.println(result+"\t"+new String(value));
	}
	//插入记录
	private static void putRecord(final HTable hTable) throws IOException {
		Put put = new Put(ROW_KEY.getBytes());
		put.add(FAMILY_NAME.getBytes(), "age".getBytes(), "25".getBytes());
		hTable.put(put);
	}
	//删除表
	private static void deleteTable(final HBaseAdmin hBaseAdmin)
			throws IOException {
		hBaseAdmin.disableTable(TABLE_NAME);
		hBaseAdmin.deleteTable(TABLE_NAME);
	}
	//创建表
	private static void createTable(final HBaseAdmin hBaseAdmin)
			throws IOException {
		if(!hBaseAdmin.tableExists(TABLE_NAME)){
			HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE_NAME);
			HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);
			tableDescriptor.addFamily(family);
			hBaseAdmin.createTable(tableDescriptor);
		}
	}
}


相关内容