HBase1.0.0 实现数据增删查
HBase1.0.0 即Hadoop 2.6 采用maven 的方式实现HBase数据简单操作
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HConnection;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes; /**
* @author 作者 E-mail:
* @version 创建时间:2015年12月28日 下午11:31:31 类说明
*/
public class HBaseUtils {
/*
* static { HBaseDaoPool.getInstance(); }
*/ private static Configuration conf;
private static HBaseAdmin admin;
private static HTable hTable = null;
static {
conf = new Configuration();
String filePath = "hbase-site.xml";
Path path = new Path(filePath);
conf.addResource(path);
conf = HBaseConfiguration.create(conf);
} /**
* 创建表并判断表是否存在,如果存在则退出
* @param name
* @param cf
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static void create_table(String name, String cf) throws Exception {
admin = new HBaseAdmin(conf);
// 先检查表是否存在
if (admin.tableExists(name)) {
System.out.println("table is exit" + name);
System.exit(0);
}
HTableDescriptor tableDesc = new HTableDescriptor(name);
HColumnDescriptor hd = new HColumnDescriptor(cf);
/* hd.setMaxVersions(version); */
tableDesc.addFamily(hd);
admin.createTable(tableDesc);
admin.close();
} /**
* 获得HBase里面所有Table
* @return
* @throws Exception
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
@SuppressWarnings({ "unused", "deprecation" })
private static List<String> getAllTable() throws MasterNotRunningException,
ZooKeeperConnectionException, Exception {
List<String> table = null;
admin = new HBaseAdmin(conf);
try {
HTableDescriptor[] listTables = admin.listTables();
if (listTables.length > 0) {
table = new ArrayList<String>();
for (HTableDescriptor tableDes : listTables) {
table.add(tableDes.getNameAsString());
System.out.println("table list:" + tableDes.getNameAsString());
}
}
} catch (Exception e) {
e.printStackTrace();
} return table;
} /**
* 添加一条记录
* @param tableName
* @param rowKey
* @param cloumnFianly
* @param column
* @param values
* @return
*/
@SuppressWarnings({ "unused", "deprecation", "resource" })
private static boolean addOneRows(String tableName, String rowKey,
String cloumnFianly, String column, byte[] values) {
HTablePool hTablePool = new HTablePool(conf, 1000);
HTableInterface table = hTablePool.getTable(tableName);
Put put = new Put(rowKey.getBytes());
put.add(cloumnFianly.getBytes(), column.getBytes(), values);
try {
table.put(put);
System.out.println("add success:" + rowKey + "....end");
return true; } catch (IOException e) {
e.printStackTrace();
System.out.println("add false :" + rowKey + " error ...end");
return false;
}
} /**
* 根据表名插入一条数据,rowkey 做了简单处理,前面加了7位随机数
* @param hConnection
* @param tableName
*/
@SuppressWarnings("deprecation")
public static void insertData(String tableName) {
try {
TableName table = TableName.valueOf(tableName);
System.out.println(table + "table");
Connection connection = ConnectionFactory.createConnection(conf);
Table tb1 = connection.getTable(table);
Random random = new Random();
int sum = random.nextInt(9999999);
String.format("%07d", sum);
String rowKkey = String.format("%07d", sum);
Put put = new Put(Bytes.toBytes(rowKkey));
put.add(Bytes.toBytes("cf1"), Bytes.toBytes("address"), Bytes.toBytes("zz"));
tb1.put(put);
tb1.close();
System.out.println("insert end....");
} catch (IOException e) { e.printStackTrace();
}
}
/**
* 根据rowKey 获得一条记录
* @param tableName
* @param rowKey
*/
public void getOneRowsByKey(String tableName, String rowKey){
try {
hTable = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = hTable.get( get );
KeyValue[] rows = result.raw();
for (int i = 0; i < rows.length; i++){
String rowname = new String(rows[i].getQualifier());
String rowValue = new String(rows[i].getValue());
System.out.println("rowname:" + rowname + "--value--" +rowValue); }
}
catch ( IOException e ) { e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// addOneRows("test", "row7", "cf", "g", "value7".getBytes());
// String table = "ps";
String table = "a1";
String cf = "cf";
// create_table(table, cf);
// getAllTable();
insertData(table);
System.out.println("success");
}
}
maven 配置:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency> <!-- hbase -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.0.0</version>
</dependency> <dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.0.0</version>
</dependency>
hbase-site.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <!--Autogenerated by Cloudera Manager-->
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://HMaster/hbase</value>
</property>
<property>
<name>hbase.client.write.buffer</name>
<value>2097152</value>
</property>
<property>
<name>hbase.client.pause</name>
<value>100</value>
</property>
<property>
<name>hbase.client.retries.number</name>
<value>35</value>
</property>
<property>
<name>hbase.client.scanner.caching</name>
<value>100</value>
</property>
<property>
<name>hbase.client.keyvalue.maxsize</name>
<value>10485760</value>
</property>
<property>
<name>hbase.regionserver.thrift.http</name>
<value>false</value>
</property>
<property>
<name>hbase.thrift.support.proxyuser</name>
<value>false</value>
</property>
<property>
<name>hbase.rpc.timeout</name>
<value>60000</value>
</property>
<property>
<name>hbase.snapshot.enabled</name>
<value>true</value>
</property>
<property>
<name>hbase.snapshot.master.timeoutMillis</name>
<value>60000</value>
</property>
<property>
<name>hbase.snapshot.region.timeout</name>
<value>60000</value>
</property>
<property>
<name>hbase.snapshot.master.timeout.millis</name>
<value>60000</value>
</property>
<property>
<name>hbase.security.authentication</name>
<value>simple</value>
</property>
<property>
<name>zookeeper.session.timeout</name>
<value>60000</value>
</property>
<property>
<name>zookeeper.znode.parent</name>
<value>/hbase</value>
</property>
<property>
<name>zookeeper.znode.rootserver</name>
<value>root-region-server</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>node5,node2,node3,node4,node1</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>
HBase1.0.0 实现数据增删查的更多相关文章
- SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)
SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...
- 学习记录——使用PHP实现数据增删查改等基本功能(前后端分离)
萌新初次学习服务器端语言,分享学习经验 实现功能:1.显示数据表 2.对数据进行分页 3.对数据进行增删查改 由于本萌新采用前后端完全分离方案,所以数据传输用的ajax,为了提高代码的复用 ...
- mysql数据增删查授权
一 介绍 MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的 ...
- Django 之restfromwork 序列化组件实现数据增删查改
rest-framework序列化之Serializer models.py from django.db import models # Create your models here. class ...
- 3.EF 6.0 Code-First实现增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...
- 利用dbutils工具实现数据的增删查改操作(dbutis入门)
一.前期准备 1.安装数据库(如:mysql5.5) 2.安装Eclipse(如:3.4) 3.下载数据库驱动包 4.下载dbutis工具包 5.在Eclipse创建名为 dbutils 的工程并在工 ...
- 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 ...
- java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)
1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件 准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...
- PHP数据访问增删查(20161028)
注:预定义数组 $_POST[ ]; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
随机推荐
- 文件上传&&验证文件格式
$(function(){ $(".layui-progress").hide(); $("[data-upload-file]").each(function ...
- js 中常用的设计模式
常用的设计模式: 工厂方法模式.单例模式.适配器模式.组合模式.迭代子模式 (23种设计模式) 总体来说设计模式分为三大类: ①创建型模式 共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原 ...
- 内核module读取进程页目录
根据当前CR3寄存器内容,读取对应物理内存中的页目录页,并进行解析 1: void dumpPageDirectoryEntry(u32 entry) 2: { 3: u8 present; 4: u ...
- cesium左侧列表定位目标
cesium左侧列表定位目标 功能:根据左侧列表经纬度等信息的值,进行搜索定位. 列表: 1 点击清除按钮可以清空所有input的值 2 点击查找可以定位到位置,如果输入的值不在范围内,会有弹出框 ...
- lambda(),map(),filter()
Lambda 函数 Lambda 函数是一种比较小的匿名函数.Python 函数通常使用 def a_function_name() 样式来定义,但对于 lambda 函数,我们根本没为它命名.这是因 ...
- Python值正则表达式(RE)
要想在Python中使用正则表达式,首先要引入模块: import re . 匹配任意一个 + 匹配至少一个 * 匹配0个至多个 ? 1个或0个(可有可无) - 表范围 \ 转义 ^ 在首 $ ...
- [机器学习][face recognition] 一个视频人脸识别实现
开发环境和用到的库: Ubuntu jupyter notebook(python3.6) OpenCV Dlib face_recognition 实现效果如下(视频截图): #coding=utf ...
- Apache Shiro反序列化远程代码执行
一.漏洞利用 wget https://raw.githubusercontent.com/sv3nbeast/ShiroScan/master/moule/ysoserial.jar 反弹shell ...
- suffixes - 列出文件后缀。
DESCRIPTION [描述] 文件后缀与文件名之间以点(.)间隔,通常包括一个或多个字母. 我们用文件后缀来描述文件的内容.很多标准的实用程序,如编译器,以后缀来识别文件类型. make(1) 就 ...
- teb教程6
代价地图的转换 简介:本部分关于怎样把代价地图转换插件应用到转换占据栅格costmap2d到几何形状来优化(测试阶段) teb_local_planner包支持costmap_converter插件, ...