HBase常用的JAVA API操作

来源:https://www.cnblogs.com/mcq1999/archive/2019/10/28/11755883.html
-Advertisement-
Play Games

為了方便以後查看,總結了一些常用的java操作hbase的代碼: ...


為了方便以後查看,總結了一些常用的java操作hbase的代碼:

package com.mcq;

import static org.hamcrest.CoreMatchers.describedAs;
import static org.hamcrest.CoreMatchers.nullValue;

import java.io.IOException;
import java.io.PushbackInputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScannable;
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.Get;
import org.apache.hadoop.hbase.client.HTable;
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;

public class TestHbase {
	private static Admin admin = null;
	private static Connection connection = null;
	private static Configuration conf = null;
	static {
		// HBase配置文件
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "192.168.1.103");
		// 獲取連接對象
		try {
			connection = ConnectionFactory.createConnection(conf);
			// 獲取HBase管理員對象
			admin = connection.getAdmin();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void close(Connection conn, Admin admin) throws IOException {
		if (conn != null) {
			conn.close();
		}
		if (admin != null) {
			admin.close();
		}
	}

	// 判斷表是否存在
	public static boolean tableExist(String tableName) throws IOException {
		boolean tableExists = admin.tableExists(TableName.valueOf(tableName));
		return tableExists;
	}

	// 創建表
	public static void createTable(String tableName, String... cfs) throws IOException {
		if (tableExist(tableName)) {
			System.out.println("表已存在");
			return;
		}
		// cfs是列族,官方建議一個表一個,但可以有多個
		// 創建表描述器
		HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
		for (String cf : cfs) {
			// 創建列描述器
			HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
//			hColumnDescriptor.setMaxVersions(3);//設置版本數
			hTableDescriptor.addFamily(hColumnDescriptor);
		}
		// 創建表操作
		admin.createTable(hTableDescriptor);
	}

	// 刪除表
	public static void deleteTable(String tableName) throws IOException {
		if (!tableExist(tableName)) {
			System.out.println("表不存在");
			return;
		}
		// 使表不可用(下線)
		admin.disableTable(TableName.valueOf(tableName));
		// 執行刪除操作
		admin.deleteTable(TableName.valueOf(tableName));
		System.out.println("表已刪除");
	}

	// 增、改
	public static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
		// 獲取表對象
//		HTable table=new HTable(conf,TableName.valueOf(tableName)); 已過時
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 創建put對象
		Put put = new Put(Bytes.toBytes(rowKey));
		// 添加數據
		put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value));
		// 執行添加操作
		table.put(put);
	}

	// 刪
	public static void delete(String tableName, String rowKey, String cf, String cn) throws IOException {
		// 獲取table對象
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 創建delete對象
		Delete delete = new Delete(Bytes.toBytes(rowKey));// 刪除整個列族
		delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(cn));// 刪除所有版本
//		delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));不推薦,只刪除最新的版本
		// 執行刪除操作
		table.delete(delete);
		table.close();
	}

	// 查——全表掃描,只能獲取最新版本
	public static void scanTable(String tableName) throws IOException {
		// 獲取table對象
		Table table = connection.getTable(TableName.valueOf(tableName));
		// 構建掃描器
		Scan scan = new Scan();
		ResultScanner resultScanner = table.getScanner(scan);

		// 遍曆數據並列印
		for (Result result : resultScanner) { // rowkey
			Cell[] cells = result.rawCells();
			for (Cell cell : cells) { // cell
				System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
						+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
						+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
						+ Bytes.toString(CellUtil.cloneValue(cell)));
			}
		}
		table.close();
	}

	// 查——獲取指定列族
	public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(Bytes.toBytes(rowKey));
		get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
//		get.addFamily(cf);
//		get.setMaxVersions();//不傳參預設是表結構內的maxversions
		get.setMaxVersions(2);
		Result result = table.get(get);
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) { // cell
			System.out.println("RK:" + Bytes.toString(CellUtil.cloneRow(cell)) + ",CF:"
					+ Bytes.toString(CellUtil.cloneFamily(cell)) + ",CN:"
					+ Bytes.toString(CellUtil.cloneQualifier(cell)) + ",VALUE:"
					+ Bytes.toString(CellUtil.cloneValue(cell)));
		}
	}

	public static void main(String[] args) throws IOException {
		// 判斷表是否存在
//		System.out.println(tableExist("student"));
//		System.out.println(tableExist("staff"));

		// 創建表
//		createTable("staff", "info");
//		System.out.println(tableExist("staff"));

		// 刪除表
//		deleteTable("staff");

		// 增、改
//		putData("student", "1001", "info", "name", "mcq");

		// 刪
//		delete("student", "1001", "info", "name");

		// 查——全表掃描
//		scanTable("student");

		//查——獲取指定列族
		getData("student", "1001","info","name");
		
		// 關閉資源
		close(connection, admin);
	}
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 昨天: __ "圖論 概念與記錄圖的方法" __ 以上是昨天的Blog,有需要者請先閱讀完以上再閱讀今天的Blog。 可能今天的有點亂,好好理理,認真看完相信你會懂得 分割線 第二天 引子:昨天我們~~簡單~~講了講圖的概念與記錄圖的方法,那麼大家有一定的底子了,我們就開始初步接觸圖論演算法了! 我們 ...
  • Linux本地內核提權漏洞復現(CVE-2019-13272) 一、漏洞描述 當調用PTRACE_TRACEME時,ptrace_link函數將獲得對父進程憑據的RCU引用,然後將該指針指向get_cred函數。但是,對象struct cred的生存周期規則不允許無條件地將RCU引用轉換為穩定引用。 ...
  • 伺服器類型:linux =》centos 7.X以上版本 常用命令使用紫色加粗標明 1.安裝yum-util(為配置docker安裝時使用阿裡鏡像做準備):yum install -y yum-utils device-mapper-persistent-data lvm2 2.配置docker安裝 ...
  • 在httprunner運行報錯問題 代碼書寫: 當此段yml文件運行時會出現一下錯誤 在出現此錯誤時,為base_url的格式出現錯誤,只需修改base_url的位置 修改位置後運行yml文件 運行成功 ...
  • vmware workstation 虛擬機官方下載路徑:https://www.vmware.com/cn/products/workstation pro/workstation pro evaluation.html CentOS 官方下載路徑:https://www.centos.org/d ...
  • 回到目錄 在共集組態中,“輸入埠”和“輸出埠”共用BJT的集電極端子(故稱為“共集”),形成一個雙埠網路,如下圖所示: 圖3-7.01 由於共集組態的電路接法和共射組態的電路接法類似,只是輸出端從發射極取出而已,所以共射組態電路的各種輸入輸出特性和分析結果可以直接用到共集組態上,這裡就不再贅述 ...
  • [oot@cent65 bin]# service mysqld startStarting MySQL.2019-10-28T15:56:47.786960Z mysqld_safe error: log-error set to '/data/log/mysqld.log', however f ...
  • [TOC] 資料庫是什麼 資料庫:存儲數據的倉庫 為什麼使用資料庫 之前用excel來進行管理數據,有諸多問題: 電子錶格只能處理有限的數據列和數據行,對於數百萬玩、數千萬等巨大的數據列很難有效地處理 電子錶格無法提供安全、方便地許可權管理和控制手段 電子錶格很難實現多個數據之間地關聯 電子錶格很難實 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...