• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > 1006-HBase操作实战(JAVA API模式),1006-hbaseapi

1006-HBase操作实战(JAVA API模式),1006-hbaseapi

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含java开发实战经典,java开发实战1200例,java并发编程实战,java项目实战,java实战培训等服务器相关知识,网友希望可以进行参考

1006-HBase操作实战(JAVA API模式),1006-hbaseapi


一、准备阶段 开发环境: hadoop: hadoop -2.4.0 hbase: hbase -0.94.11-security eclipse:Juno Service Release 2
二、创建 hbasedemo项目
1、通过 Eclipse 创建一个新 Java 工程 2、右击项目根目录,选择“Propertiesà> Java Build Pathà> Libraryà>  Add  External  JARs” 3、添加jar文件到 classpath 3.1 解压 HBase 安装文件 3.2 拷贝 hbase-0.94.7-security.jar、 hbase-0.94.11-security-tests.jar到工程 3.3 lib 子目录下所有的 jar 包添加到本工程的 Build Path下
三、使用HBase JAVA API
初始化和释放资源方法:
public void init() 数据初始化
public void destory()  释放资源

验证方法:
public void testCreate() 验证创建表
public void testDrop() 验证删除表
public void testPut()验证添加表数据
public void testGetByRowKey() 验证如何通过rowkey获取数据
public void testScanSToEnd() 验证范围行数据获取
public void testScanAll()  验证全表扫描

方法:
public List<Cell> get(String tableName, String rowKey)  通过rowKey插入表数据
public List<Result> scan(String tableName)  通过表名查询所有信息
public List<Result> scan(String tableName,String startRow,String stopRow) 查询范围行方法
public void create(String tableName, String[] cfs)  通过表名和列族信息创建表
public void drop(String tableName) 删除表
public void put(String tableName, List<CellBean> values) 根据提供的表和对象信息插入hbase

第一步: 封装HBase中的存储单元Cell对象

/**
 * 封装HBase中存储单元cell对象
 * @author shenfl
 * @version:V1.0
 * @Date:2015-6-8
 */
public class CellBean {

	//行健
	private String rowKey;
	//列族
	private String columnFamilly;
	//列名
	private String columnName;
	//cell值
	private String columnValue;

	public String getRowKey() {
		return rowKey;
	}

	public void setRowKey(String rowKey) {
		this.rowKey = rowKey;
	}

	public String getColumnFamilly() {
		return columnFamilly;
	}

	public void setColumnFamilly(String columnFamilly) {
		this.columnFamilly = columnFamilly;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getColumnValue() {
		return columnValue;
	}

	public void setColumnValue(String columnValue) {
		this.columnValue = columnValue;
	}
}

第二步: 使用HBase JAVA API 操作HBase  数据库
/**
 * 使用HBase JAVA API操作
 *
 * @author shenfl
 *
 */
public class HBaseTest {
    Configuration config = null ;
    // 创建操作表對象
    HBaseAdmin admin = null;
    // hbase的连接
    HConnection conn = null;
    @Before
    public void init() {
          config = HBaseConfiguration. create();
          // HBase只需要知道ZooKeeper,就可以操作RegionServer上的數據,设置HBase 连接ZooKeeper
          config.set( "hbase.zookeeper.quorum" , "192.168.2.35:2181,192.168.2.36:2181,192.168.2.37:2181" );
          try {
              conn = HConnectionManager.createConnection( config);
              admin = new HBaseAdmin(config );
         } catch (Exception e ) {
              e.printStackTrace();
         }
    }
    @After
    public void destory() {
          try {
              admin.close();
         } catch (IOException e ) {
              e.printStackTrace();
         }
    }
    @Test
    public void testCreate() {
         String tableName = "account" ;
         String[] columnFamilies = new String[] { "info" , "other" };
         create( tableName , columnFamilies );
    }
    //@Test
    public void testDrop() {
         drop( "account" );
    }
    @Test
    public void testPut() {
          // 设置表对象列表
         List<CellBean> cbList = new ArrayList<CellBean>();
          // 设置表名account
         String tableName = "account" ;
         CellBean e = null ;
          for (int i =0;i <3;i ++){
              e = new CellBean();
              e.setRowKey( "g20142500" +i );
              e.setColumnFamilly( "info" );
              e.setColumnName( "username" );
              e.setColumnValue( "shenfl" +i );
              cbList.add( e);
              e = new CellBean();
              e.setRowKey( "g20142500" +i );
              e.setColumnFamilly( "other" );
              e.setColumnName( "career" );
              e.setColumnValue( "singer" +i );
              cbList.add( e);
         }
         put( tableName , cbList );
    }
    @Test
    public void testGetByRowKey() {
         
         String tableName = "account" ;
         String rowKey = "g201425001" ;
         List<Cell> cells = get( tableName , rowKey );
         StringBuffer info = new StringBuffer();
          for (Cell c : cells ) {
              //使用CellUtil方法输出对应列, hbase0.96 版本使用CellUtil函数
              info.append( new String(CellUtil.cloneFamily(c))).append( ":" )
                 .append( new String(CellUtil.cloneQualifier(c))).append( "\t" )
                 .append( new String(CellUtil.cloneValue(c))).append( "\n" );
         }
         System. out .println(info );
    }
    @Test
    public void testScanSToEnd(){
         
         StringBuffer sb = new StringBuffer();
         String tableName = "account" ;
         String startRow = "g201425000" ;
         String stopRow = "g201425002" ;
         List<Result> rsList = scan( tableName , startRow , stopRow );
          byte [] rowKey = null;
          byte [] username = null;
          byte [] career = null;
          for (Result rs :rsList ){
              rowKey = rs.getRow();
              username = rs .getValue(Bytes.toBytes( "info" ), Bytes.toBytes("username"));
              career = rs .getValue(Bytes.toBytes( "other" ), Bytes.toBytes("career"));
              sb.append( new String(rowKey )).append("\t" )
               .append( new String(username )).append("\t" )
               .append( new String(career )).append("\n" );
         }
         System. out .println(sb .toString());
    }
    
    @Test
    public void testScanAll() {
         StringBuffer sb = new StringBuffer();
         List<Result> rsList = scan( "account" );
          for (Result rs : rsList ) {
             List<Cell> listCells = rs .listCells();
              for (Cell c : listCells ) {
                  // 使用CellUtil方法输出对应列, hbase0.96 版本使用CellUtil函数
                  sb.append( new String(CellUtil.cloneRow(c))).append( "\t" )
                   .append( new String(CellUtil.cloneFamily(c))).append( ":" )
                   .append( new String(CellUtil.cloneQualifier(c))).append( "\t" )
                   .append( new String(CellUtil.cloneValue(c))).append( "\n" );
             }
         }
         System. out .println(sb );
    }
    /**
     * 通过rowKey插入表数据
     * @param tableName 表名
     * @param rowKey 行健
     * @return
     */
    public List<Cell> get(String tableName , Stri
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 1006-HBase操作实战(JAVA API模式),1006-hbaseapi

相关文章

  • Spark编程指南V1.4.0(翻译),编程指南v1.4.0
  • Spark 批量写数据入HBase,spark数据入hbase
  • Hive作为Mondrian的数据源,hivemondrian
  • 机器学习算法-Adaboost,学习算法-adaboost
  • AWS EC2 调整云主机根卷大小,awsec2
  • 初试 Coding.net 在线IDE——WebIDE,coding.netwebide
  • 玩转OpenStack网络Neutron(3)--配置多种不同类型网络,openstackneutron
  • hadoop2.7完全分布式安装,hadoop2.7
  • Hadoop文件解压缩,
  • ElasticSearch的分布式安装

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • Openstack中为虚拟机使用CDROM光驱设备,openstackcdrom
    • Name node is in safe mode.错误处理方式 hadoop,nodehadoop
    • &lt;转&gt;Openstack ceilometer 宿主机监控模块扩展,openstackceilometer
    • 二分Kmeans的java实现,二分kmeansjava
    • MongoDB Query 的几个方法,mongodbquery
    • openstack报错openstack-config:command not find,find
    • Storm简述及集群安装,Storm简述集群安装
    • HDFS数据完整性,hdfs完整性
    • spark streaming kafka1.4.1中的低阶api createDirectStream使用总结,directstream
    • 《转》ceilometer的数据采集机制入门,《转》ceilometer

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有