The API 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;
import java.util.ArrayList;


public class HbaseClients {
    public static  Connection connection;
    public static void main(String[] args) throws Exception {
        create("kgc","cf1","cf2","cf3");
         // Delete ( "KGC");
         // Scan ( "Test02"); 
    }
     // initialization parameters connection object 
    static {
         // the conf 
        the Configuration the conf = HBaseConfiguration.create (); 
        . The conf SET ( " HBase. zookeeper.quorum " , " 192.168.159.110 " ); 
        conf. the SET ( " hbase.zookeeper.property.clientPort " , " 2181 " );
         // get hbase linked object ConnectionFactory 
        the try {
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //创建表
    public static void create(String table,String ... familys) throws Exception {
        //首先要拿到admin
        Admin admin = connection.getAdmin();
        //new
        HTableDescriptor htable = new HTableDescriptor(TableName.valueOf(table));
        //htable
        for (String family : familys) {
            htable.addFamily(new new HColumnDescriptor (Family)); 
        } 
        admin.createTable (The htable); 
        . System OUT .println ( " successfully created " ); 
    } 
    // Delete table 
    public  static  void the Delete (String the Table) throws Exception { 
        Admin ADMIN = connection.getAdmin ( );
         // determines whether there is a table 
        iF (admin.tableExists (TableName.valueOf (table))) {
             // is down 
            admin.disableTable (TableName.valueOf (table));
             // delete 
             admin.deleteTable (TableName.valueOf (Table));
            . the System OUT .println ( "删除成功");
        }else{
            System.out.println("对不起,表不存在");
        }
    }
    //添加数据
    public static void adddata(String table,String rowkey,String columnFamily,String column,String value) throws Exception {
        Table table1 = connection.getTable(TableName.valueOf(table));
        //NEW  PUT
        Put puts = new Put(Bytes.toBytes(rowkey));
        //puts
        puts.add(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(value));
        table1.put(puts);
        System.out.println("添加数据成功");
    }
    //删除一行(rowkey)数据
    public static void delterow(String table,String  rowkey) throws Exception {
        Table table1 = connection.getTable(TableName.valueOf(table));
        //new Delete
        Delete deletes = new Delete(Bytes.toBytes(rowkey));
        //删除
        table1.delete(deletes);
        System.out.println ( " delete line data is successfully " ); 
    } 
    // delete multiple rows 
    public  static  void DeleteRows (Table String, String ... rowkeys) throws Exception { 
        the Table table1 = Connection.GetTable (TableName.valueOf (Table));
         // new new List 
        the ArrayList <the Delete> List = new new the ArrayList <> ();
         for (String RowKey: rowkeys) { 
            List.add ( new new the Delete (Bytes.toBytes (RowKey))); 
        } 
        table1.delete (List); 
        the System . OUT .println ( " delete multi-line success");
    }
    //scan扫描
    public static void scan(String table) throws Exception {
        Table table1 = connection.getTable(TableName.valueOf(table));
        //new Scan
        Scan scan = new Scan();
        //results 就是所有的rowkey的集合
        ResultScanner results = table1.getScanner(scan);
        for (Result result : results) {
            //cells 的集合
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.println(Bytes.toString(cell.getRow()));
                System.out.println(Bytes.toString(cell.getFamily()));
                System.out.println(Bytes.toString(cell.getQualifier()));
                System.out.println(Bytes.toString(cell.getValue()));
            }
        }
    }
    //get
    public static void get(String table,String rowkey) throws Exception {
        Table table1 = connection.getTable(TableName.valueOf(table));
        //new Get
        Get get = new Get(Bytes.toBytes(rowkey));
        Result result = table1.get(get);
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println(Bytes.toString(cell.getRow()));
            System.out.println(Bytes.toString(cell.getFamily()));
            System.out.println(Bytes.toString(cell.getQualifier()));
            System.out.println(Bytes.toString(cell.getValue()));
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/wangshuang123/p/10981008.html