官方文档:http://hbase.apache.org/book.html

java简单操作hbase的表

 import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.junit.Before;
import org.junit.Test; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random; /**
* Created by Edward on 2016/6/19.
*/
public class TestHbase { public static Configuration conf = null;
public static TableName table = TableName.valueOf("phone");
public static Random random = new Random(); @Before
public void setup()
{
//通过zookeeper集群,访问hbase
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "node1,node2,node3");
System.out.println("setup");
} @Test
public void createTable() throws IOException {
HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
if(hBaseAdmin.tableExists(table)) {
hBaseAdmin.disableTable(table);
hBaseAdmin.deleteTable(table);
}
//表
HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
//列族 cf1
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor("cf1");
hColumnDescriptor.setBlockCacheEnabled(true);
hColumnDescriptor.setBlocksize(128000);
hColumnDescriptor.setMaxVersions(10);
//表增加列族
hTableDescriptor.addFamily(hColumnDescriptor);
hBaseAdmin.createTable(hTableDescriptor);
} @Test
/**
* 插入数据
*/
public void insert() throws IOException { //创建htable对象
HTable hTable = new HTable(conf, this.table);
ArrayList<Put> list = new ArrayList<Put>(); for(int i = 0; i<1000; i++)
{
//row_key
Put put = new Put(String.valueOf(i).getBytes());
//column,value
put.add("cf1".getBytes(),"name".getBytes(),"ls".getBytes());
put.add("cf1".getBytes(),"age".getBytes(), String.valueOf(i%100).getBytes());
//把每个put对象放到列表中
list.add(put);
}
//把信息放到表中
hTable.put(list);
} @Test
/**
* 查询
*/
public void search() throws IOException
{ HTable hTable = new HTable(conf, this.table);
// 创建row_key对应的get对象
Get get = new Get("123".getBytes());
// 获取get结果
Result result = hTable.get(get);
//获取 column latest cell
Cell columnLatestCell = result.getColumnLatestCell("cf1".getBytes(), "name".getBytes());
//使用 CellUtil 获取值
byte[] bytes = CellUtil.cloneValue(columnLatestCell);
System.out.println(new String(bytes));
} @Test
/**
* 通过scan方法获取数据
* **/
public void search1() throws IOException
{
HTable hTable = new HTable(conf, this.table);
//设置scan范围
Scan scan = new Scan("400".getBytes(),"450".getBytes());
//通过scan得到result scanner
ResultScanner scanner = hTable.getScanner(scan);
//使用迭代器
Iterator<Result> iterator = scanner.iterator(); while(iterator.hasNext())
{
Result result= iterator.next();
Cell columnLatestCell = result.getColumnLatestCell("cf1".getBytes(), "age".getBytes());
//获取列族中列对应的值
byte[] bytes = CellUtil.cloneValue(columnLatestCell);
//获取row_key
byte[] bytes1 = CellUtil.cloneRow(columnLatestCell);
System.out.println(new String(bytes)+" "+new String(bytes1));
}
}
}

HBase的简单java操作的更多相关文章

  1. java操作Hbase实例

    所用HBase版本为1.1.2,hadoop版本为2.4 /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.u ...

  2. Hadoop学习(6)-HBASE的安装和命令行操作和java操作

    使用HABSE之前,要先安装一个zookeeper 我以前写的有https://www.cnblogs.com/wpbing/p/11309761.html 先简单介绍一下HBASE HBASE是一个 ...

  3. 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  4. (转)Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  5. Hbase深入学习(六) Java操作HBase

    Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...

  6. Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询

    1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...

  7. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  8. 【Hbase三】Java,python操作Hbase

    Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...

  9. HBase的java操作,最新API。(查询指定行、列、插入数据等)

    关于HBase环境搭建和HBase的原理架构,请见笔者相关博客. 1.HBase对java有着较优秀的支持,本文将介绍如何使用java操作Hbase. 首先是pom依赖: <dependency ...

随机推荐

  1. 前端 css+js实现返回顶部功能

    描述: 本文主要是讲,通过css+js实现网页中的[返回顶部]功能. 实现代码: HTML: <div> <button onclick="returnTop()" ...

  2. 为什么不要使用 select * from xxx (oracle 亲测)

    打开已用时间set timing on;create table users(id number(20), name varchar2(20), password varchar2(20));inse ...

  3. 2cmd 窗口 javac 错误:编码GBK的不可映射字符

    错误截图: 解决办法:第一步 第二步:

  4. redis介绍(7)高级用法

    redis的过期策略以及内存淘汰机制 分析:这个问题其实相当重要,到底redis有没用到家,这个问题就可以看出来.比如你redis只能存5G数据,可是你写了10G,那会删5G的数据.怎么删的,这个问题 ...

  5. winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条

    因为需要使用DrawMode自行DrawItem,所以需要将DrawMode设置为OwnerDrawVarialbe或OwnerDrawFixed模式,代码如下: private void listB ...

  6. leetCode Detect Capital

    1.问题描述 Given a word, you need to judge whether the usage of capitals in it is right or not. We defin ...

  7. AIX常用命令学习(一)

    1.prtconf命令 查看AIX主机的结构特征状态 语法: prtconf [ -c ] [ -k ] [ -L ] [ -m ] [ -s ] [ -v ] Flags: -c  Displays ...

  8. Struts-config.xml配置文件《action-mappings》元素的详解

    原文地址:http://blog.163.com/sara1124@126/blog/static/11291097020105125537114/ action-mappings 该元素用于将Act ...

  9. 教你如何获取ipa包中的开发文件

    教你如何获取ipa包中的开发文件 1. 从iTunes获取到ipa包 2. 修改ipa包然后获取里面的开发文件

  10. 浅析NSTextContainer

    浅析NSTextContainer TextKit中的NSTextContainer有点晦涩难懂,如果想用TextKit实现文本分页的效果,你是必须要使用NSTextContainer的...... ...