生活随笔
收集整理的這篇文章主要介紹了
Hbase入门篇
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HBase:
數據庫:是一種面向列族存儲的非關系型數據庫用于存儲結構化和非結構化數據:適用于單表非關系型數據的存儲,不適合做關聯查詢,類似于JOIN等操作基于HDFS:數據持久化存儲的體現形式是HFile,存放于DataNode中,被ResionServer以Region的形式進行管理延遲較低,接入在線業務使用:面對大量的企業數據,HBase可以直線單表大量數據的存儲,同時提供了高效的數據訪問速度。?
package csdn.dreamzuora;import com.sun.istack.internal.logging.Logger;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.conf.Configuration;
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;/*** Title:* Description:** @version 1.0* @author: weijie* @date: 2020/11/10 18:02*/
public class HbaseUtils {public static Configuration conf;private Logger logger = Logger.getLogger(HbaseUtils.class);static{//使用 HBaseConfiguration 的單例方法實例化conf = HBaseConfiguration.create();conf.set("hbase.zookeeper.quorum", "39.96.204.209");conf.set("hbase.zookeeper.property.clientPort", "2181");}public Connection getConnection() throws IOException {return ConnectionFactory.createConnection(conf);}public Admin getAdmin() throws IOException {Connection connection = getConnection();return connection.getAdmin();}public Table getTable(TableName tableName) throws IOException {Connection connection = getConnection();return connection.getTable(tableName);}/*** 判斷表是否存在* @param tableName* @return* @throws IOException*/public boolean isTableExist(String tableName) throws IOException {Admin admin = getAdmin();return admin.tableExists(TableName.valueOf(tableName));}/*** 創建表* @param tableName* @param columnFamily* @throws IOException*/public void createTable(String tableName, String... columnFamily) throws IOException {Connection connection = ConnectionFactory.createConnection(conf);Admin admin = connection.getAdmin();//判斷表是否存在if (isTableExist(tableName)){logger.info("表" + tableName + "已存在");}else {//創建表屬性對象,表名需要轉字節HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//創建多個列族for (String cf : columnFamily){hTableDescriptor.addFamily(new HColumnDescriptor(cf));}//根據對表的配置,創建表admin.createTable(hTableDescriptor);logger.info("表" + tableName + "創建成功!");}}/*** 刪除表*/public void dropTable(String tableName) throws IOException {Admin admin = getAdmin();if (isTableExist(tableName)){admin.disableTable(TableName.valueOf(tableName));admin.deleteTable(TableName.valueOf(tableName));logger.info("表" + tableName + "刪除成功!");}else {logger.info(tableName + "表不存在");}}/*** 向表中插入數據*/public void addRowData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {//創建表對象Table table = getTable(TableName.valueOf(tableName));//向表中插入數據Put put = new Put(Bytes.toBytes(rowkey));//向Put對象中組裝數據put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));table.put(put);table.close();logger.info("數據插入成功");}/*** 刪除多行數據*/public void deleteMultiRow(String tableName, String... rows) throws IOException {Table table = getTable(TableName.valueOf(tableName));List<Delete> deleteList = new ArrayList<Delete>();for (String row : rows){Delete delete = new Delete(Bytes.toBytes(row));deleteList.add(delete);}table.delete(deleteList);table.close();}/*** 獲取所有數據*/public void getAllRows(String tableName) throws IOException {Table table = getTable(TableName.valueOf(tableName));//得到用于掃描region的對象Scan scan = new Scan();//使用Htable得到resultcanner實現類的對象ResultScanner resultScanner = table.getScanner(scan);for (Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells){//得到rowkeylogger.info("行健: " + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}}/*** 獲取某一行數據*/public void getRow(String tableName, String rowkey) throws IOException {Table table = getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowkey));/*** get.setMaxVersions() 顯示所有版本* get.setTimeStamp(timeStamp) 顯示指定時間戳版本*/Result result = table.get(get);for (Cell cell : result.rawCells()){logger.info("行鍵: " + Bytes.toString(result.getRow()));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}/*** 獲取某一行指定"列族:列"的數據*/public void getRowQualifier(String tableName, String rowkey, String family, String qualifier) throws IOException {Table table = getTable(TableName.valueOf(tableName));Get get = new Get(Bytes.toBytes(rowkey));get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));Result result = table.get(get);for (Cell cell : result.rawCells()){logger.info("行鍵: " + Bytes.toString(result.getRow()));//得到列族logger.info("列族: " + Bytes.toString(CellUtil.cloneFamily(cell)));//列名logger.info("列名: " + Bytes.toString(CellUtil.cloneQualifier(cell)));//列值logger.info("列值: " + Bytes.toString(CellUtil.cloneValue(cell)));}}}
視頻學習地址:https://www.bilibili.com/video/BV1Y4411B7jy?p=7
總結
以上是生活随笔為你收集整理的Hbase入门篇的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。