当前位置: 代码迷 >> 综合 >> Windows Hbase Docker实践
  详细解决方案

Windows Hbase Docker实践

热度:74   发布时间:2023-09-18 17:19:37.0

因为生产和测试集群要连内网有诸多不便,所以本地docker测试成为不二之选

步骤

1.生成docker container并运行

docker run -d -p 2181:2181 -p 8080:8080 -p 8085:8085 -p 9090:9090 -p 9095:9095 -p 16000:16000 -p 16010:16010 -p 16201:16201 -p 16301:16301  -p 16030:16030 -p 16020:16020 --name hbase001 harisekhon/hbase
Windows Hbase Docker实践

 2.更改本地hosts

Windows Hbase Docker实践

Windows Hbase Docker实践

3.创建表并插入值

hbase shell

create 'testtable',{NAME=>'f1'},{NAME=>'c1'}
put 'testtable','888','f1:c1','9991' 

4.测试连通

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;
import java.util.ArrayList;
import java.util.List;/*** @author arvin*/
public class HbaseTest {private static Admin admin;private static final String COLUMNS_FAMILY_1 = "cf1";private static final String COLUMNS_FAMILY_2 = "cf2";public static Connection initHbase() throws IOException {Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum", "127.0.0.1");configuration.set("hbase.zookeeper.property.clientPort", "2181");configuration.set("hbase.master", "127.0.0.1:60000");Connection connection = ConnectionFactory.createConnection(configuration);return connection;}//创建表 createpublic static void createTable(TableName tableName, String[] cols) throws IOException {admin = initHbase().getAdmin();if (admin.tableExists(tableName)) {System.out.println("Table Already Exists!");} else {HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);for (String col : cols) {HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);hTableDescriptor.addFamily(hColumnDescriptor);}admin.createTable(hTableDescriptor);System.out.println("Table Create Successful");}}public static TableName getTbName(String tableName) {return TableName.valueOf(tableName);}// 删除表 droppublic static void deleteTable(TableName tableName) throws IOException {admin = initHbase().getAdmin();if (admin.tableExists(tableName)) {admin.disableTable(tableName);admin.deleteTable(tableName);System.out.println("Table Delete Successful");} else {System.out.println("Table does not exist!");}}//put 插入数据public static void insertData(TableName tableName, Student student) throws IOException {Put put = new Put(Bytes.toBytes(student.getId()));put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name"), Bytes.toBytes(student.getName()));put.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("age"), Bytes.toBytes(student.getAge()));initHbase().getTable(tableName).put(put);System.out.println("Data insert success:" + student.toString());}// delete 删除数据public static void deleteData(TableName tableName, String rowKey) throws IOException {Delete delete = new Delete(Bytes.toBytes(rowKey));      // 指定rowKey
//        delete = delete.addColumn(Bytes.toBytes(COLUMNS_FAMILY_1), Bytes.toBytes("name"));  // 指定column,也可以不指定,删除该rowKey的所有columninitHbase().getTable(tableName).delete(delete);System.out.println("Delete Success");}// scan数据public static List<Student> allScan(TableName tableName) throws IOException {ResultScanner results = initHbase().getTable(tableName).getScanner(new Scan().addFamily(Bytes.toBytes("cf1")));List<String> list = new ArrayList<>();for (Result result : results) {Student student = new Student();for (Cell cell : result.rawCells()) {String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());}}return null;}// 根据rowkey get数据public static Student singleGet(TableName tableName, String rowKey) throws IOException {Student student = new Student();student.setId(rowKey);Get get = new Get(Bytes.toBytes(rowKey));if (!get.isCheckExistenceOnly()) {Result result = initHbase().getTable(tableName).get(get);for (Cell cell : result.rawCells()) {String colName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());switch (colName) {case "name":student.setName(value);break;case "age":student.setAge(value);break;default:System.out.println("unknown columns");}
}}System.out.println(student.toString());return student;}// 查询指定Cell数据public static String getCell(TableName tableName, String rowKey, String cf, String column) throws IOException {Get get = new Get(Bytes.toBytes(rowKey));String rst = null;if (!get.isCheckExistenceOnly()) {get = get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column));try {Result result = initHbase().getTable(tableName).get(get);byte[] resByte = result.getValue(Bytes.toBytes(cf), Bytes.toBytes(column));rst = Bytes.toString(resByte);} catch (Exception exception) {System.out.printf("columnFamily or column does not exists");}}System.out.println("Value is: " + rst);return rst;}public static void main(String[] args) throws IOException {Student student = new Student();student.setId("1");student.setName("Arvin");student.setAge("18");String table = "testtable";//        createTable(getTbName(table), new String[]{COLUMNS_FAMILY_1, COLUMNS_FAMILY_2});
//        deleteTable(getTbName(table));
//        insertData(getTbName(table), student);
//        deleteData(getTbName(table), "1");
//        singleGet(getTbName(table), "2");getCell(getTbName(table), "888", "f1", "c1");}
}

结果

Windows Hbase Docker实践

  相关解决方案