HBase API(Java)之對HBase表,命名空間等的操作


項目源碼:https://github.com/cw1322311203/hbasedemo/tree/master/hbase-api

一,環境准備

具體代碼在GitHub: https://github.com/cw1322311203/hbaseapi

新建項目后在pom.xml中添加依賴:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
	<groupId>jdk.tools</groupId>
	<artifactId>jdk.tools</artifactId>
	<version>1.8</version>
	<scope>system</scope>
	<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>

二,HBaseAPI

具體代碼在GitHub: https://github.com/cw1322311203/hbaseapi

2.1 獲取Configuration對象

public static Configuration conf;
static{
	//使用HBaseConfiguration的單例方法實例化
	conf = HBaseConfiguration.create();
	conf.set("hbase.zookeeper.quorum", "192.168.9.102");
	conf.set("hbase.zookeeper.property.clientPort", "2181");
}

2.2 判斷表是否存在

public static boolean isTableExist(String tableName) throws MasterNotRunningException,
 ZooKeeperConnectionException, IOException{
	//在HBase中管理、訪問表需要先創建HBaseAdmin對象
	//Connection connection = ConnectionFactory.createConnection(conf);
	//HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
	HBaseAdmin admin = new HBaseAdmin(conf);
	return admin.tableExists(tableName);
}

2.3 創建表

public static void createTable(String tableName, String... columnFamily) throws
 MasterNotRunningException, ZooKeeperConnectionException, IOException{
	HBaseAdmin admin = new HBaseAdmin(conf);
	//判斷表是否存在
	if(isTableExist(tableName)){
		System.out.println("表" + tableName + "已存在");
		//System.exit(0);
	}else{
		//創建表屬性對象,表名需要轉字節
		HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
		//創建多個列族
		for(String cf : columnFamily){
			descriptor.addFamily(new HColumnDescriptor(cf));
		}
		//根據對表的配置,創建表
		admin.createTable(descriptor);
		System.out.println("表" + tableName + "創建成功!");
	}
}

2.4 刪除表

public static void dropTable(String tableName) throws MasterNotRunningException,
 ZooKeeperConnectionException, IOException{
	HBaseAdmin admin = new HBaseAdmin(conf);
	if(isTableExist(tableName)){
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
		System.out.println("表" + tableName + "刪除成功!");
	}else{
		System.out.println("表" + tableName + "不存在!");
	}
}

2.5 向表中插入數據

public static void addRowData(String tableName, String rowKey, String columnFamily, String
 column, String value) throws IOException{
	//創建HTable對象
	HTable hTable = new HTable(conf, tableName);
	//向表中插入數據
	Put put = new Put(Bytes.toBytes(rowKey));
	//向Put對象中組裝數據
	put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
	hTable.put(put);
	hTable.close();
	System.out.println("插入數據成功");
}

2.6 刪除多行數據(三種標記)

public static void deleteMultiRow(String tableName, String... rows) throws IOException{
	HTable hTable = new HTable(conf, tableName);
	List<Delete> deleteList = new ArrayList<Delete>();
	for(String row : rows){
		Delete delete = new Delete(Bytes.toBytes(row));
		deleteList.add(delete);
	}
	hTable.delete(deleteList);
	hTable.close();
}

刪除有三種標記

  • Delete標記: 刪除特定列列指定的版本
  • DeleteFamily標記: 刪除特定列族所有列
  • DeleteColumn標記: 刪除特定列的所有版本

指定rowkey: 使用DeleteFamily標記

  • 不加時間戳表示刪除[指定rowkey]的所有數據
  • 加時間戳表示刪除[指定rowkey]中[時間戳版本小於或等於指定時間戳]的所有數據

指定rowkey+columnFamily: 使用DeleteFamily標記

  • 不加時間戳表示刪除[指定列族]的所有數據
  • 加了時間戳就表示刪除[指定列族]下[時間戳版本小於或等於指定時間戳]的所有數據

指定rowkey+columnFamily+column(addColumns): 使用DeleteColumn標記

  • 不加時間戳表示刪除[指定列]所有版本的數據
  • 加時間戳表示刪除[指定列]中[時間戳版本小於或等於指定時間戳]的所有數據

指定rowkey+columnFamily+column(addColumn): 使用Delete標記 (只刪除單個版本數據,生產環境盡量別用)

  • 不加時間戳表示刪除[指定列]中[最新版本]的數據

  • 加時間戳表示刪除[指定列]中[指定時間戳版本]的數據

  • 不推薦的原因是:操作不同(如flush前后操作產生的結果會不一樣)結果可能不同
    在flush前如果有多個版本的數據,此時進行addColumn(不加時間戳)操作,會將最新版本的數據刪除,然后老版本的數據會出現

    在flush后進行addColumn(不加時間戳)操作,會將最新版本的數據刪除,而此時flush已將老版本的數據進行了刪除,所有此時老版本的數據就不會出現了

2.7 獲取所有數據

public static void getAllRows(String tableName) throws IOException{
	HTable hTable = new HTable(conf, tableName);
	//得到用於掃描region的對象
	Scan scan = new Scan();
	//使用HTable得到resultcanner實現類的對象
	ResultScanner resultScanner = hTable.getScanner(scan);
	for(Result result : resultScanner){
		Cell[] cells = result.rawCells();
		for(Cell cell : cells){
			//得到rowkey
			System.out.println("行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)));
			//得到列族
			System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
			System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
			System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
		}
	}
}

2.8 獲取某一行數據

public static void getRow(String tableName, String rowKey) throws IOException{
	HTable table = new HTable(conf, tableName);
	Get get = new Get(Bytes.toBytes(rowKey));
	//get.setMaxVersions();顯示所有版本
    //get.setTimeStamp();顯示指定時間戳的版本
	Result result = table.get(get);
	for(Cell cell : result.rawCells()){
		System.out.println("行鍵:" + Bytes.toString(result.getRow()));
		System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
		System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
		System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
		System.out.println("時間戳:" + cell.getTimestamp());
	}
}

2.9 獲取某一行指定“列族:列”的數據

public static void getRowQualifier(String tableName, String rowKey, String family, String
 qualifier) throws IOException{
	HTable table = new HTable(conf, 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()){
		System.out.println("行鍵:" + Bytes.toString(result.getRow()));
		System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
		System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
		System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
	}
}

三,HBaseUtils工具類

具體代碼在GitHub: https://github.com/cw1322311203/hbaseapi

1.第一種工具類

package com.cw.bigdata.util;

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;

public class HBaseUtils {
    public static Configuration conf;

    /** * TODO 獲取Configuration對象 */
    static {
        // 使用HBaseConfiguration的單例方法實例化
        conf = HBaseConfiguration.create();
        // 如果在項目中沒有加入hbase-site.xml配置文件,則可以采用以下方式配置
        //conf.set("hbase.zookeeper.quorum", "cm1.cdh.com,cm3.cdh.com,cm2.cdh.com");
        //conf.set("hbase.zookeeper.property.clientPort", "2181");
    }


    /** * TODO 判斷表是否存在 * * @param tableName * @return * @throws IOException */
    public static boolean isTableExist(String tableName) throws IOException {
        // 在HBase中管理、訪問表需要先創建HBaseAdmin對象
        HBaseAdmin admin = new HBaseAdmin(conf);
        return admin.tableExists(tableName);
    }

    /** * TODO 創建表 * * @param tableName * @param columnFamily * @throws IOException */
    public static void createTable(String tableName, String... columnFamily) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        // 判斷表是否存在
        if (isTableExist(tableName)) {
            System.out.println("表" + tableName + "已存在");
        } else {
            // 創建表屬性對象,表名需要轉字節
            HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
            // 創建多個列族
            for (String cf : columnFamily) {
                descriptor.addFamily(new HColumnDescriptor(cf));
            }
            // 根據對表的配置,創建表
            admin.createTable(descriptor);
            System.out.println("表" + tableName + "創建成功!");
        }
    }

    /** * 刪除表 * * @param tableName * @throws IOException */
    public static void dropTable(String tableName) throws IOException {
        HBaseAdmin admin = new HBaseAdmin(conf);
        if (isTableExist(tableName)) {
            admin.disableTable(tableName);
            admin.deleteTable(tableName);
            System.out.println("表" + tableName + "刪除成功!");
        } else {
            System.out.println("表" + tableName + "不存在!");
        }
    }

    /** * TODO 向表中插入數據 * * @param tableName * @param rowkey * @param columnFamily * @param column * @param value * @throws IOException */
    public static void addRowData(String tableName, String rowkey, String columnFamily, String column, String value) throws IOException {
        // 創建HTable對象
        HTable hTable = new HTable(conf, tableName);
        // 向表中插入數據
        Put put = new Put(Bytes.toBytes(rowkey));
        // 向put對象中組裝數據
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
        hTable.put(put);
        hTable.close();
        System.out.println("插入數據成功!");
    }

    /** * TODO 刪除多行數據 * * @param tableName * @param rows * @throws IOException */
    public static void deleteMultiRow(String tableName, String... rows) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        List<Delete> deleteList = new ArrayList<Delete>();
        for (String row : rows) {
            Delete delete = new Delete(Bytes.toBytes(row));
            deleteList.add(delete);
        }
        hTable.delete(deleteList);
        hTable.close();
    }

    /** * TODO 獲取所有數據 * * @param tableName * @throws IOException */
    public static void getAllRows(String tableName) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        // 得到用於掃描region的對象
        Scan scan = new Scan();
        // 使用HTable得到resultscanner實現類的對象
        ResultScanner resultScanner = hTable.getScanner(scan);
        for (Result result : resultScanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {

                // 得到rowkey
                System.out.println("******** 行鍵:" + Bytes.toString(CellUtil.cloneRow(cell)) + " *******");
                // 得到列族
                System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
                // 列
                System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
                // 值
                System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }
    }

    /** * TODO 獲取某一行數據 * * @param tableName * @param rowkey * @throws IOException */
    public static void getRow(String tableName, String rowkey) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowkey));
        //get.setMaxVersions();顯示所有版本
        //get.setTimeStamp();顯示指定時間戳的版本
        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("******** 行鍵:" + Bytes.toString(result.getRow()) + " ********");
            System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
            System.out.println("時間戳:" + cell.getTimestamp());
        }
    }

    /** * TODO 獲取某一行指定“列族:列”的數據 * * @param tableName * @param rowkey * @param family * @param qualifier * @throws IOException */
    public static void getRowQualifier(String tableName, String rowkey, String family, String qualifier) throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
        Result result = hTable.get(get);
        for (Cell cell : result.rawCells()) {
            System.out.println("******* 行鍵:" + Bytes.toString(result.getRow()) + " ******");
            System.out.println("列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
            System.out.println("列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
            System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
    }

    public static void main(String[] args) throws IOException {
        String tableName = "cw:student";
        if (isTableExist(tableName)) {
            getAllRows(tableName);
            getRow(tableName, "1001");
            getRowQualifier(tableName, "1002", "info", "name");
        }
    }
}

2.第二種工具類:使用本地線程池方式

package com.cw.bigdata.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/** * HBase操作工具類 */
public class HBaseUtil {

    // ThreadLocal
    // 只要在一個線程中緩存是共享的
    private static ThreadLocal<Connection> connHolder = new ThreadLocal<Connection>();

    //private static Connection conn = null;

    private HBaseUtil() {

    }

    /** * TODO 獲取HBase連接對象 * * @return * @throws Exception */
    public static void makeHbaseConnection() throws Exception {
        Connection conn = connHolder.get();
        if (conn == null) {
            Configuration conf = HBaseConfiguration.create();
            // 如果在項目中沒有加入hbase-site.xml配置文件,則可以采用以下方式配置
            //conf.set("hbase.zookeeper.quorum", "cm1.cdh.com,cm3.cdh.com,cm2.cdh.com");
            //conf.set("hbase.zookeeper.property.clientPort", "2181");
            conn = ConnectionFactory.createConnection(conf);
            connHolder.set(conn);
        }
    }

    /** * TODO 增加數據 * * @param rowkey * @param family * @param column * @param value * @throws Exception */
    public static void insertData(String tableName, String rowkey, String family, String column, String value) throws Exception {
        Connection conn = connHolder.get();
        Table table = conn.getTable(TableName.valueOf(tableName));

        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(value));

        table.put(put);
        table.close();
    }

    /** * TODO 關閉連接 * * @throws Exception */
    public static void close() throws Exception {
        Connection conn = connHolder.get();
        if (conn != null) {
            conn.close();
            connHolder.remove();
        }
    }
}






package com.cw.bigdata.hbase;


import com.cw.bigdata.util.HBaseUtil;

/** * 測試Hbase API */
public class TestHbaseAPI_3 {
    public static void main(String[] args) throws Exception {

        // 創建連接
        HBaseUtil.makeHbaseConnection();

        // 增加數據
        HBaseUtil.insertData("cw:student", "1002", "info", "name", "lisi");

        // 關閉連接
        HBaseUtil.close();
    }
}

3.第三種工具類

package com.cw.bigdata.hbase;

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;

/** * DDL:(Admin對象) * 1.判斷表是否存在 * 2.創建表 * 3.創建命名空間 * 4.刪除表 * <p> * DML:(Table對象) * 5.插入數據 * 6.查數據(get) * 7.查數據(scan) * 8.刪除數據 */
public class TestAPI {


    private static Connection connection = null;
    private static Admin admin = null;

    static {
        try {
            // 1.獲取配置文件信息
            // HBaseConfiguration configuration = new HBaseConfiguration();// 已過時
            Configuration configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "cm1.cdh.com,cm3.cdh.com,cm2.cdh.com");
            configuration.set("hbase.zookeeper.property.clientPort", "2181");//可寫可不寫,默認為2181

            // 2.創建連接對象
            connection = ConnectionFactory.createConnection(configuration);

            // 3.創建Admin對象
            //HBaseAdmin admin = new HBaseAdmin(configuration); // 已過時
            admin = connection.getAdmin();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** * TODO 判斷表是否存在 * * @param tableName * @return * @throws IOException */
    public static boolean isTableExist(String tableName) throws IOException {
        // 1.判斷表是否存在
        boolean exists = admin.tableExists(TableName.valueOf(tableName));

        // 2.返回結果
        return exists;
    }


    /** * TODO 創建表 * * @param tableName * @param cfs * @throws IOException */
    public static void createTable(String tableName, String... cfs) throws IOException {

        // 1.判斷是否存在列族信息
        if (cfs.length <= 0) {
            System.out.println("請設置列族信息!");
            return;
        }

        // 2.判斷表是否存在
        if (isTableExist(tableName)) {
            System.out.println(tableName + "表已存在!");
            return;
        }

        // 3.創建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));

        // 4.循環添加列族信息
        for (String cf : cfs) {
            // 5.創建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);

            // 6.添加具體的列族信息
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        // 7.創建表
        admin.createTable(hTableDescriptor);

    }

    /** * TODO 刪除表 * * @param tableName * @throws IOException */
    public static void dropTable(String tableName) throws IOException {

        // 1.判斷表是否存在
        if (!isTableExist(tableName)) {
            System.out.println(tableName + "表不存在!");
            return;
        }

        // 2.使表下線
        admin.disableTable(TableName.valueOf(tableName));

        // 3.刪除表
        admin.deleteTable(TableName.valueOf(tableName));
    }

    /** * TODO 創建命名空間 * * @param namespace */
    public static void createNameSpace(String namespace) {

        // 1.創建命名空間描述器
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(namespace).build();

        // 2.創建命名空間
        try {
            admin.createNamespace(namespaceDescriptor);
        } catch (NamespaceExistException e) {
            System.out.println(namespace + "命名空間已存在!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /** * TODO 向表中插入數據 * * @param tableName * @param rowKey * @param columnFamily * @param columnName * @param value * @throws IOException */
    public static void putData(String tableName, String rowKey, String columnFamily, String columnName, String value) throws IOException {
        // 1.獲取表對象
        Table table = connection.getTable(TableName.valueOf(tableName));

        // 2.創建put對象
        Put put = new Put(Bytes.toBytes(rowKey));

        // 3.給Put對象賦值
        put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName), Bytes.toBytes(value));

        // 4.插入數據
        table.put(put);

        // 5.關閉表連接
        table.close();
    }

    /** * TODO 獲取數據(get) * * @param tableName * @param rowKey * @param columnFamily * @param columnName * @throws IOException */
    public static void getData(String tableName, String rowKey, String columnFamily, String columnName) throws IOException {
        // 1.獲取表對象
        Table table = connection.getTable(TableName.valueOf(tableName));

        // 2.創建Get對象
        Get get = new Get(Bytes.toBytes(rowKey));

        // 2.1 指定獲取的列族
        //get.addFamily(Bytes.toBytes(columnFamily));

        // 2.2 指定列族和列
        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(columnName));

        // 2.3 設置獲取數據的版本數
        get.setMaxVersions(5);

        // 3.獲取數據
        Result result = table.get(get);

        // 4.解析result並打印
        for (Cell cell : result.rawCells()) {

            // 5.打印數據
            System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)) +
                    ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
                    ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
                    ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));

        }

        // 6.關閉表連接
        table.close();
    }

    /** * TODO 掃描全表數據 * * @param tableName * @throws IOException */
    public static void scanTable(String tableName) throws IOException {

        // 1.獲取表對象
        Table table = connection.getTable(TableName.valueOf(tableName));

        // 2.構建Scan對象
        Scan scan = new Scan(Bytes.toBytes("1001"), Bytes.toBytes("1003"));// 左閉右開

        // 3.掃描表
        ResultScanner resultScanner = table.getScanner(scan);

        // 4.解析resultScanner
        for (Result result : resultScanner) {

            // 5.解析result並打印
            for (Cell cell : result.rawCells()) {

                // 6.打印數據
                System.out.println("RowKey:" + Bytes.toString(CellUtil.cloneRow(cell)) +
                        ",CF:" + Bytes.toString(CellUtil.cloneFamily(cell)) +
                        ",CN:" + Bytes.toString(CellUtil.cloneQualifier(cell)) +
                        ",Value:" + Bytes.toString(CellUtil.cloneValue(cell)));
            }
        }

        // 7.關閉表連接
        table.close();
    }


    public static void deleteData(String tableName, String rowKey, String cf, String cn) throws IOException {

        // 1.獲取表對象
        Table table = connection.getTable(TableName.valueOf(tableName));

        // 2.創建刪除對象
        Delete delete = new Delete(Bytes.toBytes(rowKey));


        /* Delete標記: 刪除特定列列指定的版本 DeleteFamily標記: 刪除特定列族所有列 DeleteColumn標記: 刪除特定列的所有版本 指定rowkey: 使用DeleteFamily標記 ---->不加時間戳表示刪除[指定rowkey]的所有數據,加時間戳表示刪除[指定rowkey]中[時間戳版本小於或等於指定時間戳]的所有數據 指定rowkey+columnFamily: 使用DeleteFamily標記 ---->不加時間戳表示刪除[指定列族]的所有數據,加了時間戳就表示刪除[指定列族]下[時間戳版本小於或等於指定時間戳]的所有數據 指定rowkey+columnFamily+column(addColumns): 使用DeleteColumn標記 ---->不加時間戳表示刪除[指定列]所有版本的數據,加時間戳表示刪除[指定列]中[時間戳版本小於或等於指定時間戳]的所有數據。 指定rowkey+columnFamily+column(addColumn): 使用Delete標記 (只刪除單個版本數據,生產環境盡量別用) ---->不加時間戳表示刪除[指定列]中[最新版本]的數據,加時間戳表示刪除[指定列]中[指定時間戳版本]的數據。 ---->不推薦的原因是:操作不同(如flush前后操作產生的結果會不一樣)結果可能不同 如:在flush前如果有多個版本的數據,此時進行addColumn(不加時間戳)操作,會將最新版本的數據刪除,然后老版本的數據會出現 在flush后進行addColumn(不加時間戳)操作,會將最新版本的數據刪除,而此時flush已將老版本的數據進行了刪除,所有此時老版本的數據就不會出現了 */

        // 2.1 設置刪除的列
        // 刪除列最好使用addColumns
        // addColumns:不加時間戳表示刪除指定列所有版本的數據(推薦)
        // addColumns:加時間戳表示刪除時間戳小於或等於指定時間戳的指定列的所有版本。
        // addColumn:不加時間戳表示刪除最新版本的數據,操作不同(如flush前后操作產生的結果會不一樣)結果可能不同
        // addColumn:加時間戳表示刪除指定時間戳的指定列版本的數據。

        //delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn),1574158036021L);

        // 2.2 刪除指定的列族
        // addFamily:刪除指定列族的所有列的所有版本數據。
        delete.addFamily(Bytes.toBytes(cf));

        // 3.執行刪除操作
        table.delete(delete);

        // 4.關閉連接
        table.close();
    }

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

    public static void main(String[] args) throws IOException {

        // 1.測試表是否存在
        //System.out.println(isTableExist("student1"));

        // 2.創建表測試
        //createTable("wc:student1", "info1", "info2");

        // 3.刪除表測試
        //dropTable("student1");
        //System.out.println(isTableExist("student1"));

        // 4.創建命名空間測試
        //createNameSpace("wc");

        // 5.插入數據測試
        //putData("student", "1003", "info", "name", "John");

        // 6.獲取單行數據測試
        //getData("student", "1001", "info", "name");

        // 7.測試掃描數據
        //scanTable("student");

        // 8.刪除測試
        deleteData("student", "1005", "info", "name");

        // 關閉資源
        close();
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM