1. 程式人生 > >HBase的java操作,最新API。(查詢指定行、列、插入資料等)

HBase的java操作,最新API。(查詢指定行、列、插入資料等)

關於HBase環境搭建和HBase的原理架構,請見筆者相關部落格。

1.HBase對java有著較優秀的支援,本文將介紹如何使用java操作Hbase。

首先是pom依賴:

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-server</artifactId>
    <version>1.3.1</version>
</dependency>
    <dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-common</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

2.操作HBase的關鍵類:

public class HbaseDemo {
 
    private static Admin admin;
 
    public static void main(String[] args){
        try {
              createTable("user_table", new String[] { "information", "contact" });
              User user = new User("001", "xiaoming", "123456", "man", "20", "13355550021", "
[email protected]
"); insertData("user_table", user); User user2 = new User("002", "xiaohong", "654321", "female", "18", "18757912212", "[email protected]"); insertData("user_table", user2); List<User> list = getAllData("user_table"); System.out.println("--------------------插入兩條資料後--------------------"); for (User user3 : list){ System.out.println(user3.toString()); } System.out.println("--------------------獲取原始資料-----------------------"); getNoDealData("user_table"); System.out.println("--------------------根據rowKey查詢--------------------"); User user4 = getDataByRowKey("user_table", "user-001"); System.out.println(user4.toString()); System.out.println("--------------------獲取指定單條資料-------------------"); String user_phone = getCellData("user_table", "user-001", "contact", "phone"); System.out.println(user_phone); User user5 = new User("test-003", "xiaoguang", "789012", "man", "22", "12312132214", "
[email protected]
"); insertData("user_table", user5); List<User> list2 = getAllData("user_table"); System.out.println("--------------------插入測試資料後--------------------"); for (User user6 : list2){ System.out.println(user6.toString()); } deleteByRowKey("user_table", "user-test-003"); List<User> list3 = getAllData("user_table"); System.out.println("--------------------刪除測試資料後--------------------"); for (User user7 : list3){ System.out.println(user7.toString()); } } catch (Exception e) { e.printStackTrace(); } } //連線叢集 public static Connection initHbase() throws IOException { Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.property.clientPort", "2181"); configuration.set("hbase.zookeeper.quorum", "127.0.0.1"); //叢集配置↓ //configuration.set("hbase.zookeeper.quorum", "101.236.39.141,101.236.46.114,101.236.46.113"); configuration.set("hbase.master", "127.0.0.1:60000"); Connection connection = ConnectionFactory.createConnection(configuration); return connection; } //建立表 public static void createTable(String tableNmae, String[] cols) throws IOException { TableName tableName = TableName.valueOf(tableNmae); admin = initHbase().getAdmin(); if (admin.tableExists(tableName)) { System.out.println("表已存在!"); } else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for (String col : cols) { HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); } } //插入資料 public static void insertData(String tableName, User user) throws IOException { TableName tablename = TableName.valueOf(tableName); Put put = new Put(("user-" + user.getId()).getBytes()); //引數:1.列族名 2.列名 3.值 put.addColumn("information".getBytes(), "username".getBytes(), user.getUsername().getBytes()) ; put.addColumn("information".getBytes(), "age".getBytes(), user.getAge().getBytes()) ; put.addColumn("information".getBytes(), "gender".getBytes(), user.getGender().getBytes()) ; put.addColumn("contact".getBytes(), "phone".getBytes(), user.getPhone().getBytes()); put.addColumn("contact".getBytes(), "email".getBytes(), user.getEmail().getBytes()); //HTable table = new HTable(initHbase().getConfiguration(),tablename);已棄用 Table table = initHbase().getTable(tablename); table.put(put); } //獲取原始資料 public static void getNoDealData(String tableName){ try { Table table= initHbase().getTable(TableName.valueOf(tableName)); Scan scan = new Scan(); ResultScanner resutScanner = table.getScanner(scan); for(Result result: resutScanner){ System.out.println("scan: " + result); } } catch (IOException e) { e.printStackTrace(); } } //根據rowKey進行查詢 public static User getDataByRowKey(String tableName, String rowKey) throws IOException { Table table = initHbase().getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); User user = new User(); user.setId(rowKey); //先判斷是否有此條資料 if(!get.isCheckExistenceOnly()){ Result result = table.get(get); for (Cell cell : result.rawCells()){ String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); if(colName.equals("username")){ user.setUsername(value); } if(colName.equals("age")){ user.setAge(value); } if (colName.equals("gender")){ user.setGender(value); } if (colName.equals("phone")){ user.setPhone(value); } if (colName.equals("email")){ user.setEmail(value); } } } return user; } //查詢指定單cell內容 public static String getCellData(String tableName, String rowKey, String family, String col){ try { Table table = initHbase().getTable(TableName.valueOf(tableName)); String result = null; Get get = new Get(rowKey.getBytes()); if(!get.isCheckExistenceOnly()){ get.addColumn(Bytes.toBytes(family),Bytes.toBytes(col)); Result res = table.get(get); byte[] resByte = res.getValue(Bytes.toBytes(family), Bytes.toBytes(col)); return result = Bytes.toString(resByte); }else{ return result = "查詢結果不存在"; } } catch (IOException e) { e.printStackTrace(); } return "出現異常"; } //查詢指定表名中所有的資料 public static List<User> getAllData(String tableName){ Table table = null; List<User> list = new ArrayList<User>(); try { table = initHbase().getTable(TableName.valueOf(tableName)); ResultScanner results = table.getScanner(new Scan()); User user = null; for (Result result : results){ String id = new String(result.getRow()); System.out.println("使用者名稱:" + new String(result.getRow())); user = new User(); for(Cell cell : result.rawCells()){ String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()); //String family = Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength()); String colName = Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()); String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()); user.setId(row); if(colName.equals("username")){ user.setUsername(value); } if(colName.equals("age")){ user.setAge(value); } if (colName.equals("gender")){ user.setGender(value); } if (colName.equals("phone")){ user.setPhone(value); } if (colName.equals("email")){ user.setEmail(value); } } list.add(user); } } catch (IOException e) { e.printStackTrace(); } return list; } //刪除指定cell資料 public static void deleteByRowKey(String tableName, String rowKey) throws IOException { Table table = initHbase().getTable(TableName.valueOf(tableName)); Delete delete = new Delete(Bytes.toBytes(rowKey)); //刪除指定列 //delete.addColumns(Bytes.toBytes("contact"), Bytes.toBytes("email")); table.delete(delete); } //刪除表 public static void deleteTable(String tableName){ try { TableName tablename = TableName.valueOf(tableName); admin = initHbase().getAdmin(); admin.disableTable(tablename); admin.deleteTable(tablename); } catch (IOException e) { e.printStackTrace(); } } }

3.本文以操作User例項為例,新手看下User類:

public class User {
 
    private String id;
    private String username;
    private String password;
    private String gender;
    private String age;
    private String phone;
    private String email;
 
    public User(String id, String username, String password, String gender, String age, String phone, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.gender = gender;
        this.age = age;
        this.phone = phone;
        this.email = email;
    }
 
    public User(){
 
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getGender() {
        return gender;
    }
 
    public void setGender(String gender) {
        this.gender = gender;
    }
 
    public String getAge() {
        return age;
    }
 
    public void setAge(String age) {
        this.age = age;
    }
 
    public String getPhone() {
        return phone;
    }
 
    public void setPhone(String phone) {
        this.phone = phone;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender='" + gender + '\'' +
                ", age='" + age + '\'' +
                ", phone='" + phone + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

4.測試結果如下圖: 在這裡插入圖片描述

java操作HBase成功.

(親自測試,非常不錯hbase操作案例!!!,也是最新的Hbase API)