hbase java api

阅读更多
一、
配置
windows hosts
添加Linux映射

否则,运行时报错,unknowHostException

二、

	package com.study.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.Put;
import org.apache.hadoop.hbase.client.Result;
import org.junit.Test;

public class HbaseDemo {

	@Test
	public void testDropTable() throws Exception {
		
		// 配置文件
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HBaseAdmin admin = new HBaseAdmin(conf);

		// 删除表,删除前需先禁用表,然后才会删除,否则,无效
		admin.disableTable("hbase_test".getBytes());
		admin.deleteTable("hbase_test".getBytes());
		
		admin.close();
	}
	
	/**
	 * 删除数据
	 * @throws Exception
	 */
	@Test
	public void testDeleteData() throws Exception{
		
		// 配置文件
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HTable table = new HTable(conf, "hbase_test".getBytes());

		// 指定删除数据的行健:rk1
		Delete delete = new Delete("rk1".getBytes());
		table.delete(delete);
		
		table.close();
	}
	
	/**
	 * 修改数据
	 * 与插入数据一样,只是列中的value变化了
	 * @throws Exception
	 */
	@Test
	public void testUpdateData() throws Exception{
		
		// 配置文件
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HTable table = new HTable(conf, "hbase_test".getBytes());
		
		// 插入一行数据:行健
		Put put = new Put("rk1".getBytes());
		// 添加列名称:根据列族名称,添加列名、列中值
		put.add("cf1".getBytes(), "cell".getBytes(), "cellValue".getBytes());
		table.put(put);
		
		// 关闭连接
		table.close();
	}
	
	
	/**
	 * 获取某列的数据
	 * @throws Exception
	 */
	@Test
	public void testGetCell() throws Exception{
		
		// 配置文件
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HTable table = new HTable(conf, "hbase_test".getBytes());
		
		// 获取行健rk1的数据
		Get get = new Get("rk1".getBytes());
		// 获取列族cf1中cell列中的数据
		get.addColumn("cf1".getBytes(), "cell".getBytes());
		// 数据存储为二进制格式,需要由byte[] --> string
		Result result = table.get(get);
		byte[] bytes = result.getValue("cf1".getBytes(), "cell".getBytes());
		System.out.println("列中数据为:"+new String(bytes));
		table.close();
	}
	
	
	/**
	 * 插入数据
	 * @throws Exception
	 */
	@Test
	public void testInsertData() throws Exception{
		
		// 配置文件
		Configuration conf = new Configuration();
		conf.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HTable table = new HTable(conf, "hbase_test".getBytes());
		
		// 插入一行数据:行健
		Put put = new Put("rk1".getBytes());
		// 添加列名称:根据列族名称,添加列名、列中值
		put.add("cf1".getBytes(), "cell".getBytes(), "value".getBytes());
		table.put(put);
		
		// 关闭连接
		table.close();
		// 进入hbase :  hbase shell ; scan "hbase_test" ; 查看数据已插入表中
	}
	
	
	/**
	 * windows 上 hosts 文件中添加  linux01 linux02 linux03 的映射
	 * 否则,运行报错,unknownHostException linux02 
	 */
	@Test
	public void testCreate() throws Exception{
		// 配置文件
		Configuration c = new Configuration();
		// zookeeper 
		c.set("hbase.zookeeper.quorum", "linux01:2181,linux02:2181,linux03:2181");
		HBaseAdmin admin = new HBaseAdmin(c);
		
		// 表名称
		TableName name = TableName.valueOf("hbase_test".getBytes());
		HTableDescriptor table = new HTableDescriptor(name );
		// 列族
		HColumnDescriptor family = new HColumnDescriptor("cf1");
		table.addFamily(family);
		HColumnDescriptor family1 = new HColumnDescriptor("cf2");
		table.addFamily(family1);
		
		admin.createTable(table);
		// 关闭连接
		admin.close();
		// 启动 hbase shell 
		// list 命令查看
	}
}





你可能感兴趣的:(hbase)