美文网首页
HBase(1.1.2版本)API

HBase(1.1.2版本)API

作者: 建康_木子 | 来源:发表于2019-07-05 10:48 被阅读0次
    package inspur.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 org.apache.hadoop.security.UserGroupInformation;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @ClassName HBaseTest
     * @Desc 1.1.2版本HBase测试
     * @Author lijk
     * @Date 2019/7/416:33
     * @Version 1.0
     */
    public class HBaseTest {
        Configuration configuration=null;
        Connection connection = null;
        Admin admin = null;
        public HBaseTest() { }
    
        /**
         *@Desc /初始化配置
         *@Param
         *@return
         */
        private void init(){
            this.configuration = HBaseConfiguration.create();
            try {
                this.connection = ConnectionFactory.createConnection(this.configuration);
                this.admin = connection.getAdmin();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        /**
         *@Desc 关闭资源
         *@Param
         *@return
         */
        private void close(){
            if (this.admin!=null){
                try {
                    admin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
       *@Desc 建表
       *@Param family:列族 tableName:表名
       *@return
       */
        private  void createTable(List<String> families, String tabledName){
            init();
            TableName talbe = TableName.valueOf(tabledName);
            try {
                if (admin.tableExists(talbe)){
                    System.out.println("table: "+tabledName+" is exists!");
                }else {
                    HTableDescriptor tableDescriptor = new HTableDescriptor(talbe);
                    for (String family : families) {
                        HColumnDescriptor columnDescriptor = new HColumnDescriptor(family);
                        tableDescriptor.addFamily(columnDescriptor);
                    }
                    admin.createTable(tableDescriptor);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                close();
            }
        }
        
        /**
         *@Desc //删除表格
         *@Param 
         *@return 
         */
        private void deleteTable(String tableName){
            init();
            TableName table = TableName.valueOf(tableName);
            try {
                if (admin.tableExists(table)){
                    admin.disableTable(table);
                    admin.deleteTable(table);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //查看已有的表格
         *@Param
         *@return 
         */
        private void listTables(){
            init();
            try {
                HTableDescriptor[] listTables = admin.listTables();
                for (HTableDescriptor listTable : listTables) {
                    System.out.println(listTable.getNameAsString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //插入数据
         *@Param
         *@return
         */
        private void insertRow(String tableName,String rowkey,String family,String col,String value){
            init();
            Table table=null;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Put put = new Put(Bytes.toBytes(rowkey));
            put.addColumn(Bytes.toBytes(family),Bytes.toBytes(col),Bytes.toBytes(value));
            try {
                table.put(put);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //批量插入
         *@Param
         *@return
         */
        private void insertRow(String tableName,List<CellTest> cells){
            init();
            Table table ;
            List<Put> puts = new ArrayList<>();
            try {
                table = connection.getTable(TableName.valueOf(tableName));
                for (CellTest cell : cells) {
                    Put put = new Put(Bytes.toBytes(cell.rowkey));
                    put.addColumn(Bytes.toBytes(cell.colFamily),Bytes.toBytes(cell.getCol()),Bytes.toBytes(cell.getValue()));
                    puts.add(put);
                }
                table.put(puts);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        private static class CellTest {
            private String rowkey;
            private String colFamily;
            private String col;
            private String value;
    
            private CellTest(String rowkey, String colFamily, String col, String value) {
                this.rowkey = rowkey;
                this.colFamily = colFamily;
                this.col = col;
                this.value = value;
            }
    
            private CellTest() {
            }
    
            private String getRowkey() {
                return rowkey;
            }
    
            private void setRowkey(String rowkey) {
                this.rowkey = rowkey;
            }
    
            private String getColFamily() {
                return colFamily;
            }
    
            private void setColFamily(String colFamily) {
                this.colFamily = colFamily;
            }
    
            private String getCol() {
                return col;
            }
    
            private void setCol(String col) {
                this.col = col;
            }
    
            private String getValue() {
                return value;
            }
    
            private void setValue(String value) {
                this.value = value;
            }
        }
        /**
         *@Desc //删除数据
         *@Param
         *@return
         */
        private void deleteRow(String tableName,String rowkey,String family,String col){
            init();
            Table table = null;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Delete delete = new Delete(Bytes.toBytes(rowkey));
            //删除指定列族
    //        delete.addFamily(Bytes.toBytes(family));
            //删除指定列
    //        delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(col));
            try {
                table.delete(delete);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //批量删除
    //        List<Delete> deleteList = new ArrayList<Delete>();
    //        deleteList.add(delete);
    //        table.delete(deleteList);*/
            close();
        }
        /**
         *@Desc //根据rowkey查找数据
         *@Param
         *@return
         */
        private void selectByO(String tableName, String rowkey){
            init();
            Table table =null;
            Result result = null;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowkey));
                result = table.get(get);
                formatPrint(result);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //获取指定列族数据
         *@Param
         *@return
         */
        private void selectByO(String tableName,String rowkey,String family){
            init();
            Table table;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowkey));
                get.addFamily(Bytes.toBytes(family));
                Result result = table.get(get);
                formatPrint(result);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //获取指定列数据
         *@Param
         *@return
         */
        private  void selectByO(String tableName, String rowkey, String family, String col){
            init();
            Table table;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
                Get get = new Get(Bytes.toBytes(rowkey));
                get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col));
                Result result = table.get(get);
                formatPrint(result);
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //格式化输出
         *@Param
         *@return
         */
        private void formatPrint(Result result){
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
                System.out.println("TimeStamp+"+cell.getTimestamp()+" ");
                System.out.println("ColumnFamil:"+new String(CellUtil.cloneFamily(cell))+" ");
                System.out.println("colName:"+new String(CellUtil.cloneQualifier(cell))+" ");
                System.out.println("value:"+ new String(CellUtil.cloneValue(cell))+" ");
            }
        }
        /**
         *@Desc //批量查找数据
         *@Param
         *@return
         */
        private void scanData(String tableName){
            init();
            Table table = null;
            ResultScanner scanner = null;
            Scan scan = null;
            try {
                table = connection.getTable(TableName.valueOf(tableName));
                scan = new Scan();
                scanner = table.getScanner(scan);
                for (Result result : scanner) {
                    formatPrint(result);
                }
                table.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        /**
         *@Desc //Kerberos认证,windows需要和kdc时间同步
         *@Param
         *@return
         */
        private void login(){
            String krb5Path = "E:\\project\\unsafeTest\\conf\\krb5.conf";
            String keytabPath = "E:\\project\\unsafeTest\\conf\\hbase.headless.keytab";
            System.setProperty("java.security.krb5.conf","E:\\project\\unsafeTest\\src\\conf\\krb5.conf");
            Configuration entries = new Configuration();
            entries.addResource("core-site.xml");
            entries.addResource("hbase-site.xml");
            entries.addResource("hdfs-site.xml");
            UserGroupInformation.setConfiguration(entries);
            try {
                UserGroupInformation.loginUserFromKeytab("hbase-test","E:\\project\\unsafeTest\\src\\conf\\hbase.headless.keytab");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) throws IOException {
            HBaseTest test = new HBaseTest();
    //        Kerberos认证,一定要时间同步
            test.login();
    //        test.createTable(Arrays.asList("f1","f2"),"hbasetest1");
    //        test.listTables();
    //        test.insertRow("hbasetest1","row1","f1","col1","value1");
    //        test.selectByO("hbasetest1","row1","f1","col1");
    //        List<CellTest> cells = new ArrayList<>();
    //        cells.add(new CellTest("row2","f2","col2","value2"));
    //        cells.add(new CellTest("row3","f1","col3","value3"));
    //        cells.add(new CellTest("row4","f2","col4","value4"));
    //        test.insertRow("hbasetest1",cells);
            test.scanData("hbasetest1");
    //        test.selectByO("hbasetest1","row1");
    //        test.deleteTable("hbasetest1");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:HBase(1.1.2版本)API

          本文链接:https://www.haomeiwen.com/subject/gadshctx.html