大數據與人工智能入門到放棄(10 回顧篇 HBase java api操作)

記:

最近在公司寫單元測試,因爲要涉及到周任務達標問題,所以天天加班,今天週末,安排一波Hbase完全分佈式的搭建安裝

HBase java api操作

在很多情況下,操作hbase不是在命令行直接操作的,所以有了很多api,這裏我用java api進行操作。所以打開eclipse。

1.解壓hbase,然後把lib下的jar包拷貝到eclipse中;

2.選中那些jar包,bulid path一波

3.直接上代碼

package com.junxi.hbase;

import java.io.IOException;
import java.io.InterruptedIOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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.RetriesExhaustedWithDetailsException;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class HBaseDemo {

	HBaseAdmin admin = null;
	String tb = "person";
	HTable table = null;
	
	@Before
	public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "master,node1,node2");
		admin = new HBaseAdmin(conf);
		table = new HTable(conf, tb.getBytes());
	}
	
	@Test
	public void createTable() throws IOException {
		HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tb));
		HColumnDescriptor family = new HColumnDescriptor("cf".getBytes());
	
		desc.addFamily(family);
		
		if(admin.tableExists(tb)) {
			admin.disableTable(tb);
			admin.deleteTable(tb);
		}
		
		admin.createTable(desc);
	}
	
	@Test
	public void insert() throws RetriesExhaustedWithDetailsException, InterruptedIOException {
		Put put = new Put("111".getBytes());
		put.add("cf".getBytes(), "name".getBytes(), "zhangsan".getBytes());
		put.add("cf".getBytes(), "age".getBytes(), "11".getBytes());
		put.add("cf".getBytes(), "sex".getBytes(), "1".getBytes());
		table.put(put);
	}
	
	@Test
	public void get() throws IOException {
		
		Get get = new Get("111".getBytes());
		Result result = table.get(get);
		Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
		Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
		Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
		System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
	}
	
	@Test
	public void scan() throws IOException {
		Scan scan = new Scan();
		ResultScanner res = table.getScanner(scan);
		for (Result result : res) {
			Cell cell1 = result.getColumnLatestCell("cf".getBytes(), "name".getBytes());
			Cell cell2 = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
			Cell cell3 = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell1)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell2)));
			System.out.println(Bytes.toString(CellUtil.cloneValue(cell3)));
		}
	}
	
	@After
	public void destory() throws IOException {
		if(admin != null) {
			admin.close();
		}
	}
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章