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 命令查看
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值