b0120 Hbase 数据库使用
说明
版本 hbase 2.3.6
安装在hadoop 上
安装
单机版
分布式
b0105 大数据集群-2021分布式A(手动3节点)-环境搭建
b0106 大数据集群-2021分布式B(服务器)-环境搭建
使用
命令行
参考
HBase Shell常用命令和基本操作(附带实例) (归档为知笔记 )
查看自带帮助
linux
: hbase
hbase> help
python
安装依赖包
pip install happybase pip install thrift
服务器启动hbase thrift 服务
hbase-daemon.sh start thrift jps 16363 ThriftServer netstat -anp|grep 9090 tcp6 0 0 :::9090 :::* LISTEN 16363/java
D:\1_common\python\code\projects\Bigdata2022
测试连接
# -*- coding: utf-8 -*- import happybase #链接,端口默认是9090 hbase thrift 启动的默认端口也是9090 connection = happybase.Connection('hc2108',port=9090) # connection.tables():获取Hbase实例中的表名列表,返回一个list table_name_list = connection.tables() print(table_name_list) connection.close()
ddl
# -*- coding: utf-8 -*- """ """ import happybase conn = happybase.Connection("hc2108", 9090) # 创建表---> zhy为表名,info 为列族名 conn.create_table('zhy', {"info":{}, "info2":{}}) # 删除表--> disable默认为False,删除表的手要修改为True # conn.delete_table("zhy", True) conn.close()
dml
# -*- coding: utf-8 -*- import happybase connection = happybase.Connection('hc2108',port=9090) # 获取一个表对象 table = connection.table('zhy') print(table) # <happybase.table.Table name=b'zhy'> # table.put(rowkey, {"info:content": value}) table.put("1004",{'info:name':"111111"}) table.put("1005",{'info:name':"5555555"}) table.put("1005",{'info:age':"65"}) table.put("1103",{'info:name':"103"}) # 删除 # table.delete("1103") connection.close()
query
# -*- coding: utf-8 -*- import happybase #链接,端口默认是9090 hbase thrift 启动的默认端口也是9090 connection = happybase.Connection('hc2108',port=9090) # connection.tables():获取Hbase实例中的表名列表,返回一个list table_name_list = connection.tables() print(table_name_list) # 获取一个表对象 table = connection.table('zhy') # 根据一个完整的rowkey 查询 row = table.row("1005") print(row) # out {b'info:age': b'65', b'info:name': b'5555555'} row = table.rows(["1004","1005"]) print(row) # out [(b'1004', {b'info:name': b'111111'}), (b'1005', {b'info:age': b'65', b'info:name': b'5555555'})] # ----------------- scan # for key, value in table.scan(): # # print(key,value) # # 读取中文乱码 解决 # data = str(value).encode().decode('unicode-escape').encode('raw_unicode_escape').decode() # print(key, str(data)) """ b'1004' {b'info:name': b'111111'} b'1005' {b'info:age': b'65', b'info:name': b'5555555'} """ # ----------------- scan 指定row_start和row_stop参数来设置开始和结束扫描的row key # for key, value in table.scan(row_start=b'100',row_stop=b'101', columns=['info:name','info:age']): # print(key,value) """ b'1004' {b'info:name': b'111111'} b'1005' {b'info:age': b'65', b'info:name': b'5555555'} """ # ----------------- scan 查询rowkey 以什么开头 # for key, value in table.scan(row_prefix=b'100'): # print(key,value) connection.close()
参考
python操作Hbase ,王轩12, 2021-04-03 11:28:56
java
本地工程位置
D:\1_common\java\code\projects\idea\BigData2022\DB
package priv.szb.db.java.hbase;
maven 添加以下依赖 pom.xml
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-auth</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.3.6</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.3.1</version> </dependency> </dependencies>
连接测试
package priv.szb.db.java.hbase; 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.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; public class TestConnect { public static void main(String[] args) { String tableName = "zhy"; try { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort","2181"); // zookeepe端口 conf.set("hbase.zookeeper.quorum","192.168.1.10"); // zookeepe服务器 Connection connection = ConnectionFactory.createConnection(conf); HBaseAdmin admin = (HBaseAdmin) connection.getAdmin(); System.out.println(admin.tableExists( TableName.valueOf(tableName) )); // out true // 关闭连接 if(admin != null){ try { admin.close(); } catch (IOException e) { e.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } }
例1
package priv.szb.db.java.hbase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /** * 2022-07-17 * hbase 测试,跑通.基于以下修改 * * Hbase Java客户端访问集群 * https://www.codenong.com/js54bfae1c199a/ */ public class HBaseTest { public static Configuration configuration; public static Connection connection; public static Admin admin; public static void main(String[] args) throws IOException { getClusterStatus(); // listTables(); // createTable("t2",new String[]{"cf1","cf2"}); // listTables(); // insterRow("t2", "rw1", "cf1", "q1", "val1"); // getData("t2", "rw1", "cf1", "q1"); // scanData("t2", "rw1", "rw2"); // deleRow("t2","rw1","cf1","q1"); // deleteTable("t2"); } //初始化链接 public static void init(){ configuration = HBaseConfiguration.create(); // configuration.set("hbase.zookeeper.quorum","116.196.64.103"); //ip配置一个也可以,但是host需要都配 configuration.set("hbase.zookeeper.quorum","192.168.1.10"); configuration.set("hbase.zookeeper.property.clientPort","2181"); configuration.set("zookeeper.znode.parent","/hbase"); try { connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); } catch (IOException e) { e.printStackTrace(); } } //关闭连接 public static void close(){ try { if(null != admin) admin.close(); if(null != connection) connection.close(); } catch (IOException e) { e.printStackTrace(); } } //建表 public static void createTable(String tableNmae,String[] cols) throws IOException { init(); TableName tableName = TableName.valueOf(tableNmae); if(admin.tableExists(tableName)){ System.out.println("####talbe is exists!####"); }else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for(String col:cols){ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } close(); } //删表 public static void deleteTable(String tableName) throws IOException { init(); TableName tn = TableName.valueOf(tableName); if (admin.tableExists(tn)) { admin.disableTable(tn); admin.deleteTable(tn); } close(); } //查看已有表 public static void listTables() throws IOException { init(); System.out.println("查看已有表:"); HTableDescriptor hTableDescriptors[] = admin.listTables(); for(HTableDescriptor hTableDescriptor :hTableDescriptors){ System.out.println(hTableDescriptor.getNameAsString()); } close(); } //插入数据 public static void insterRow(String tableName,String rowkey,String colFamily,String col,String val) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(Bytes.toBytes(rowkey)); put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col), Bytes.toBytes(val)); table.put(put); //批量插入 /* List<Put> putList = new ArrayList<Put>(); puts.add(put); table.put(putList);*/ table.close(); close(); } //删除数据 public static void deleRow(String tableName,String rowkey,String colFamily,String col) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowkey)); //删除指定列族 //delete.addFamily(Bytes.toBytes(colFamily)); //删除指定列 //delete.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col)); table.delete(delete); //批量删除 /* List<Delete> deleteList = new ArrayList<Delete>(); deleteList.add(delete); table.delete(deleteList);*/ table.close(); close(); } //根据rowkey查找数据 public static void getData(String tableName,String rowkey,String colFamily,String col)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(Bytes.toBytes(rowkey)); //获取指定列族数据 //get.addFamily(Bytes.toBytes(colFamily)); //获取指定列数据 //get.addColumn(Bytes.toBytes(colFamily),Bytes.toBytes(col)); Result result = table.get(get); showCell(result); table.close(); close(); } //格式化输出 public static void showCell(Result result){ Cell[] cells = result.rawCells(); for(Cell cell:cells){ System.out.println("row key:"+new String(CellUtil.cloneRow(cell))+" "); System.out.println("Timetamp:"+cell.getTimestamp()+" "); System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); System.out.println("column Name:"+new String(CellUtil.cloneQualifier(cell))+" "); System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); } } //获取集群状态 public static void getClusterStatus() { init(); ClusterStatus status = null; try { status = admin.getClusterStatus(); } catch (IOException e) { e.printStackTrace(); } System.out.println("getClusterStatus集群状态: " + status); close(); } //批量查找数据 public static void scanData(String tableName,String startRow,String stopRow)throws IOException{ init(); Table table = connection.getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); //scan.setStartRow(Bytes.toBytes(startRow)); //scan.setStopRow(Bytes.toBytes(stopRow)); ResultScanner resultScanner = table.getScanner(scan); for(Result result : resultScanner){ showCell(result); } table.close(); close(); } }
例2
package priv.szb.db.java.hbase; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.hadoop.hbase.client.*; 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.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.util.Bytes; /** * 2022-07-17 * hbase 测试,跑通.基于以下修改 * * java maven调用hbase * https://blog.csdn.net/whq12789/article/details/88234592 */ public class HBaseService { public static Configuration configuration; public static Connection connection; static { // System.setProperty("hadoop.home.dir", "D:\\Program\\hadoop-common-2.6.0-bin-master"); configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.property.clientPort", "2181"); configuration.set("hbase.zookeeper.quorum", "192.168.1.10"); configuration.set("hbase.client.keyvalue.maxsize","500000"); try { connection=ConnectionFactory.createConnection(configuration); } catch (IOException e) { e.printStackTrace(); } //configuration.set("hbase.master", "192.168.1.100:600000"); } public static void main(String[] args) { // createTable("tbtest"); // insertData("tbtest"); // QueryAll("tbtest"); // QueryByCondition1("tbtest"); // QueryByCondition2("tbtest"); // QueryByCondition3("tbtest"); // deleteRow("tbtest","rowkey123"); // dropTable("tbtest"); } public static void showCell(Result result) { Cell[] cells = result.rawCells(); for (Cell cell : cells) { System.out.println("row key:" + new String(CellUtil.cloneRow(cell)) + " "); System.out.println("Timetamp:" + cell.getTimestamp() + " "); System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)) + " "); System.out.println("column Name:" + new String(CellUtil.cloneQualifier(cell)) + " "); System.out.println("value:" + new String(CellUtil.cloneValue(cell)) + " "); } } public static void createTable(String tableName) { System.out.println("start create table ......"); try { Admin hBaseAdmin = connection.getAdmin(); if (hBaseAdmin.tableExists(TableName.valueOf(tableName))) {// 如果存在要创建的表,那么先删除,再创建 hBaseAdmin.disableTable(TableName.valueOf(tableName)); hBaseAdmin.deleteTable(TableName.valueOf(tableName)); System.out.println(tableName + " is exist,delete...."); } HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); 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 ......"); } public static void insertData(String tableName) { System.out.println("start insert data ......"); try { HTable table = (HTable) connection.getTable(TableName.valueOf(tableName)); Put put = new Put("rowkey123".getBytes());// 一个PUT代表一行数据,再NEW一个PUT表示第二行数据,每行一个唯一的ROWKEY,此处rowkey为put构造方法中传入的值 put.addColumn("column1".getBytes(), null, "aaa".getBytes());// 本行数据的第一列 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 ......"); } public static void dropTable(String tableName) { try { Admin admin = connection.getAdmin();; admin.disableTable(TableName.valueOf(tableName)); admin.deleteTable(TableName.valueOf(tableName)); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void deleteRow(String tableName, String rowkey) { try { Table table = connection.getTable(TableName.valueOf(tableName)); List list = new ArrayList(); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println("删除行成功!"); } catch (IOException e) { e.printStackTrace(); } } public static void QueryAll(String tableName) { try { 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())); showCell(r); } } catch (IOException e) { e.printStackTrace(); } } public static void QueryByCondition1(String tableName) { try { Table table = connection.getTable(TableName.valueOf(tableName)); Get scan = new Get("rowkey123".getBytes());// 根据rowkey查询 Result r = table.get(scan); System.out.println("获得到rowkey:" + new String(r.getRow())); showCell(r); } catch (IOException e) { e.printStackTrace(); } } public static void QueryByCondition2(String tableName) { try { Table table = connection.getTable(TableName.valueOf(tableName)); 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())); showCell(r); } } catch (Exception e) { e.printStackTrace(); } } public static void QueryByCondition3(String tableName) { try { Table table = connection.getTable(TableName.valueOf(tableName)); List<Filter> filters = new ArrayList<Filter>(); 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())); showCell(r); } rs.close(); } catch (Exception e) { e.printStackTrace(); } } }
写满200篇博文再说