package com.yuhui.gd.hadoop.hbase;


import java.util.Iterator;

import java.util.Map;

import java.util.Map.Entry;

import java.util.NavigableMap;

import java.util.Set;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.HColumnDescriptor;

import org.apache.hadoop.hbase.HTableDescriptor;

import org.apache.hadoop.hbase.client.Delete;

import org.apache.hadoop.hbase.client.Get;

import org.apache.hadoop.hbase.client.HBaseAdmin;

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.util.Bytes;


public class HBaseUtil {

    // 声明静态配置 HBaseConfiguration

    static Configuration config = HBaseConfiguration.create();

    static {

        config.set("hbase.zookeeper.quorum", "hd204,hd205,hd206");

    }


    // 创建一张表,通过HBaseAdmin HTableDecriptor来创建

    public static void create(String tableName, String[] columnFamilys)

    throws Exception {

        HBaseAdmin admin = new HBaseAdmin(config);


        if (admin.tableExists(tableName)) {

            System.err.println("表名为: " + tableName + " 已经存在!");

            System.exit(0);

        } else {

            HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);

            for (String columnFamily : columnFamilys) {

                HColumnDescriptor columnDescriptor = new HColumnDescriptor(

                columnFamily);

                tableDescriptor.addFamily(columnDescriptor);

            }

            admin.createTable(tableDescriptor);

            System.out.println("Create table Success!");

        }

    }


    // 添加一条数据,通过HTable Put为已经存在的表来添加数据

    public static void put(String tableName, String row, String columnFamily,

        String columnQualifer, String data) throws Exception {

        HTable table = new HTable(config, tableName);

        Put put = new Put(Bytes.toBytes(row));

        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(columnQualifer),

                Bytes.toBytes(data));

        table.put(put);

        System.out.println("put '" + row + "' , '" + columnFamily + ":"

        + columnQualifer + "','" + data + "'");

    }


    // 根据表名,行键值查询一行记录

    public static void get(String tableName, String row) throws Exception {

        HTable table = new HTable(config, tableName);

        Get get = new Get(Bytes.toBytes(row));

        Result result = table.get(get);

        NavigableMap<byte[], NavigableMap<byte[], byte[]>> map = result.getNoVersionMap();

        Set<Entry<byte[], NavigableMap<byte[], byte[]>>> set = map.entrySet();

        System.out.println("ROW\t\tCOLUMN:CELL");

        for (Entry<byte[], NavigableMap<byte[], byte[]>> entry : set) {

            String familyName = new String(entry.getKey());

            NavigableMap<byte[], byte[]> nmap = entry.getValue();

            Set<Entry<byte[], byte[]>> valueSet = nmap.entrySet();

            for (Entry<byte[], byte[]> column : valueSet) {

                String columnName = new String(column.getKey());

                String columnValue = new String(column.getValue());


                System.out.println(row + "\t\t" + familyName + ":" + columnName

                + "\t" + columnValue);

            }

        }

    }


    // 根据表名,查询全表(不包含版本信息---版本时间戳)

    public static void get(String tableName) throws Exception {

        HTable table = new HTable(config, tableName);

        ResultScanner scanner = table.getScanner(new Scan());

        Iterator<Result> itr = scanner.iterator();

        System.out.println("ROW\t\t\tCOLUMN:CELL");

        while (itr.hasNext()) {

            Result result = itr.next();

            String rowValue = new String(result.getRow());

            String tabCount = rowValue.length() >= 8 ? "\t" : "\t\t";

            NavigableMap<byte[], NavigableMap<byte[], byte[]>> map = result.getNoVersionMap();

            for (Entry<byte[], NavigableMap<byte[], byte[]>> entry : map.entrySet()) {

                String family = new String(entry.getKey());

                NavigableMap<byte[], byte[]> navigableMap = entry.getValue();

                Set<Map.Entry<byte[], byte[]>> set = navigableMap.entrySet();

                for (Entry<byte[], byte[]> en : set) {

                    System.out.println(rowValue + tabCount + "\t" + family

                    + ":" + new String(en.getKey()) + "\t" + new String(en.getValue()));

                }

            }

        }

    }


    // 根据表名,查询全表(包含版本信息---版本时间戳)

    public static void get(String tableName, boolean containsTimeStamp)

    throws Exception {

        if (!containsTimeStamp) {

            get(tableName);

        } else {

            HTable table = new HTable(config, tableName);

            Scan scan = new Scan();

            ResultScanner resultScanner = table.getScanner(scan);

            Iterator<Result> resultItr = resultScanner.iterator();

            System.out.println("ROW\t\t\tCOLUMN:CELL");

            while (resultItr.hasNext()) {

                Result result = resultItr.next();

                String rowValue = new String(result.getRow());

                String tabCount = rowValue.length() >= 8 ? "\t" : "\t\t";

                NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map                         = result.getMap();

                Set<Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>>>                         set = map.entrySet();

                for (Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry                     : set) {

                    String familyName = new String(entry.getKey());

                    NavigableMap<byte[], NavigableMap<Long, byte[]>> columnMap =                                     entry.getValue();

                    Set<Map.Entry<byte[], NavigableMap<Long, byte[]>>> columnSet =                                     columnMap.entrySet();

                    for (Entry<byte[], NavigableMap<Long, byte[]>> ent : columnSet) {

                        String columnName = new String(ent.getKey());

                        NavigableMap<Long, byte[]> valueMap = ent.getValue();

                        Set<Map.Entry<Long, byte[]>> valueSet = valueMap.entrySet();

                        for (Entry<Long, byte[]> e : valueSet) {

                            Long timeStamp = e.getKey();

                            String columnValue = new String(e.getValue());

                            System.out.println(rowValue + tabCount+ "\tcolumn=" +                                             familyName + ":"+ columnName + "\ttimestamp="

                                + String.valueOf(timeStamp) + "\tvalue="+ columnValue);

                        }

                    }

                }

            }

        }

    }


    // 删除表中某一行

    public static void deleteRow(String tableName, String row, String family,

Long timestamp) throws Exception {

        HTable table = new HTable(config, tableName);

        Delete delete = new Delete(Bytes.toBytes(row));

        if (null == timestamp) {

            table.delete(delete.deleteFamily(Bytes.toBytes(family)));

        } else {

            table.delete(delete.deleteFamily(Bytes.toBytes(family),timestamp.longValue()));

        }


    }


    // 删除表中某一行的某个字段

    public static void deleteColumn(String tableName, String row,

        String family, String column, Long timestamp) throws Exception {

        HTable table = new HTable(config, tableName);

        Delete delete = new Delete(Bytes.toBytes(row));

        if (null == timestamp) {

            table.delete(delete.deleteColumn(Bytes.toBytes(family),Bytes.toBytes(column)));

        } else {

            table.delete(delete.deleteColumn(Bytes.toBytes(family),

                            Bytes.toBytes(column), timestamp.longValue()));

        }

    }


    // 删除某个表

    public static boolean deleteTable(String tableName) throws Exception {

        HBaseAdmin admin = new HBaseAdmin(config);

        if (admin.tableExists(tableName)) {

            admin.disableTable(tableName);

            admin.deleteTable(tableName);

            return true;

        }

        return false;

    }


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

        get("heroes", "1");

    }


}