HBase JAVA API 开发
来源:互联网 发布:java架构师工作 编辑:程序博客网 时间:2024/06/05 14:28
- 本地hosts配置:配置HBase所在集群的hosts,直接复制/etc/hosts下面的内容
- 到HBase安装节点下载/etc/hbase/conf/hbase-site.xml,复制到JAVA MAVEN项目resources目录
- pom文件
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client --><dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.2</version></dependency>
4.JAVA代码
package cn.gcks.repository;import cn.gcks.util.CommonUtils;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.*;import org.apache.hadoop.hbase.client.*;import org.apache.hadoop.hbase.util.Bytes;import java.io.IOException;/** * HBase API */public class HBaseRepository { public static Configuration configuration; public static Connection connection; public static Admin admin; // 初始化HBase连接 static { configuration = HBaseConfiguration.create(); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } //建表 public static void createTable(String tableNmae, String[] cols) throws IOException { TableName tableName = TableName.valueOf(tableNmae); if (admin.tableExists(tableName)) { System.out.println("talbe is exists!"); } else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for (String col : cols) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } close(); } //插入数据 public static void insterRow(String tableName, String rowkey, String colFamily, String col, String val) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val)); table.put(put); //批量插入 // /* List<Put> putList = new ArrayList<Put>(); puts.add(put); table.put(putList);*/ table.close(); close(); } /** * 根据行键、列族、列限定符查找数据 * * @param tableName 表名 * @param rowkey 行键 * @param colFamily 列族(默认值为null) * @param qualifier 列限定符(默认值为null) * @throws Exception */ public static Result getData(String tableName, String rowkey, String colFamily, String qualifier) throws Exception { Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowkey)); if (CommonUtils.isNotBlank(colFamily)) { if (CommonUtils.isNotBlank(qualifier)) { // 获取指定列数据 get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier)); } else { // 获取指定列族数据 get.addFamily(Bytes.toBytes(colFamily)); } } Result result = table.get(get); table.close(); close(); return result; } //批量查找数据 public static void scanData(String tableName, String startRow, String stopRow) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); //scan.setStartRow(Bytes.toBytes(startRow)); // scan.setStopRow(Bytes.toBytes(stopRow)); ResultScanner resultScanner = table.getScanner(scan); for (Result result : resultScanner) { showCell(result); } table.close(); close(); } /** * 根据行键、列族、列限定符删除数据 * * @param tableName 表名 * @param rowkey 行键 * @param colFamily 列族(默认值为null) * @param qualifier 列限定符(默认值为null) * @throws Exception */ public static void deleRow(String tableName, String rowkey, String colFamily, String qualifier) throws IOException { Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowkey)); if (CommonUtils.isNotBlank(colFamily)) { if (CommonUtils.isNotBlank(qualifier)) { // 删除指定列 delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(qualifier)); } else { //删除指定列族 delete.addFamily(Bytes.toBytes(colFamily)); } } table.delete(delete); // 批量删除 // /* List<Delete> deleteList = new ArrayList<Delete>(); deleteList.add(delete); table.delete(deleteList);*/ table.close(); close(); } //查看已有表 public static void listTables() throws IOException { HTableDescriptor hTableDescriptors[] = admin.listTables(); for (HTableDescriptor hTableDescriptor : hTableDescriptors) { System.out.println(hTableDescriptor.getNameAsString()); } close(); } //删表 public static void deleteTable(String tableName) throws IOException { TableName tn = TableName.valueOf(tableName); if (admin.tableExists(tn)) { admin.disableTable(tn); admin.deleteTable(tn); } close(); } //格式化输出 public static void showCell(Result result) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("==========================="); System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)) + " "); System.out.println("Timetamp:" + cell.getTimestamp() + " "); System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.println("column qualifier:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " "); System.out.println("==========================="); } } public static void close() { try { if (null != admin) admin.close(); if (null != connection) connection.close(); } catch (IOException e) { e.printStackTrace(); } }}
0 0
- HBase JAVA API 开发
- Hbase API开发实例(Java版)
- java API开发hbase启动错误解决办法
- Hbase的Java开发API操作实现
- hbase开发,hbase表操作及其java api实现
- hbase开发,hbase表操作及其java api实现
- hbase初识---hbase shell操作以及对应java API开发
- HBase之Java API
- HBase之Java API
- HBase 的Java API
- HBase Java API详解
- hbase java api详解
- HBase Java API详解
- Hbase Java API详解
- HBase之Java API
- Hbase Java API详解
- Hbase for Java API
- HBase Java API详解
- 谷歌电子市场第4天总结
- [Android Studio 插件] AndroidPixelDimenGenerator 自动生成多种分辨率dimen文件
- 虚拟机和主机之间ping不通/开发板和虚拟机之间ping不通
- You need to run build with JDK or have tools.jar on the classpath.
- 2016/10/19
- HBase JAVA API 开发
- yii xss简单攻击
- Dlib人脸关键点检测顺序
- C#控制Excel格式的方法
- mybatis写兼容不同数据库的sql
- 生成二维码方法----C#
- 如何线程安全地遍历List:Vector、CopyOnWriteArrayList
- 基于Mongodb进行分布式数据存储
- 取得子线程的返回值(Future 搭载Callable)