一 HBase基础API操作
2017-01-13 13:26
351 查看
概述:
HBase的主要客户端接口是由org.apache.hadoop.hbase.client 包中的HTable类提供的。通过这个类可以完成对HBase的CRUD(Create, Read, Update, Delete)操作。下面分别介绍。一 创建表 createTable 方法:
直接贴代码出来,复制到Eclipse中就可以使用。后面介绍其他API的时候只贴出具体函数的实现部分。其他相同部分不再贴出package com.sjmkt.hbase.api; 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.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.HBaseAdmin; /* * @notices 需要把环境中的core-site.xml,hbase-site.xml文件复制到resources目录下。加载的时候自动读取 * @author CJ-XS * */ public class HBaseAPI { // 定义静态变量方便以后重复使用 private final static String tablename = "HBase_Table"; public static void main(String[] args) throws Exception { // 设置本地hadoop的安装路径。安装了MapReduce插件的话就不需要这一行代码了,插件会帮我们设置好 System.setProperty("hadoop.home.dir", "E:\\jdk\\hadoop2.6.0\\"); // 创建所需的配置,这里会读取resources目录下的配置文件。可以打印出来 Configuration conf = HBaseConfiguration.create(); // 创建表 createTable(conf); } /** * @param conf * @throws Exception */ public static void createTable(Configuration conf) throws Exception{ Connection conn = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin)conn.getAdmin(); // 如果已经有这个彪了,先删除 if( admin.tableExists(TableName.valueOf(tablename)) ){ admin.disableTable(TableName.valueOf(tablename)); admin.deleteTable(TableName.valueOf(tablename)); } // 创建表描述器 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tablename)); // 添加列族 desc.addFamily(new HColumnDescriptor("Info")); // 创建表 admin.createTable(desc); // 关闭admin对象 admin.close(); } }
二 插入数据 Put方法
Put方法分为put单个对象和put<List>两种方式,大体是一样的。另外HBase中没有改这一说,所谓的改就是重新插入一条新的记录。public static void putData(Configuration conf) throws Exception{ // 获取 HTable对象 Connection conn = ConnectionFactory.createConnection(conf); HTable table = (HTable)conn.getTable(TableName.valueOf(tablename)); // 1 插入单个put对象 // 创建Put对象并制定行健 Put put = new Put(Bytes.toBytes("rowkey0001")); // 添加列的键值对 put.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("id"), Bytes.toBytes("1")); put.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("name"), Bytes.toBytes("XS")); put.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("age"), Bytes.toBytes("30")); // 插入hbase表 table.put(put); // 2 插入List<Put>对象 ArrayList<Put> puts = new ArrayList<Put>(); Put put1 = new Put(Bytes.toBytes("rowkey0002")); put1.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("id"), Bytes.toBytes("2")); put1.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("name"), Bytes.toBytes("JS")); put1.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("age"), Bytes.toBytes("31")); puts.add(put1); // Put put2 = new Put(Bytes.toBytes("rowkey0003")); put2.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("id"), Bytes.toBytes("3")); put2.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("name"), Bytes.toBytes("CC")); put2.addColumn(Bytes.toBytes("Info"), Bytes.toBytes("age"), Bytes.toBytes("32")); puts.add(put2); // 插入hbase表 table.put(puts); // 关闭对象 table.close(); }
三 获取数据 get/scan方法
get方法获取单行和获取多行数据public static void getData(Configuration conf) throws Exception{ // 获取 HTable对象 Connection conn = ConnectionFactory.createConnection(conf); HTable table = (HTable)conn.getTable(TableName.valueOf(tablename)); Get get = new Get(Bytes.toBytes("rowkey0002")); // 打开注释获取单行 // Result result = table.get(get); // // 输出结果 // for(Cell cell : result.rawCells()){ // String str = Bytes.toString(CellUtil.cloneRow(cell)); // str += "\t"; // str += Bytes.toString(CellUtil.cloneFamily(cell)); // str += "\t"; // str += Bytes.toString(CellUtil.cloneQualifier(cell)); // str += "\t"; // str += Bytes.toString(CellUtil.cloneValue(cell)); // System.out.println(str); // } // Get<list> Get get2 = new Get(Bytes.toBytes("rowkey0001")); ArrayList<Get> gets = new ArrayList<Get>(); gets.add(get); gets.add(get2); Result[] results = table.get(gets); for(Result rs : results){ for(Cell cell : rs.rawCells()){ String str = Bytes.toString(CellUtil.cloneRow(cell)); str += "\t"; str += Bytes.toString(CellUtil.cloneFamily(cell)); str += "\t"; str += Bytes.toString(CellUtil.cloneQualifier(cell)); str += "\t"; str += Bytes.toString(CellUtil.cloneValue(cell)); System.out.println(str); } System.out.println("--------------------------------------------"); } table.close(); } }
scan方法有三个接口函数。获取全表、获取某个列族、获取某个列族中的某列
public static void scanData(Configuration conf) throws Exception{ // 获取 HTable对象 Connection conn = ConnectionFactory.createConnection(conf); HTable table = (HTable)conn.getTable(TableName.valueOf(tablename)); // 获取整个列族 ResultScanner scanner = table.getScanner(Bytes.toBytes("Info")); for(Result res : scanner){ System.out.println(res); } System.out.println("---------------------------------------"); // 获取列族中的某个列 ResultScanner scanner1 = table.getScanner(Bytes.toBytes("Info"), Bytes.toBytes("name")); for(Result res : scanner1){ System.out.println(res); } System.out.println("---------------------------------------"); // 获取整个表 Scan scan = new Scan(); ResultScanner scanner2 = table.getScanner(scan); for(Result res : scanner2){ System.out.println(res); } table.close(); }
相关文章推荐
- VC error c2243:"类型转换" 转换存在,但无法访问
- C# WebService身份验证
- C++11 标准库实现 time2Str, Str2time
- 太原理工大学java课实验报告
- 39级台阶
- 二维码的生成和解析
- vs2010快捷键ctrl+shift+r
- centos 安装ant
- 响应式设计
- 对 Select 的各种操作(JQuery)
- ISO27001适用性-导图
- (一) rabbitmq在ubuntu14.04上的部署
- [00020]合并多个字典或映射
- scrollview嵌套listview默认显示顶端方法
- HTTPS 简介及使用官方工具 Certbot 配置 SSL 安全证书详细教程
- 寻找最小的k个数
- inflate方法
- 西游就是一段投股史
- 寻找最小的k个数
- JavaScript “函数重载”