HBase1.0.0 即Hadoop 2.6 采用maven 的方式实现HBase数据简单操作

  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.hbase.HBaseConfiguration;
  9. import org.apache.hadoop.hbase.HColumnDescriptor;
  10. import org.apache.hadoop.hbase.HTableDescriptor;
  11. import org.apache.hadoop.hbase.MasterNotRunningException;
  12. import org.apache.hadoop.hbase.TableName;
  13. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  14. import org.apache.hadoop.hbase.client.Connection;
  15. import org.apache.hadoop.hbase.client.ConnectionFactory;
  16. import org.apache.hadoop.hbase.client.HBaseAdmin;
  17. import org.apache.hadoop.hbase.client.HConnection;
  18. import org.apache.hadoop.hbase.client.HTableInterface;
  19. import org.apache.hadoop.hbase.client.HTablePool;
  20. import org.apache.hadoop.hbase.client.Put;
  21. import org.apache.hadoop.hbase.client.Table;
  22. import org.apache.hadoop.hbase.util.Bytes;
  23.  
  24. /**
  25. * @author 作者 E-mail:
  26. * @version 创建时间:2015年12月28日 下午11:31:31 类说明
  27. */
  28. public class HBaseUtils {
  29. /*
  30. * static { HBaseDaoPool.getInstance(); }
  31. */
  32.  
  33. private static Configuration conf;
  34. private static HBaseAdmin admin;
       private static HTable hTable = null;
  35. static {
  36. conf = new Configuration();
  37. String filePath = "hbase-site.xml";
  38. Path path = new Path(filePath);
  39. conf.addResource(path);
  40. conf = HBaseConfiguration.create(conf);
  41. }
  42.  
  43. /**
  44. * 创建表并判断表是否存在,如果存在则退出
  45. * @param name
  46. * @param cf
  47. * @throws Exception
  48. */
  49. @SuppressWarnings("deprecation")
  50. public static void create_table(String name, String cf) throws Exception {
  51. admin = new HBaseAdmin(conf);
  52. // 先检查表是否存在
  53. if (admin.tableExists(name)) {
  54. System.out.println("table is exit" + name);
  55. System.exit(0);
  56. }
  57. HTableDescriptor tableDesc = new HTableDescriptor(name);
  58. HColumnDescriptor hd = new HColumnDescriptor(cf);
  59. /* hd.setMaxVersions(version); */
  60. tableDesc.addFamily(hd);
  61. admin.createTable(tableDesc);
  62. admin.close();
  63. }
  64.  
  65. /**
  66. * 获得HBase里面所有Table
  67. * @return
  68. * @throws Exception
  69. * @throws ZooKeeperConnectionException
  70. * @throws MasterNotRunningException
  71. */
  72. @SuppressWarnings({ "unused", "deprecation" })
  73. private static List<String> getAllTable() throws MasterNotRunningException,
  74. ZooKeeperConnectionException, Exception {
  75. List<String> table = null;
  76. admin = new HBaseAdmin(conf);
  77. try {
  78. HTableDescriptor[] listTables = admin.listTables();
  79. if (listTables.length > 0) {
  80. table = new ArrayList<String>();
  81. for (HTableDescriptor tableDes : listTables) {
  82. table.add(tableDes.getNameAsString());
  83. System.out.println("table list:" + tableDes.getNameAsString());
  84. }
  85. }
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89.  
  90. return table;
  91. }
  92.  
  93. /**
  94. * 添加一条记录
  95. * @param tableName
  96. * @param rowKey
  97. * @param cloumnFianly
  98. * @param column
  99. * @param values
  100. * @return
  101. */
  102. @SuppressWarnings({ "unused", "deprecation", "resource" })
  103. private static boolean addOneRows(String tableName, String rowKey,
  104. String cloumnFianly, String column, byte[] values) {
  105. HTablePool hTablePool = new HTablePool(conf, 1000);
  106. HTableInterface table = hTablePool.getTable(tableName);
  107. Put put = new Put(rowKey.getBytes());
  108. put.add(cloumnFianly.getBytes(), column.getBytes(), values);
  109. try {
  110. table.put(put);
  111. System.out.println("add success:" + rowKey + "....end");
  112. return true;
  113.  
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. System.out.println("add false :" + rowKey + " error ...end");
  117. return false;
  118. }
  119. }
  120.  
  121. /**
  122. * 根据表名插入一条数据,rowkey 做了简单处理,前面加了7位随机数
  123. * @param hConnection
  124. * @param tableName
  125. */
  126. @SuppressWarnings("deprecation")
  127. public static void insertData(String tableName) {
  128. try {
  129. TableName table = TableName.valueOf(tableName);
  130. System.out.println(table + "table");
  131. Connection connection = ConnectionFactory.createConnection(conf);
  132. Table tb1 = connection.getTable(table);
  133. Random random = new Random();
  134. int sum = random.nextInt(9999999);
  135. String.format("%07d", sum);
  136. String rowKkey = String.format("%07d", sum);
  137. Put put = new Put(Bytes.toBytes(rowKkey));
  138. put.add(Bytes.toBytes("cf1"), Bytes.toBytes("address"), Bytes.toBytes("zz"));
  139. tb1.put(put);
  140. tb1.close();
  141. System.out.println("insert end....");
  142. } catch (IOException e) {
  143.  
  144. e.printStackTrace();
  145. }
  146. }
  1. /**
  2. * 根据rowKey 获得一条记录
  3. * @param tableName
  4. * @param rowKey
  5. */
  6. public void getOneRowsByKey(String tableName, String rowKey){
  7. try {
  8. hTable = new HTable(conf, tableName);
  9. Get get = new Get(Bytes.toBytes(rowKey));
  10. Result result = hTable.get( get );
  11. KeyValue[] rows = result.raw();
  12. for (int i = 0; i < rows.length; i++){
  13. String rowname = new String(rows[i].getQualifier());
  14. String rowValue = new String(rows[i].getValue());
  15. System.out.println("rowname:" + rowname + "--value--" +rowValue);
  16.  
  17. }
  18. }
  19. catch ( IOException e ) {
  20.  
  21. e.printStackTrace();
  22. }
  23. }
  1.  
  1. public static void main(String[] args) throws Exception {
  2.  
  3. // addOneRows("test", "row7", "cf", "g", "value7".getBytes());
  4. // String table = "ps";
  5. String table = "a1";
  6. String cf = "cf";
  7. // create_table(table, cf);
  8. // getAllTable();
  9. insertData(table);
  10. System.out.println("success");
  11. }
  12. }

maven 配置:

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-common</artifactId>
  4. <version>2.6.0</version>
  5. </dependency>
  6.  
  7. <!-- hbase -->
  8. <dependency>
  9. <groupId>org.apache.hbase</groupId>
  10. <artifactId>hbase-client</artifactId>
  11. <version>1.0.0</version>
  12. </dependency>
  13.  
  14. <dependency>
  15. <groupId>org.apache.hbase</groupId>
  16. <artifactId>hbase-server</artifactId>
  17. <version>1.0.0</version>
  18. </dependency>
  1. hbase-site.xml 配置
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <!--Autogenerated by Cloudera Manager-->
  4. <configuration>
  5. <property>
  6. <name>hbase.rootdir</name>
  7. <value>hdfs://HMaster/hbase</value>
  8. </property>
  9. <property>
  10. <name>hbase.client.write.buffer</name>
  11. <value>2097152</value>
  12. </property>
  13. <property>
  14. <name>hbase.client.pause</name>
  15. <value>100</value>
  16. </property>
  17. <property>
  18. <name>hbase.client.retries.number</name>
  19. <value>35</value>
  20. </property>
  21. <property>
  22. <name>hbase.client.scanner.caching</name>
  23. <value>100</value>
  24. </property>
  25. <property>
  26. <name>hbase.client.keyvalue.maxsize</name>
  27. <value>10485760</value>
  28. </property>
  29. <property>
  30. <name>hbase.regionserver.thrift.http</name>
  31. <value>false</value>
  32. </property>
  33. <property>
  34. <name>hbase.thrift.support.proxyuser</name>
  35. <value>false</value>
  36. </property>
  37. <property>
  38. <name>hbase.rpc.timeout</name>
  39. <value>60000</value>
  40. </property>
  41. <property>
  42. <name>hbase.snapshot.enabled</name>
  43. <value>true</value>
  44. </property>
  45. <property>
  46. <name>hbase.snapshot.master.timeoutMillis</name>
  47. <value>60000</value>
  48. </property>
  49. <property>
  50. <name>hbase.snapshot.region.timeout</name>
  51. <value>60000</value>
  52. </property>
  53. <property>
  54. <name>hbase.snapshot.master.timeout.millis</name>
  55. <value>60000</value>
  56. </property>
  57. <property>
  58. <name>hbase.security.authentication</name>
  59. <value>simple</value>
  60. </property>
  61. <property>
  62. <name>zookeeper.session.timeout</name>
  63. <value>60000</value>
  64. </property>
  65. <property>
  66. <name>zookeeper.znode.parent</name>
  67. <value>/hbase</value>
  68. </property>
  69. <property>
  70. <name>zookeeper.znode.rootserver</name>
  71. <value>root-region-server</value>
  72. </property>
  73. <property>
  74. <name>hbase.zookeeper.quorum</name>
  75. <value>node5,node2,node3,node4,node1</value>
  76. </property>
  77. <property>
  78. <name>hbase.zookeeper.property.clientPort</name>
  79. <value>2181</value>
  80. </property>
  81. </configuration>

HBase1.0.0 实现数据增删查的更多相关文章

  1. SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...

  2. 学习记录——使用PHP实现数据增删查改等基本功能(前后端分离)

    萌新初次学习服务器端语言,分享学习经验 实现功能:1.显示数据表    2.对数据进行分页    3.对数据进行增删查改 由于本萌新采用前后端完全分离方案,所以数据传输用的ajax,为了提高代码的复用 ...

  3. mysql数据增删查授权

    一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...

  4. Django 之restfromwork 序列化组件实现数据增删查改

    rest-framework序列化之Serializer models.py from django.db import models # Create your models here. class ...

  5. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  6. 利用dbutils工具实现数据的增删查改操作(dbutis入门)

    一.前期准备 1.安装数据库(如:mysql5.5) 2.安装Eclipse(如:3.4) 3.下载数据库驱动包 4.下载dbutis工具包 5.在Eclipse创建名为 dbutils 的工程并在工 ...

  7. Ubuntu 14.10 下ZooKeeper+Hadoop2.6.0+HBase1.0.0 的HA机群高可用配置

    1 硬件环境 Ubuntu 14.10 64位 2 软件环境 openjdk-7-jdk hadoop 2.6.0 zookeeper-3.4.6 hbase-1.0.0 3 机群规划 3.1 zoo ...

  8. java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)

    1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件  准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...

  9. PHP数据访问增删查(20161028)

    注:预定义数组   $_POST[ ]; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

随机推荐

  1. msf generate exec payload

    daniel@daniel-mint ~/msf/metasploit-framework $ ruby msfpayload windows/exec CMD=calc.exe N WARNING: ...

  2. LightOJ 1248 Dice (III) (期望DP / 几何分布)

    题目链接:LightOJ - 1248 Description Given a dice with n sides, you have to find the expected number of t ...

  3. (转)Adaboost

    基本原理 Adaboost算法基本原理就是将多个弱分类器(弱分类器一般选用单层决策树)进行合理的结合,使其成为一个强分类器. Adaboost采用迭代的思想,每次迭代只训练一个弱分类器,训练好的弱分类 ...

  4. Haproxy+ssl+nvm+forever

    1 nvm介绍 NVM(Node version manager)顾名思义,就是Node.js的版本管理软件,可以轻松的在Node.js各个版本间切换,项目源码在GitHub: #安装git客户端 [ ...

  5. jwt认证登录

    配置文件:#服务配置 server: port: 9002 #spring配置 spring: #应用配置 application: name: ynhrm-system #指定服务名 #数据库连接池 ...

  6. spark textFile读取多个文件

    1.spark textFile读取File 1.1 简单读取文件 val spark = SparkSession.builder() .appName("demo") .mas ...

  7. jumpserver3.0安装

    由于来源身份不明.越权操作.密码泄露.数据被窃.违规操作等因素都可能会使运营的业务系统面临严重威胁,一旦发生事故,如果不能快速定位事故原因,运维人员往往就会背黑锅.几种常见的运维人员背黑锅场景:1)由 ...

  8. SpringMVC学习(6):数据验证

    在系列(4).(5)中我们展示了如何绑定数据,绑定完数据之后如何确保我们得到的数据的正确性?这就是我们本篇要说的内容 -> 数据验证. 这里我们采用Hibernate-validator来进行验 ...

  9. jsp struts2导入excel并且存储到数据库中

    开发中遇到一个问题: 需要从外部导入excel,拿到其中的数据然后保存到数据库中. 1.先在jsp端使用input进行上传: <form action="storeOBDexcel&q ...

  10. nodejs 进阶:图片缩小

    demo 效果: 代码: /** * Created by ZXW on 2017/10/30. */ var fs = require('fs'); var gm = require('gm'); ...