zoukankan      html  css  js  c++  java
  • 熟悉常用的HBASE 操作

    package cn.edu.zucc.hbase; 
    import java.io.IOException; 
    import java.util.Scanner;
    
    import org.apache.hadoop.conf.Configuration; 
    import org.apache.hadoop.hbase.Cell;
    import org.apache.hadoop.hbase.CellUtil;
    import org.apache.hadoop.hbase.HBaseConfiguration;
    import org.apache.hadoop.hbase.HTableDescriptor; 
    import org.apache.hadoop.hbase.TableName;
    import org.apache.hadoop.hbase.client.Admin; 
    import org.apache.hadoop.hbase.client.Connection; 
    import org.apache.hadoop.hbase.client.ConnectionFactory;
    import org.apache.hadoop.hbase.client.Delete;
    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.client.Table;
    import org.apache.hadoop.hbase.util.Bytes;
    public class HbasePractice { 
        public static Configuration configuration;
        public static Connection connection; 
        public static Admin admin; 
        //列出所有的表
        public static void listTables() throws IOException 
        { 
            
            HTableDescriptor[] hTableDescriptors = admin.listTables(); 
            for (HTableDescriptor hTableDescriptor : hTableDescriptors) 
            { 
                System.out.println("表名:" + hTableDescriptor.getNameAsString()); 
            }
        }
        //在终端打印出指定的表的所有记录数据
        public static void getData(String tableName) throws IOException 
        { 
            init(); 
            Table table = connection.getTable(TableName.valueOf(tableName)); 
            Scan scan = new Scan(); 
            ResultScanner scanner = table.getScanner(scan); 
            for (Result result : scanner) 
            { 
                printRecoder(result); 
             }
        }
    
        public static void printRecoder(Result result) throws IOException 
        { 
            for (Cell cell : result.rawCells()) 
            { 
                System.out.print("行健: " + new String(CellUtil.cloneRow(cell))+"  "); 
                System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell))+"  "); 
                System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell))+"  "); 
                System.out.print(" 值: " + new String(CellUtil.cloneValue(cell))+"  "); 
                System.out.println("时间戳: " + cell.getTimestamp()); } }
    
        //向已经创建好的表添加的列族或列
        public static void insertRow(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(); 
            
        } 
        
        //向已经创建好的表删除指定的列族或列
        public static void deleteRow(String tableName, String rowKey, String colFamily, String col) throws IOException 
        { 
            
            Table table = connection.getTable(TableName.valueOf(tableName)); 
            Delete delete = new Delete(rowKey.getBytes()); 
            delete.addFamily(Bytes.toBytes(colFamily));
            delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col)); 
            table.delete(delete); 
            table.close();
            
        }
        //清空指定的表的所有记录数据
        public static void clearRows(String tableName) throws IOException 
        { 
            
            TableName tablename = TableName.valueOf(tableName); 
            admin.disableTable(tablename); 
            admin.truncateTable(tablename, false); 
            
        }
        //统计表的行数
        public static void countTable(String tableName) throws IOException 
        { 
            
            Table table = connection.getTable(TableName.valueOf(tableName)); 
            Scan scan = new Scan(); 
            ResultScanner scanner = table.getScanner(scan); 
            int num = 0; 
            for (Result result = scanner.next(); result != null; result = scanner .next()) 
            { 
                num++; 
            } 
            System.out.println("行数:" + num); 
            scanner.close(); 
        }
    
    
        //初始化
            public static void init() 
            { 
                configuration = HBaseConfiguration.create();
                configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); 
                try
                { 
                    connection = ConnectionFactory.createConnection(configuration); 
                    admin = connection.getAdmin(); 
                } 
                catch (IOException e) 
                { 
                    e.printStackTrace();    
                } 
                
            } 
        //关闭hbase
        public static void close() 
        { 
            try 
            { 
                if (admin != null) 
                { 
                    admin.close();     
                } 
                if (connection != null) 
                { 
                    connection.close();     
                } 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            
        } 
        //主函数
        public static void main(String[] args) 
        {  
            try 
            {  
                int n=0;
                init(); 
                Scanner in = new Scanner(System.in);
                while(n!=7)
                {
                    System.out.println("1.列出 Hbase 所有的表的信息");
                    System.out.println("2.在终端打印出指定表的所有记录数据");
                    System.out.println("3.向已创建好的表添加指定的列族列");
                    System.out.println("4.向已创建好的表删除指定的列族列");
                    System.out.println("5.清空指定的表的所有记录数据");
                    System.out.println("6.统计表的行数");
                    System.out.println("7.退出");
                    System.out.println("请选择:");
                    if(in.hasNextInt())
                    {
                        n=in.nextInt();
                    }
                    else
                    {
                        System.out.println("输入的不是整数,请重新输入:");
                        continue;
                    }
                    switch(n)
                    {
                    case 1:listTables(); break;
                    case 2:getData("student");break;
                    case 3:insertRow("student", "00000000", "score", "math", "99"); break;
                    case 4:deleteRow("student", "00000000", "score", "math");  break;
                    case 5:clearRows("student"); break;
                    case 6:countTable("student"); break;
                    case 7:break;
                    default:System.out.println("输入错误,请重新输入");
                    }
                }
                close();
            } 
            catch (IOException e)
            { 
                e.printStackTrace(); 
            } 
        }
        
    }
  • 相关阅读:
    HDU5873
    HDU5874
    HDU1565(状态压缩dp)
    POJ2774(二分+哈希)
    HDU4474
    HDU2602(背包)
    单链表
    POJ2503(hash)
    POJ1200(hash)
    顺序表
  • 原文地址:https://www.cnblogs.com/wxd136/p/9787852.html
Copyright © 2011-2022 走看看