`
qindongliang1922
  • 浏览: 2211115 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:118396
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:126724
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:60797
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:71955
社区版块
存档分类
最新评论

如何使用Java API操作Hbase(基于0.96新的api)

阅读更多
写了个Hbase新的api的增删改查的工具类,以供参考,直接拷贝代码就能用,散仙觉得基础的功能,都有了,代码如下:
package com.dhgate.hbase.test;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.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.filter.PageFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 基于新的API
 * Hbase0.96版本
 * 写的工具类
 * 
 * @author qindongliang
 * 大数据技术交流群: 376932160
 * 
 * **/
public class HbaseCommons {

	
	static Configuration conf=HBaseConfiguration.create();
	static String tableName="";
 
	
	
	
	public static void main(String[] args)throws Exception {
		
		//String tableName="test";
		//createTable(tableName, null);
		
	}
	
	
	/**
	 * 批量添加数据
	 * @param tableName 标名字
	 * @param rows rowkey行健的集合
	 * 本方法仅作示例,其他的内容需要看自己义务改变
	 * 
	 * **/
	public static void insertList(String tableName,String rows[])throws Exception{
		HTable table=new HTable(conf, tableName);
		List<Put> list=new ArrayList<Put>();
		for(String r:rows){
			Put p=new Put(Bytes.toBytes(r));
		   //此处示例添加其他信息
			//p.add(Bytes.toBytes("family"),Bytes.toBytes("column"), 1000, Bytes.toBytes("value"));
			list.add(p);
		}
		table.put(list);//批量添加
		table.close();//释放资源
	}
	
	/**
	 * 创建一个表
	 * @param tableName 表名字
	 * @param columnFamilys 列簇
	 * 
	 * **/
	public static void createTable(String tableName,String[] columnFamilys)throws Exception{
		//admin 对象
		HBaseAdmin admin=new HBaseAdmin(conf);
		if(admin.tableExists(tableName)){
			System.out.println("此表,已存在!");
		}else{
			//旧的写法
			//HTableDescriptor tableDesc=new HTableDescriptor(tableName);
			//新的api
			HTableDescriptor tableDesc=new HTableDescriptor(TableName.valueOf(tableName));
			
			for(String columnFamily:columnFamilys){
				tableDesc.addFamily(new HColumnDescriptor(columnFamily));
			}
			
			admin.createTable(tableDesc);
			System.out.println("建表成功!");
			
		}
		admin.close();//关闭释放资源
		
	}
	
	/**
	 * 删除一个表
	 * @param tableName 删除的表名
	 * */
	public static void deleteTable(String tableName)throws Exception{
		HBaseAdmin admin=new HBaseAdmin(conf);
		if(admin.tableExists(tableName)){
			admin.disableTable(tableName);//禁用表
			admin.deleteTable(tableName);//删除表
			System.out.println("删除表成功!");
		}else{
			System.out.println("删除的表不存在!");
		}
		admin.close();
	}
	
	/**
	 * 插入一条数据
	 * @param tableName 表明
	 * @param columnFamily 列簇
	 * @param column      列
	 * @param value       值
	 * 
	 * ***/
	public static void insertOneRow(String tableName,String rowkey,String columnFamily,String column,String value)throws Exception{
		
		HTable table=new HTable(conf, tableName);
		Put put=new Put(Bytes.toBytes(rowkey));
		put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
		table.put(put);//放入表
		table.close();//释放资源
		
	}
	
	/**
	 * 删除一条数据
	 * @param tableName 表名
	 * @param row  rowkey行键
	 * 
	 * */
	public static void deleteOneRow(String tableName,String row)throws Exception{
		
		HTable table=new HTable(conf, tableName);
		Delete delete=new Delete(Bytes.toBytes(row));
		table.delete(delete);
		table.close();
	}
	
	
	/**
	 * 删除多条数据
	 * @param tableName 表名
	 * @param rows 行健集合
	 * 
	 * **/
	public static void deleteList(String tableName,String rows[])throws Exception{
		HTable table=new HTable(conf, tableName);
		List<Delete> list=new ArrayList<Delete>();
		for(String row:rows){
			Delete del=new Delete(Bytes.toBytes(row));
			list.add(del);
		}
		table.delete(list);
		table.close();//释放资源
		
	}
	
	
	/**
	 * 获取一条数据,根据rowkey
	 * @param  tableName 表名
	 * @param  row 行健
	 * 
	 * **/
	public static void getOneRow(String tableName,String row)throws Exception{
		HTable table=new HTable(conf, tableName);
		Get get=new Get(Bytes.toBytes(row));
		Result result=table.get(get);
		printRecoder(result);//打印记录
		table.close();//释放资源
	}
	
	/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * */
	public static void showAll(String tableName)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}
	
	
	/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * @param rowKey  行健
	 * */
	public static void ScanPrefixByRowKey(String tableName,String rowKey)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}
	
	
	/**
	 * 查看某个表下的所有数据
	 * 
	 * @param tableName 表名
	 * @param rowKey 行健扫描
	 * @param limit  限制返回数据量
	 * */
	public static void ScanPrefixByRowKeyAndLimit(String tableName,String rowKey,long limit)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setFilter(new PrefixFilter(Bytes.toBytes(rowKey)));
		scan.setFilter(new  PageFilter(limit));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);//打印记录
		}
		table.close();//释放资源
	}
	
	
	/**
	 * 根据rowkey扫描一段范围
	 * @param tableName 表名
	 * @param startRow 开始的行健
	 * @param stopRow  结束的行健
	 * **/
	public  void scanByStartAndStopRow(String tableName,String startRow,String stopRow)throws Exception{
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		scan.setStartRow(Bytes.toBytes(startRow));
		scan.setStopRow(Bytes.toBytes(stopRow));
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			printRecoder(r);
		}
		table.close();//释放资源
		
		
	}
	/**
	 * 扫描整个表里面具体的某个字段的值
	 * @param tableName  表名
	 * @param columnFalimy 列簇
	 * @param column 列
	 * **/
	public static  void getValueDetail(String tableName,String columnFalimy,String column)throws Exception{
	
		HTable table=new HTable(conf, tableName);
		Scan scan=new Scan();
		ResultScanner rs=table.getScanner(scan);
		for(Result r:rs){
			System.out.println("值: " +new String(r.getValue(Bytes.toBytes(columnFalimy), Bytes.toBytes(column))));
		}
		table.close();//释放资源
		
	}
	
	
	
	
	
	
	
	
	/**
	 * 打印一条记录的详情
	 * 
	 * */
	public  static void printRecoder(Result result)throws Exception{
		for(Cell cell:result.rawCells()){
			System.out.print("行健: "+new String(CellUtil.cloneRow(cell)));
			System.out.print("列簇: "+new String(CellUtil.cloneFamily(cell)));
			System.out.print(" 列: "+new String(CellUtil.cloneQualifier(cell)));
			System.out.print(" 值: "+new String(CellUtil.cloneValue(cell)));
			System.out.println("时间戳: "+cell.getTimestamp());	
		}
	}
	
	
	
}


3
0
分享到:
评论

相关推荐

    hbase 的java代码 集合 hbase 0.96

    总结来说,掌握HBase 0.96版本的Java编程涉及理解HBase的核心概念,如表、列族、行键、时间戳,以及如何通过Java API进行连接、操作数据、管理表和处理故障。同时,了解异步操作、批量操作和监听器机制也是提升HBase...

    hbase-0.96 jar包

    使用这个jar包,开发者可以构建基于HBase的应用程序,利用Java API进行数据操作。在开发过程中,需要注意的是HBase的API设计是面向列的,与传统的面向行的数据库API有所不同,需要理解如何定义表结构,如何进行增删...

    java连接hbase的jar包

    2. **建立连接**: 使用配置创建`HConnection`对象,但请注意,从HBase 0.96版本开始,推荐使用`Connection`代替。 ```java Connection connection = ConnectionFactory.createConnection(config); ``` 3. **操作表...

    Hbase 官方中文文档

    HBase官方中文文档概述了Apache HBase TM的基本概念、配置方法、升级策略、shell使用、数据模型、架构设计、安全机制、API接口、性能调优以及故障排除等多方面的知识。HBase是一个开源的非关系型分布式数据库(NoSQL...

    HBase 官方文档

    - **REST**:说明 HBase 的 REST API 接口及其使用方法。 - **Thrift**:解释 HBase 的 Thrift 接口,并给出示例。 - **C/C++ Apache HBase Client**:提供 C/C++ 开发者使用的 HBase 客户端接口。 #### 十二、...

    [APACHE]Apache Crunch - Getting Started.pdf

    Crunch项目的API支持包括对HBase 0.96.x的支持,使得Crunch能够在HBase上执行数据处理任务。此外,Crunch项目还提供了Source Code、Mailing Lists、Issue Tracking、Wiki等资源,便于开发者下载、参与讨论、报告问题...

Global site tag (gtag.js) - Google Analytics