HBase的JAVA API

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;
import org.apache.hadoop.hbase.thrift.generated.Hbase.Processor.createTable;
import org.apache.hadoop.hbase.thrift.generated.Hbase.Processor.deleteTable;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class HBaseApp {
	

	private static final String TABLE_NAME = "table2";
	private static final String FAMILY_NAME = "family2";
	private static final String ROW_KEY = "rowkey2";
	public static void main(String[] args) throws Exception {

		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
	    //使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.quorum", "hadoop,hadoop1,hadoop2");
		//创建表,删除表使用HBaseAdmin
		final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
		//createTable(hBaseAdmin);
		//deleteTable(hBaseAdmin);
		
		//插入,查询,遍历使用HTable
		final HTable hTable = new HTable(conf, TABLE_NAME);
	
		//putRecord(hTable);
		getRecord(hTable);
		//scanTable(hTable);	
		hTable.close();
	}
	
	
	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);//keyvalues={rowkey2/family2:age/1522807817820/Put/vlen=2/seqid=0}
		final byte[] value = result.getValue(FAMILY_NAME.getBytes(), "age".getBytes());//25
		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);
		}
	}
}


猜你喜欢

转载自blog.csdn.net/sofuzi/article/details/80913715
今日推荐