hbase java API跟新数据,创建表
package hbaseCURD;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;
public class test {
public static void main(String[] args) throws IOException {
TableManager tm = new TableManager();
tm.getConf();
// tm.createTable("testtable","cf1","cf2");
HTable mytable=tm.getTableObj("testtable");
// Put put = new Put(Bytes.toBytes("row1"));
// put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("cf1ddfddvalue"));
// put.add(Bytes.toBytes("cf2"), Bytes.toBytes("c2"), Bytes.toBytes("cf2v3333alue"));
// mytable.put(put);
//查询
Get get = new Get(Bytes.toBytes("row1"));
Result result = mytable.get(get);
System.out.println("get result:" + Bytes.toString(result.getValue(Bytes.toBytes("cf1"), Bytes.toBytes("c1"))));
//Result[] result = table.get(List<Get>);//查询指定Rowkey的多条记录
}
}
package hbaseCURD;
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.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class TableManager {
private static Configuration conf;
// 设置集群的配置信息
public void getConf() {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master,slave1,slave2");
conf.set("hbase.master", "master:60000");
}
// 获取表对象
public HTable getTableObj(String tablename) {
HTable mytable = null;
try {
mytable = new HTable(conf, tablename);
} catch (IOException e) {
e.printStackTrace();
}
return mytable;
}
//
public void createTable(String tableName, String... args) throws MasterNotRunningException, ZooKeeperConnectionException {
// args数组保存的是列族
HBaseAdmin admin = new HBaseAdmin(conf);
// 创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
for (String st : args) {
htd.addFamily(new HColumnDescriptor(st));
}
try {
admin.createTable(htd);
} catch (IOException e) {
e.printStackTrace();
}
}
public void deleteTable(String tableName) {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
// 创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void modifyTable(String tableName,String newcoloumf) {
try {
HBaseAdmin admin = new HBaseAdmin(conf);
// 创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
admin.disableTable(Bytes.toBytes(tableName));
admin.modifyColumn(tableName, new HColumnDescriptor("cf1"));
admin.enableTable(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Hbase的访问方式
1、Native Java API:最常规和高效的访问方式;
2、HBase Shell:HBase的命令行工具,最简单的接口,适合HBase管理使用;
3、Thrift Gateway:利用Thrift序列化技术,支持C++,PHP,Python等多种语言,适合其他异构系统在线访问HBase表数据;
4、REST Gateway:支持REST 风格的Http API访问HBase, 解除了语言限制;
5、MapReduce:直接使用MapReduce作业处理Hbase数据;
6、使用Pig/hive处理Hbase数据。
常用Java API的用法:
1、加载配置
[java] view plaincopy在CODE上查看代码片派生到我的代码片
Configuration config = HBaseConfiguration.create();
//可以自定义配置,也可以从自定义配置文件中读取
/*config.set(“hbase.zookeeper.property.clientPort”, “4181”);
config.set(“hbase.zookeeper.quorum”, “hadoop.datanode5.com,hadoop.datanode2.com,hadoop.datanode3.com”);
config.set(“hbase.master”, “hadoop.datanode3.com\:600000”);*/
2、表的创建、表信息修改、表删除
[java] view plaincopy在CODE上查看代码片派生到我的代码片
HBaseAdmin admin = new HBaseAdmin(config);
//创建表
HTableDescriptor htd = new HTableDescriptor(tableName);
htd.addFamily(new HColumnDescriptor(“cf1”));
htd.addFamily(new HColumnDescriptor(“cf2”));
admin.createTable(htd);
//修改表信息
admin.disableTable(tableName);
// modifying existing ColumnFamily
admin.modifyColumn(tableName, new HColumnDescriptor(“cf1”));
admin.enableTable(tableName);
//删除表
admin.disableTable(Bytes.toBytes(tableName));
admin.deleteTable(Bytes.toBytes(tableName));
3、添加记录
[java] view plaincopy在CODE上查看代码片派生到我的代码片
/** 在多次使用时,建议用HTablePool
HTable table = new HTable(config, tableName);
=>
HTablePool pool = new HTablePool(config, 1000);
HTableInterface table = pool.getTable(tableName);*/
HTable table = new HTable(config, tableName);
/**
* 在插入操作时,默认不适用任何缓存
* 可自定义使用缓存,以及缓存大小
* 每个任务最后需要手工调用 flushCommits();
*/
/*table.setAutoFlush(false);
table.setWriteBufferSize(1024);*/
Put put1 = new Put(Bytes.toBytes(rowKey));
if (ts == 0) {
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
} else {
//自定义版本时,从自定义的版本号,类型为long
put1.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), ts,Bytes.toBytes(value));
}
table.put(put1);
//table.flushCommits();
4、查询,根据Rowkey查询
[java] view plaincopy在CODE上查看代码片派生到我的代码片
Get get1 = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get1);
System.out.println(“get result:” + Bytes.toString(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier))));
Result[] result = table.get(List);//查询指定Rowkey的多条记录
5、查询,指定条件和rowkey区间查询
[java] view plaincopy在CODE上查看代码片派生到我的代码片
Scan scan = new Scan();
//默认缓存大小为1,设置成一个合理的值,可以减少scan过程中next()的时间开销,代价是客户端的内存
scan.setCaching(500);
scan.setCacheBlocks(false);
//根据startRowKey、endRowKey查询
//Scan scan = new Scan(Bytes.toBytes(“startRowKey”), Bytes.toBytes(“endRowKey”));
//rowKey之外的过滤条件,在List中可以add;
/**List filters = new ArrayList();
Filter filter = new SingleColumnValueFilter(“familyName”.getBytes(),
“qualifierName”.getBytes(),
CompareOp.EQUAL,
Bytes.toBytes(“value”));
filters.add(filter);
scan.setFilter(new FilterList(filters));*/
ResultScanner scanner = table.getScanner(scan);
System.out.println(“scan result list:”);
for (Result result : scanner) {
System.out.println(Bytes.toString(result.getRow()));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes(“data”), Bytes.toBytes(“data1”))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes(“data”), Bytes.toBytes(“data2”))));
}
scanner.close();
参考:
1、http://www.taobaotest.com/blogs/1605
2、http://abloz.com/hbase/book.html#data_model_operations(官网示例)
版权声明:本文为博主原创文章,未经博主允许不得转载。
hbase java API跟新数据,创建表的更多相关文章
- 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表
基于CDH5.x 下面使用eclipse 操作hive .使用java通过jdbc连接HIVESERVICE 创建表 import java.sql.Connection; import java.s ...
- hbase java api样例(版本1.3.1,新API)
hbase版本:1.3.1 目的:HBase新API的使用方法. 尝试并验证了如下几种java api的使用方法. 1.创建表 2.创建表(预分区) 3.单条插入 4.批量插入 5.批量插入(客户端缓 ...
- 大数据学习系列之三 ----- HBase Java Api 图文详解
版权声明: 作者:虚无境 博客园出处:http://www.cnblogs.com/xuwujing CSDN出处:http://blog.csdn.net/qazwsxpcm 个人博客出处:http ...
- Hbase Java API详解
HBase是Hadoop的数据库,能够对大数据提供随机.实时读写访问.他是开源的,分布式的,多版本的,面向列的,存储模型. 在讲解的时候我首先给大家讲解一下HBase的整体结构,如下图: HBase ...
- HBase 学习之路(六)——HBase Java API 的基本使用
一.简述 截至到目前(2019.04),HBase 有两个主要的版本,分别是1.x 和 2.x ,两个版本的Java API有所不同,1.x 中某些方法在2.x中被标识为@deprecated过时.所 ...
- HBase 系列(六)——HBase Java API 的基本使用
一.简述 截至到目前 (2019.04),HBase 有两个主要的版本,分别是 1.x 和 2.x ,两个版本的 Java API 有所不同,1.x 中某些方法在 2.x 中被标识为 @depreca ...
- HBase Java API使用(一)
前言 1. 创建表:(由master完成) 首先需要获取master地址(master启动时会将地址告诉zookeeper)因而客户端首先会访问zookeeper获取master的地址 client和 ...
- 【Hbase学习之三】Hbase Java API
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-2.6.5 hbase-0.98.12.1-h ...
- Hbase(六) hbase Java API
一. 几个主要 Hbase API 类和数据模型之间的对应关系: 1. HBaseAdmin关系: org.apache.hadoop.hbase.client.HBaseAdmin作用:提供了一个接 ...
随机推荐
- linux 7- - watch,free,mpstat,vmstat,iostat,pidstat,df,du
十八. 和系统运行状况相关的Shell命令: 1. Linux的实时监测命令(watch): watch 是一个非常实用的命令,可以帮你实时监测一个命令的运行结果,省得一遍又一遍的 ...
- 在Nginx中做负载均衡配置的实例讲解
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法. 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某 ...
- Android进程间通信(IPC)机制Binder简要介绍和学习计划【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6618363 在Android系统中,每一个应用程序都是由一些Activity和Ser ...
- Android硬件抽象层(HAL)概要介绍【转】
本文转载自:http://blog.csdn.net/luoshengyang/article/details/6567257 Android的硬件抽象层,简单来说,就是对Linux内核驱动程序的封装 ...
- 本地建立SVN服务器
想在自己电脑上搭建SVN服务器,于是有以下步骤. 首先明确SVN服务包括服务器和客户端,平时听到的TortoiseSVN就是一个客户端. 首先下载两个软件,服务器端我使用的是VisualSVN,版本是 ...
- jquery 实现智能炫酷的翻页相册效果
jquery 实现智能炫酷的翻页相册效果巧妙的运用 Html 的文档属性,大大减少jquery 的代码量,实现了智能炫酷的翻页相册.兼容性很好,实现了代码与标签的完全分离1. [代码]jquery ...
- iis常见问题解决
iis7以上版本部署4.0框架项目常见问题解决 配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (overrideModeDefault=&quo ...
- win7系统查看端口占用情况
我们在启动应用或者在开发的时候的时候经常发现我们需要使用的端口被别的程序占用,但是我们又不知道是被谁占用,这时候我们需要找出“真凶”,如何做到呢? 方法/步骤 开始---->运行----&g ...
- strnpy函数
函数原型: char * strncpy ( char * destination, const char * source, size_t num ); 功能:从字符串source中复制 num个字 ...
- 利用Python进行文章特征提取(二)
本篇blog是利用Python进行文章特征提取的续篇,主要介绍构建带TF-IDF权重的文章特征向量. In [1]: # 带TF-IDF权重的扩展词库 # 在第一篇文档里 主要是利用词库模型简单判断单 ...