Hbase的测试代码是怎样的
发表于:2024-09-30 作者:千家信息网编辑
千家信息网最后更新 2024年09月30日,这篇文章主要介绍"Hbase的测试代码是怎样的",在日常操作中,相信很多人在Hbase的测试代码是怎样的问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Hbase的测试代
千家信息网最后更新 2024年09月30日Hbase的测试代码是怎样的
这篇文章主要介绍"Hbase的测试代码是怎样的",在日常操作中,相信很多人在Hbase的测试代码是怎样的问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"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.MasterNotRunningException;import org.apache.hadoop.hbase.TableName;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.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.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.Filter;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.apache.hadoop.hbase.util.Bytes;public class HBaseTest { public static Configuration configuration; public static Connection conn = null; static { configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.property.clientPort", "2181");// configuration.set("hbase.zookeeper.quorum", "bfct163");// configuration.set("hbase.master", "bfct162:60000"); configuration.set("hbase.zookeeper.quorum", "192.168.17.163"); configuration.set("hbase.master", "192.168.17.162:60000"); try { conn = ConnectionFactory.createConnection(configuration); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) {// createTable("wujintao");// insertData("wujintao"); QueryAll("wujintao");// QueryByCondition1("wujintao");// QueryByCondition2("wujintao");// QueryByCondition3("wujintao");// deleteRow("wujintao","112233bbbcccc");// deleteByCondition("wujintao","abcdef"); // dropTable("wujintao"); } /** * 创建表 * * @param tableName */ public static void createTable(String tableName) { System.out.println("start create table ......"); try { TableName TABLE = TableName.valueOf(tableName); Admin hBaseAdmin = conn.getAdmin(); if (hBaseAdmin.tableExists(TABLE)) {// 如果存在要创建的表,那么先删除,再创建 hBaseAdmin.disableTable(TABLE); hBaseAdmin.deleteTable(TABLE); System.out.println(tableName + " is exist,detele...."); } HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE); tableDescriptor.addFamily(new HColumnDescriptor("column1")); tableDescriptor.addFamily(new HColumnDescriptor("column2")); tableDescriptor.addFamily(new HColumnDescriptor("column3")); hBaseAdmin.createTable(tableDescriptor); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end create table ......"); } /** * 插入数据 * * @param tableName */ public static void insertData(String tableName){ System.out.println("start insert data ......"); try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Put put = new Put("112233bbbcccc".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值 put.addColumn("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列addColumn put.addColumn("column2".getBytes(), null, "bbb".getBytes());// 本行数据的第三列 put.addColumn("column3".getBytes(), null, "ccc".getBytes());// 本行数据的第三列 table.put(put); } catch (IOException e) { e.printStackTrace(); } System.out.println("end insert data ......"); } /** * 删除一张表 * * @param tableName */ public static void dropTable(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Admin admin = conn.getAdmin(); admin.disableTable(TABLE); admin.deleteTable(TABLE); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 根据 rowkey删除一条记录 * * @param tablename * @param rowkey */ public static void deleteRow(String tableName, String rowkey) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Listlist = new ArrayList (); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println("删除行成功!"); } catch (IOException e) { e.printStackTrace(); } } /** * 组合条件删除 * * @param tablename * @param rowkey */ public static void deleteByCondition(String tablename, String rowkey) { // 目前还没有发现有效的API能够实现 根据非rowkey的条件删除 这个功能能,还有清空表全部数据的API操作 } /** * 查询所有数据 * * @param tableName */ public static void QueryAll(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); ResultScanner rs = table.getScanner(new Scan()); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } } catch (IOException e) { e.printStackTrace(); } } /** * 单条件查询,根据rowkey查询唯一一条记录 * * @param tableName */ public static void QueryByCondition1(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Get scan = new Get("112233bbbcccc".getBytes());// 根据rowkey查询 Result r = table.get(scan); System.out.println("获得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } catch (IOException e) { e.printStackTrace(); } } /** * 单条件按查询,查询多条记录 * * @param tableName */ public static void QueryByCondition2(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Filter filter = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); // 当列column1的值为aaa时进行查询 Scan s = new Scan(); s.setFilter(filter); ResultScanner rs = table.getScanner(s); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } } catch (Exception e) { e.printStackTrace(); } } /** * 组合条件查询 * * @param tableName */ public static void QueryByCondition3(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); List filters = new ArrayList (); Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); filters.add(filter1); Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("column2"), null, CompareOp.EQUAL, Bytes.toBytes("bbb")); filters.add(filter2); Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes("column3"), null, CompareOp.EQUAL, Bytes.toBytes("ccc")); filters.add(filter3); FilterList filterList1 = new FilterList(filters); Scan scan = new Scan(); scan.setFilter(filterList1); ResultScanner rs = table.getScanner(scan); for (Result r : rs) { System.out.println("获得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } rs.close(); } catch (Exception e) { e.printStackTrace(); } }}
到此,关于"Hbase的测试代码是怎样的"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
数据
查询
条件
代码
测试
学习
本行
方法
更多
帮助
组合
实用
有效
成功
接下来
一行
代表
功能
多条
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
系统软件开发需要多长时间
2008电脑版正版数据库
内网爬虫 对网络安全
软件开发过程架构
农行软件开发中心 笔试经验
sql查数据库版本
浦东新区个人数据库服务有哪些
网络安全法谁负责谁运营
钉钉的服务器数量
在哪里学网络安全工程师
淘宝新店铺数据 软件开发
南京海航软件开发系统
域名服务器可以一起买吗
小红书用什么软件开发的
陕西煤炭网络技术
托管服务器安全吗
计算机网络技术新兴岗位
北京红孩儿互联网科技
软件开发中的pg
利用读秀数据库检索图书
服务器支持java
微信小程序的无法连接服务器
大型系统软件开发完工百分比
软件开发完怎么保护
自建邮件服务器 垃圾邮件
陕西煤炭网络技术
应用软件开发计划书
手机网易邮箱服务器拒绝发送
o数据库查看表字段
网络技术学院奖海报