Java API 操作HBase Shell

HBase Shell API 操作

創建工程

本實驗的環境實在ubuntu18.04下完成,首先在改虛擬機中安裝開發工具eclipse。

然後創建Java項目名字叫hbase-test

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

配置運行環境

在src下創建HBaseDemo類
在這裏插入圖片描述
然後編寫init方法和close方法,一個創建與HBASE的連接,一個關閉連接。

/**
	 * 創建連接返回admin
	 */
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", "file:///usr/local/hbase/hbase-tmp");
		// configuration.set("hbase.zookeeper.quorum", "hadoop02");
		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();
		}
	}

建表操作

	/**
	 * 創建表方法
	 * 
	 * @param myTableName
	 * @param colFamily
	 * @throws IOException
	 */
	public static void createTable(String myTableName, String[] colFamily) throws IOException {
		TableName tableName = TableName.valueOf(myTableName);
		if (admin.tableExists(tableName)) {
			System.out.println("talbe is exists!");
		} else {
			TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
			for (String str : colFamily) {
				ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
				tableDescriptor.setColumnFamily(family);
			}
			admin.createTable(tableDescriptor.build());
		}
	}

測試:創建student表,列族有score

	public static void main(String[] args) throws IOException {
		init();
		System.out.print("==================分割線===================");
		//創建student表
		  createTable("student",new String[]{"score"});
		 
		close();
	}

查看執行結果

在這裏插入圖片描述

查看現有表的名稱

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

測試:

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割線===================");
		listTables();
		System.out.println("==================分割線===================");
		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();
}

測試:

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割線===================");
		deleteTable("student");
		System.out.println("==================分割線===================");
		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();
}

測試

public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割線===================");
		deleteRow("student", "zhangsan", "score", "Math");
		System.out.println("==================分割線===================");
		close();
	}

結果 zhangsan行,score列族中的Math列被刪除

添加數據

	/**
	 * 向指定表中插入數據
	 * 
	 * @param tableName
	 * @param rowKey
	 * @param colFamily
	 * @param col
	 * @param val
	 * @throws IOException
	 */
	public static void insertData(String tableName, String rowKey, String colFamily, String col, String val)
			throws IOException {
		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();
	}

向student表中添加三條數據,分別是zhangsan的英語成績,數學成績和計算機成績。

	public static void main(String[] args) throws IOException {
		init();
		System.out.print("==================分割線===================");
		 
		  //插入三條數據
		  insertData("student","zhangsan","score","English","69");
		  insertData("student","zhangsan","score","Math","86");
		  insertData("student","zhangsan","score","Computer","77");
		 
		close();
	}

測試結果
在這裏插入圖片描述

查看數據

/**
	 * 獲取指定表中ceil數據
	 * 
	 * @param tableName
	 * @param rowKey
	 * @param colFamily
	 * @param col
	 * @throws IOException
	 */
	public static void getData(String tableName, String rowKey, String colFamily, String col) throws IOException {
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get get = new Get(rowKey.getBytes());
		get.addColumn(colFamily.getBytes(), col.getBytes());
		Result result = table.get(get);
		System.out.println(tableName+"表,"+colFamily+"列族,"+col+"的值是:"+new String(result.getValue(colFamily.getBytes(), col == null ? null : col.getBytes())));
		table.close();
	}

查看張三英語的 成績

	public static void main(String[] args) throws IOException {
		init();
		System.out.println("==================分割線===================");
		
		  getData("student","zhangsan", "score","English");
		 
		close();
	}

控制檯輸出結果:
在這裏插入圖片描述

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