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();
    }
}

posted @ 2019-12-10 16:15  陈小哥cw  阅读(1507)  评论(0编辑  收藏  举报