
Hadoop_Java操作Hbase
发布日期:2021-05-07 00:19:17
浏览次数:21
分类:技术文章
本文共 5774 字,大约阅读时间需要 19 分钟。
Hadoop_Java操作Hbase
package com.lius.hadoop.hbase;import java.io.IOException;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.Admin;import org.apache.hadoop.hbase.client.Connection;import org.apache.hadoop.hbase.client.ConnectionFactory;import org.apache.hadoop.hbase.client.Delete;import org.apache.hadoop.hbase.client.Durability;import org.apache.hadoop.hbase.client.Get;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.client.Table;import org.apache.hadoop.hbase.util.Bytes;import org.apache.log4j.Logger;/** * java操作Hbase * @author Administrator * */public class operationHbase { public static Logger log = Logger.getLogger(operationHbase.class); public static Configuration conf = null; private static Connection connection = null; //初始化配置 static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } public static void main(String[] args) throws IOException { init(); //创建表// createTable(TableName.valueOf("test")); //插入数据// insertData("marry","test"); //删除一张表 dropTable("t1"); //根据rowkey删除一条记录// deleteRow("xiaoming","test"); //查询所有数据// QueryAll("test"); //单条件查询,根据rowkey查询唯一一条记录// QueryByCondition1("marry","test"); connection.close(); } /** * 删除一张表 * @param string * @throws IOException */ private static void dropTable(String tableName) throws IOException { // TODO Auto-generated method stub Admin admin = connection.getAdmin(); TableName tabName = TableName.valueOf(tableName); admin.disableTable(tabName); admin.deleteTable(tabName); } /** * 根据rowkey删除一条记录 * @param string * @param string2 * @throws IOException */ private static void deleteRow(String rowkey, String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); Listlists = new ArrayList(); Delete delete = new Delete(rowkey.getBytes()); lists.add(delete); table.delete(lists); table.close(); } /** * 单条件查询,根据rowkey查询唯一一条记录 * @param string * @param string2 * @throws IOException */ private static void QueryByCondition1(String rowkey, String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); Get scan = new Get(rowkey.getBytes()); Result r = table.get(scan); List cells = r.listCells(); for(Cell cell:cells) { System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)))); } table.close(); } /** * 查询所有数据 * @param string * @throws IOException */ private static void QueryAll(String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); ResultScanner rs = table.getScanner(new Scan()); for(Result r:rs) { System.out.println("获得的rowkey:"+new String(r.getRow())); List | cells = r.listCells(); for(Cell cell:cells) { System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s ", Bytes.toString(CellUtil.cloneRow(cell)), Bytes.toString(CellUtil.cloneFamily(cell)), Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)))); } } table.close(); } private static void init() throws IOException { // TODO Auto-generated method stub connection = ConnectionFactory.createConnection(conf); } /** * 插入数据 * @param rowkey * @param tableName * @throws IOException */ private static void insertData(String rowkey, String tableName) throws IOException { // TODO Auto-generated method stub Table table = connection.getTable(TableName.valueOf(tableName)); //create Table Put put = new Put(rowkey.getBytes()); //create Put put.addColumn(Bytes.toBytes("column2"),"screen_width".getBytes(),"1080".getBytes());//addColumn(Family,key,Value) put.addColumn("column2".getBytes(), "screen_height".getBytes(), "1920".getBytes()); put.addColumn("column2".getBytes(), "url".getBytes(), "www.baidu.com".getBytes()); put.addColumn("column2".getBytes(), "event_data".getBytes(), "12|16|13|17|12|16".getBytes()); put.setDurability(Durability.SYNC_WAL); table.put(put); table.close(); } /** * 创建表 * @param string * @throws IOException */ private static void createTable(TableName tableName) throws IOException { // TODO Auto-generated method stub log.info("start create table..."); Admin hbaseAdmin = connection.getAdmin(); if(hbaseAdmin.tableExists(tableName)) { hbaseAdmin.disableTable(tableName); hbaseAdmin.deleteTable(tableName); log.info(String.format("table %s is exists,delete...", tableName.toString())); }// TableDescriptor table = TableDescriptorBuilder.newBuilder(tableName).build();// table.getColumnFamilies(); HTableDescriptor hTable = new HTableDescriptor(tableName); hTable.addFamily(new HColumnDescriptor("column1")); hTable.addFamily(new HColumnDescriptor("column2")); hTable.addFamily(new HColumnDescriptor("column3")); hbaseAdmin.createTable(hTable); log.info(String.format("end create table %s", tableName.toString())); }} |
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月28日 21时45分23秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【unity shader 入门精要】CH2 渲染流水线
2019-03-04
【unity shader 入门精要】CH7 基础纹理
2019-03-04
java学习笔记6:windows、linux安装配置jdk
2019-03-04
java学习笔记24:文档注释与代码块
2019-03-04
java学习笔记31:Arrays类介绍使用
2019-03-04
java学习笔记36:Integer的基本方法
2019-03-04
java并发学习2:线程的应用
2019-03-04
java并发学习12:问题引入
2019-03-04
java并发学习20:park与unpark
2019-03-04
java并发学习24:固定运行顺序模式
2019-03-04
html5学习9:HTML5文档结构详解
2019-03-04
介绍一个不错的分析客户价值的模型RFM
2019-03-04
SpringMVC---使用
2019-03-04
2.2.4 加减法运算和溢出判断更换
2019-03-04
2.2.6 强制类型转换
2019-03-04
计算机网络教程 谢希仁 第三章 数据链路层
2019-03-04
Redis缓存数据的处理流程
2019-03-04
Linux:文件句柄泄漏问题
2019-03-04
Linux:多线程简介
2019-03-04