Hbase 的Java API 操作
package com.wzt.dao; //package hbaseExec2; /* * 创建一个students表,并进行相关操作 */ import java.io.IOException; import java.util.ArrayList; import java.util.List; 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.KeyValue; 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.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HBaseJavaAPI { // 声明静态配置 private static Configuration conf = null; static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "cloud5:2181,cloud6:2181,cloud7:2181"); } //判断表是否存在 private static boolean isExist(String tableName) throws IOException { HBaseAdmin hAdmin = new HBaseAdmin(conf); return hAdmin.tableExists(tableName); } // 创建数据库表 public static void createTable(String tableName, String[] columnFamilys) throws Exception { // 新建一个数据库管理员 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { System.out.println("表 "+tableName+" 已存在!"); System.exit(0); } else { // 新建一个students表的描述 HTableDescriptor tableDesc = new HTableDescriptor(tableName); // 在描述里添加列族 for (String columnFamily : columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(columnFamily)); } // 根据配置好的描述建表 hAdmin.createTable(tableDesc); System.out.println("创建表 "+tableName+" 成功!"); } } // 删除数据库表 public static void deleteTable(String tableName) throws Exception { // 新建一个数据库管理员 HBaseAdmin hAdmin = new HBaseAdmin(conf); if (hAdmin.tableExists(tableName)) { // 关闭一个表 hAdmin.disableTable(tableName); hAdmin.deleteTable(tableName); System.out.println("删除表 "+tableName+" 成功!"); } else { System.out.println("删除的表 "+tableName+" 不存在!"); System.exit(0); } } // 添加一条数据 public static void addRow(String tableName, String row, String columnFamily, String column, String value) throws Exception { HTable table = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row));// 指定行 // 参数分别:列族、列、值 put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); } // 删除一条(行)数据 public static void delRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Delete del = new Delete(Bytes.toBytes(row)); table.delete(del); } // 删除多条数据 public static void delMultiRows(String tableName, String[] rows) throws Exception { HTable table = new HTable(conf, tableName); List<Delete> delList = new ArrayList<Delete>(); for (String row : rows) { Delete del = new Delete(Bytes.toBytes(row)); delList.add(del); } table.delete(delList); } // 获取一条数据 public static void getRow(String tableName, String row) throws Exception { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); // 输出结果,raw方法返回所有keyvalue数组 for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("时间戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out.print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } // 获取所有数据 public static void getAllRows(String tableName) throws Exception { HTable table = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); // 输出结果 for (Result result : results) { for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("时间戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out .print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } } // 主函数 public static void main(String[] args) { try { String tableName = "student"; // 第一步:创建数据库表:“student” String[] columnFamilys = { "info", "course" }; HBaseJavaAPI.createTable(tableName, columnFamilys); // 第二步:向数据表的添加数据 // 添加第一行数据 if (isExist(tableName)) { HBaseJavaAPI.addRow(tableName, "zpc", "info", "age", "20"); HBaseJavaAPI.addRow(tableName, "zpc", "info", "sex", "boy"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "china", "97"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "math", "128"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "english", "85"); // 添加第二行数据 HBaseJavaAPI.addRow(tableName, "henjun", "info", "age", "19"); HBaseJavaAPI.addRow(tableName, "henjun", "info", "sex", "boy"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "china","90"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "math","120"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "english","90"); // 添加第三行数据 HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "age", "18"); HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "sex","girl"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "china","100"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "math","100"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "english","99"); // 第三步:获取一条数据 System.out.println("**************获取一条(zpc)数据*************"); HBaseJavaAPI.getRow(tableName, "zpc"); // 第四步:获取所有数据 System.out.println("**************获取所有数据***************"); HBaseJavaAPI.getAllRows(tableName); // 第五步:删除一条数据 System.out.println("************删除一条(zpc)数据************"); HBaseJavaAPI.delRow(tableName, "zpc"); HBaseJavaAPI.getAllRows(tableName); // 第六步:删除多条数据 System.out.println("**************删除多条数据***************"); String rows[] = new String[] { "qingqing","xiaoxue" }; HBaseJavaAPI.delMultiRows(tableName, rows); HBaseJavaAPI.getAllRows(tableName); // 第七步:删除数据库 System.out.println("***************删除数据库表**************"); HBaseJavaAPI.deleteTable(tableName); System.out.println("表"+tableName+"存在吗?"+isExist(tableName)); } else { System.out.println(tableName + "此数据库表不存在!"); } } catch (Exception e) { e.printStackTrace(); } } }
from http://www.2cto.com/database/201503/381955.html
相关推荐
HBase Java API操作数据库示例代码-HBaseDemo.rar HBase Java API操作数据库示例代码-HBaseDemo.rar HBase Java API操作数据库示例代码-HBaseDemo.rar
HBase Java API类介绍
hbase java api 访问 增加修改删除(一) 详情请看:http://blog.csdn.net/wild46cat/article/details/53306621
Hbase笔记 —— 利用JavaAPI的方式操作Hbase数据库(往hbase的表中批量插入数据)
hbase java api 访问 查询、分页 详情请看:http://blog.csdn.net/wild46cat/article/details/53321409
使用JavaAPI实现HBase的ddl(创建表、删除表、修改表(添加列族等))、dml(添加数据、删除数据)、dql(查询数据(get、scan))等操作 除此之外还包含一些其他操作:命名空间的应用、快照的应用等 对应(《HBase...
java 端开发 hbase 所需jar包
使用Java API连接虚拟机HBase并进行数据库操作,Java源代码
11-HBase Java API编程实践1
Hbase Java API详解 是开发hbase 数据库很好的收 对初学者有好友
HBase是Hadoop的数据库,能够对大数据提供随机、实时读写访问。他是开源的,分布式的,多版本的,面向列的,存储模型。
HBASE
最好用的开发api
Hadoop平台技术 5.4.2 HBase Java API应用-教学课件
Hbase调用JavaAPI实现批量导入操作.docx
自行制作的HBase 1.2.0 Javadoc API CHM版本。内容抽取自官方站点网页
HBase-APi操作demo
Hbase的JavaAPI 包括表的增删改查
在集群中创建java项目调用api来操作hbase,主要涉及对hbase的创建表格,删除表格,插入数据,删除数据,查询一条数据,查询所有数据等操作。 具体流程如下: 1.创建项目 2.获取jar包到项目的lib目录下(这边试用的事...
2019-8____2019-10工作期间的java版hiveApi,hbaseApi和spark版本的hiveApi,hbaseApi