大數據之Hadoop學習(七)Java API編程實例對HBase數據庫進行增刪改查等操作

一、啓動Hadoop和HBase

1.啓動Hadoop

cd /usr/local/hadoop/
./sbin/start-dfs.sh

在這裏插入圖片描述

2.啓動HBase

cd /usr/local/hbase/
bin/start-hbase.sh

在這裏插入圖片描述

二、新建Java Project——>新建Class

在這裏插入圖片描述
在這裏插入圖片描述
在工程中導入外部jar包:
導入hbase安裝目錄中的lib文件中的所有jar包
在這裏插入圖片描述
在這裏插入圖片描述

三、對數據庫的增刪改查

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;

public class ExampleForHbase {
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;

	// 主函數中的語句請逐句執行,只需刪除其前的//即可,如:執行insertRow時請將其他語句註釋
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		while (true) {
			System.out.println("******************HBase對數據庫的操作******************");
			System.out.println("1.創建一個表");
			System.out.println("2.在Score表中插入一條數據,其行鍵爲95001,sname爲Mary");
			System.out.println("3.在Score表中插入一條數據,其行鍵爲95001,course:Math爲88");
			System.out.println("4.在Score表中插入一條數據,其行鍵爲95001,course:English爲85");
			System.out.println("5.刪除Score表中指定列數據,其行鍵爲95001,列族爲course,列爲Math");
			System.out.println("6.刪除Score表中指定列族數據,其行鍵爲95001,列族爲course");
			System.out.println("7.刪除Score表中指定行數據,其行鍵爲95001");
			System.out.println("8.查詢Score表中,行鍵爲95001,列族爲course,列爲Math的值");
			System.out.println("9.查詢Score表中,行鍵爲95001,列族爲sname的值");
			System.out.println("10.刪除Score表");
			System.out.println("請輸入相對序號:");
			int a = in.nextInt();
			switch (a) {
			
			case 1:
				// 創建一個表,表名爲Score,列族爲sname,course
				createTable("Score", new String[] { "sname", "course" });
				break;
				
			case 2:
				// 在Score表中插入一條數據,其行鍵爲95001,sname爲Mary(因爲sname列族下沒有子列所以第四個參數爲空)
				// 等價命令:put 'Score','95001','sname','Mary'
				insertRow("Score", "95001", "sname", "", "Mary");
				break;
				
			case 3:
				// 在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列)
				// 等價命令:put 'Score','95001','score:Math','88'
				insertRow("Score", "95001", "course", "Math", "88");
				break;
				
			case 4:
				// 在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列)
				// 等價命令:put 'Score','95001','score:English','85'
				insertRow("Score", "95001", "course", "English", "85");
				break;
				
			case 5:
				// 1、刪除Score表中指定列數據,其行鍵爲95001,列族爲course,列爲Math
				// 執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼取消註釋註釋,將刪除制定列族的代碼註釋
				// 等價命令:delete 'Score','95001','score:Math'
				deleteRow("Score", "95001", "course", "Math");
				break;
				
			case 6:
				// 2、刪除Score表中指定列族數據,其行鍵爲95001,列族爲course(95001的Math和English的值都會被刪除)
				// 執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼註釋,將刪除制定列族的代碼取消註釋
				// 等價命令:delete 'Score','95001','score'
				deleteRow("Score", "95001", "course", "");
				break;
				
			case 7: 
				// 3、刪除Score表中指定行數據,其行鍵爲95001
				// 執行這句代碼前請deleteRow方法的定義中,將刪除指定列數據的代碼註釋,以及將刪除制定列族的代碼註釋
				// 等價命令:deleteall 'Score','95001'
				deleteRow("Score", "95001", "", "");

				
			case 8:
				// 查詢Score表中,行鍵爲95001,列族爲course,列爲Math的值
				getData("Score", "95001", "course", "Math");
				break;
				
			case 9:
				// 查詢Score表中,行鍵爲95001,列族爲sname的值(因爲sname列族下沒有子列所以第四個參數爲空)
				getData("Score", "95001", "sname", "");
				break;
				
			case 10:
				// 刪除Score表
				deleteTable("Score");
				break;
			}
		}
	}

	// 建立連接
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		try {
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 關閉連接
	public static void close() {
		try {
			if (admin != null) {
				admin.close();
			}
			if (null != connection) {
				connection.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 建表。HBase的表中會有一個系統默認的屬性作爲主鍵,主鍵無需自行創建,默認爲put命令操作中表名後第一個數據,因此此處無需創建id* 
	 * @param myTableName
	 *            表名
	 * @param colFamily
	 *            列族名
	 * @throws IOException
	 */
	public static void createTable(String myTableName, String[] colFamily)
			throws IOException {

		init();
		TableName tableName = TableName.valueOf(myTableName);

		if (admin.tableExists(tableName)) {
			System.out.println("talbe is exists!");
		} else {
			HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
			for (String str : colFamily) {
				HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
				hTableDescriptor.addFamily(hColumnDescriptor);
			}
			admin.createTable(hTableDescriptor);
			System.out.println("create table success");
		}
		close();
	}

	/**
	 * 刪除指定表
	 * 
	 * @param tableName
	 *            表名
	 * @throws IOException
	 */
	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();
	}

	/**
	 * 查看已有表
	 * 
	 * @throws IOException
	 */
	public static void listTables() throws IOException {
		init();
		HTableDescriptor hTableDescriptors[] = admin.listTables();
		for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
			System.out.println(hTableDescriptor.getNameAsString());
		}
		close();
	}

	/**
	 * 向某一行的某一列插入數據
	 * 
	 * @param tableName
	 *            表名
	 * @param rowKey
	 *            行鍵
	 * @param colFamily
	 *            列族名
	 * @param col
	 *            列名(如果其列族下沒有子列,此參數可爲空)
	 * @param val
	 ** @throws IOException
	 */
	public static void insertRow(String tableName, String rowKey,
			String colFamily, String col, String val) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Put put = new Put(rowKey.getBytes());
		put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
		table.put(put);
		table.close();
		close();
	}

	/**
	 * 刪除數據
	 * 
	 * @param tableName
	 *            表名
	 * @param rowKey
	 *            行鍵
	 * @param colFamily
	 *            列族名
	 * @param col
	 *            列名
	 * @throws IOException
	 */
	public static void deleteRow(String tableName, String rowKey,
			String colFamily, String col) throws IOException {
		init();
		Table table = connection.getTable(TableName.valueOf(tableName));
		Delete delete = new Delete(rowKey.getBytes());
		// 刪除指定列族的所有數據
		// delete.addFamily(colFamily.getBytes());
		// 刪除指定列的數據
		// delete.addColumn(colFamily.getBytes(), col.getBytes());

		table.delete(delete);
		table.close();
		close();
	}

	/**
	 * 根據行鍵rowkey查找數據
	 * 
	 * @param tableName
	 *            表名
	 * @param rowKey
	 *            行鍵
	 * @param colFamily
	 *            列族名
	 * @param col
	 *            列名
	 * @throws IOException
	 */
	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(rowKey.getBytes());
		get.addColumn(colFamily.getBytes(), col.getBytes());
		Result result = table.get(get);
		showCell(result);
		table.close();
		close();
	}

	/**
	 * 格式化輸出
	 * 
	 * @param result
	 */
	public static void showCell(Result result) {
		Cell[] cells = result.rawCells();
		for (Cell cell : cells) {
			System.out.println("RowName:" + new String(CellUtil.cloneRow(cell))
					+ " ");
			System.out.println("Timetamp:" + cell.getTimestamp() + " ");
			System.out.println("column Family:"
					+ new String(CellUtil.cloneFamily(cell)) + " ");
			System.out.println("row Name:"
					+ new String(CellUtil.cloneQualifier(cell)) + " ");
			System.out.println("value:" + new String(CellUtil.cloneValue(cell))
					+ " ");
		}
	}
}

自行選擇相應的序號進行操作即可
在這裏插入圖片描述
在這裏插入圖片描述
每次執行完,都可以回到shell界面查看是否執行成功,如:執行完插入數據後,在shell界面中執行scan ‘Score’

scan 'Score'

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章