HBase1.2.4新版api操作示例
来源:互联网 发布:oracle数据库接口 编辑:程序博客网 时间:2024/06/07 03:30
用java操作hbase,hbase1.2.4新版对api做了很多调整,当然原来的api也能用,只是会提示你is deprecated,对象上还会被划一道横线,看着不爽,旧api早晚会淘汰,所以有必要使用新api:
1.代码如下
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;import java.util.ArrayList;import java.util.List;/** * Created by hasee on 2017/3/3. */public class HBaseDao { public static Configuration conf; public static Connection connection; static{ Configuration HBASE_CONFIG=new Configuration(); HBASE_CONFIG.set("hbase.zookeeper.quorum","192.168.73.1,192.168.73.2,192.168.73.3"); HBASE_CONFIG.set("hbase.zookeeper.property.clientPort","2181"); conf= HBaseConfiguration.create(HBASE_CONFIG); try { connection=ConnectionFactory.createConnection(conf); } catch (IOException e) { e.printStackTrace(); } } /** * 创建表 * @param tableName * @param familys * @throws IOException */ public static void createTable(String tableName,String[] familys) throws IOException { //HBaseAdmin admin=new HBaseAdmin(conf); // Connection connection= ConnectionFactory.createConnection(conf); Admin admin=connection.getAdmin(); if(admin.tableExists(TableName.valueOf(tableName))){ System.out.println("table already exists!"); }else { HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName)); for(int i=0;i<familys.length;i++){ tableDescriptor.addFamily(new HColumnDescriptor(familys[i])); } admin.createTable(tableDescriptor); System.out.println("create table "+tableName+" ok."); } } /** * 删除表 * @param tableName * @throws IOException */ public static void deleteTable(String tableName) throws Exception { try { Admin admin=connection.getAdmin(); admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); System.out.println("delete table "+tableName+" ok."); } catch (MasterNotRunningException e) { e.printStackTrace(); }catch (ZooKeeperConnectionException e){ e.printStackTrace(); } } /** * 插入数据 * @param tableName * @param rowKey * @param family * @param qualifier * @param value * @throws Exception */ public static void addRecord(String tableName,String rowKey,String family,String qualifier,String value) throws Exception { try { Table table=connection.getTable(TableName.valueOf(tableName)); Put put=new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value)); table.put(put); System.out.println("insert record "+rowKey+" to table "+tableName+" ok."); } catch (IOException e) { e.printStackTrace(); } } /** * 获取数据 * @param tableName * @param rowKey * @throws Exception */ public static void getOneRecord(String tableName,String rowKey) throws IOException { Table table=connection.getTable(TableName.valueOf(tableName)); Get get=new Get(rowKey.getBytes()); Result rs=table.get(get); for(Cell cell:rs.rawCells()){ System.out.println(new String(cell.getRowArray())+" "); System.out.println(new String(cell.getFamilyArray())+":"); System.out.println(new String(cell.getQualifierArray())+" "); System.out.println(cell.getTimestamp()+" "); System.out.println(new String(cell.getValueArray())+" "); } } /** * 浏览每一行 * @param tableName */ public static void getAllRecord(String tableName) { try { Table table=connection.getTable(TableName.valueOf(tableName)); Scan scan=new Scan(); ResultScanner rs=table.getScanner(scan); for (Result r:rs) { for (Cell cell : r.rawCells()) { System.out.println(new String(cell.getRowArray()) + " "); System.out.println(new String(cell.getFamilyArray()) + ":"); System.out.println(new String(cell.getQualifierArray()) + " "); System.out.println(cell.getTimestamp() + " "); System.out.println(new String(cell.getValueArray()) + " "); } } } catch (IOException e) { e.printStackTrace(); } } public static void deleteRecord(String tableName,String rowKey) throws IOException { Table table=connection.getTable(TableName.valueOf(tableName)); List list=new ArrayList(); Delete delete=new Delete(rowKey.getBytes()); list.add(delete); table.delete(list); System.out.println("delete record "+rowKey+" ok."); } public static void main(String[] args) throws Exception { HBaseDao hBaseDao=new HBaseDao(); //hBaseDao.createTable("hello",new String[]{"ai","bi"}); //hBaseDao.addRecord("hello","row2","bi","521","yangqing"); //hBaseDao.getOneRecord("hello","row2"); //hBaseDao.getAllRecord("hello"); hBaseDao.deleteRecord("hello","row1"); }}
2.另外我是在idea中的maven项目中开发的,下面贴上java操作hbase的maven依赖:
<dependencies> <!--Hadoop main artifact--> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.4</version> </dependency> <!--Unit test artifacts--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-library</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-core</artifactId> <version>1.8</version> </dependency></dependencies>
0 0
- HBase1.2.4新版api操作示例
- HBase1.2.4新版本API 操作实例
- 新版Hbase API 用法示例
- Hbase1.0 客户端api
- Hbase1.0 客户端api
- 新版百度地图API基本操作
- 基于scala 新版API操作HBase
- HBase1.0+ java-api 介绍
- java对hbase的基本操作,新版API实验
- HBase1.0以上版本的API改变
- Hbase1.0 客户端api---神算子
- Alluxio文件操作Java API示例
- HBase1.0.1基本操作(java代码)
- 新版nessus 6.10.7 API 接口,可实现自动扫描,自定义入库操作
- hbase1.X的部分api改变(慢慢更新)
- 新版博客操作说明
- java7新版文件操作
- 新版API多文件输入
- leetcode 215. Kth Largest Element in an Array
- 7Liveasia Casino 200% Slots Welcome Bonus
- 内部类、访问权限课堂笔记
- HashMap 多线程处理之 Fail-Fast机制
- d3js的初接触
- HBase1.2.4新版api操作示例
- 应聘技巧(所有行业都可以使用的技巧。个人看法)
- 差分约束(个人理解)
- JS命名空间
- ADS安装到100%不动后,无法打开ADS
- JavaScript 运动框架 Step by step
- Redis工具类2
- Android Matrix图像变换处理
- 互联网协议入门(二)