HBase Java API development: batch operations

Level 1: Obtain data in batches

Knowledge points

1.table.get(gets) will return a Result[] result array, which stores all the data of this query. You can use this array to traverse what we need data.

2.result.rawCells(), result is a single result. All the data in a row is stored here. rowCells( of result ) method will return the set of all columns (Cell) of this row.

3. The Cell object is a single column. To obtain the value in the column, you can use the CellUtil.cloneXXX() method, such as cloneValue(cell) The value of this column will be returned.

Programming requirements

Write a program to obtain the data with row keys 2018 and 2020 in the table and output it. The table step1_student is as follows:

package step1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.Get;
import org.apache.hadoop.hbase.client.HTable;
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.client.TableDescriptor;
import org.apache.hadoop.hbase.generated.rest.rest_jsp;
import org.apache.hadoop.hbase.util.Bytes;

public class Task {

public void batchGet() throws Exception {
/********* Begin *********/
        Configuration config = HBaseConfiguration.create();
        Connection Connection = ConnectionFactory.createConnection(config);
        List<String> rows=new ArrayList<>();
        rows.add("2018");
        rows.add("2020");
        TableName tableName = TableName.valueOf(Bytes.toBytes("step1_student"));
        Table table = Connection.getTable(tableName);
        getData(table,rows);
/********* End *********/
}
    public List<String> getData(Table table, List<String> rows) throws Exception {
    List<Get> gets = new ArrayList<>();
    for (String str : rows) {
        Get get = new Get(Bytes.toBytes(str));
        gets.add(get);
    }
    List<String> values = new ArrayList<>();
    Result[] results = table.get(gets);
    for (Result result : results) {
        System.out.println("Row:" + Bytes.toString(result.getRow()));
        for (Cell kv : result.rawCells()) {
            String family = Bytes.toString(CellUtil.cloneFamily(kv));
            String qualifire = Bytes.toString(CellUtil.cloneQualifier(kv));
            String value = Bytes.toString(CellUtil.cloneValue(kv));
            values.add(value);
            System.out.println(family + ":" + qualifire + "\t" + value);
        }
    }
    return values;
}
}

Level 2: Deleting data in batches

Knowledge points

1. Delete a single row of data

Table table = conn.getTable(tableName); //Get the table
byte[] row = Bytes.toBytes("row1");//Define row
Delete delete = new Delete(row);//Create delete object
table.delete(delete);//Delete

2. Delete multiple rows of data

Table table = conn.getTable(tableName);
List<Delete> deletes = new ArrayList<>();
for(int i = 1; i < 5;i + + ){
    byte[] row = Bytes.toBytes("row" + i);
    Delete delete = new Delete(row);
    deletes.add(delete);
}
table.delete(deletes);

Programming requirements

There is an existing table step2_table. Please write a program to delete the table row keys from row1 to row5 and row7 to row10 data.

package step2;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.TableDescriptors;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

public class Task {

public void batchDelete()throws Exception{
/********* Begin *********/
Configuration conf = HBaseConfiguration.create();
 
        Connection conn = ConnectionFactory.createConnection(conf);
 
        TableName tableName = TableName.valueOf("step2_table");
        Table table = conn.getTable(tableName);
        List<String> rows1 = new ArrayList();
        for(int i = 1; i<6;i + + ){
            String row = "row" + i;
            rows1.add(row);
            }
        delete(table,rows1);
        List<String> rows2 = new ArrayList<>();
        for(int i = 7;i<11;i + + ){
            String row = "row" + i;
            rows2.add(row);
            }
 
        delete(table,rows2);
\t\t
/********* End *********/
}
    public void delete(Table table,List<String> rows)throws IOException{
        List<Delete> deletes = new ArrayList<>();
        for(String str : rows){
            byte[] row = Bytes.toBytes(str);
            Delete delete = new Delete(row);
            deletes.add(delete);
            }
        table.delete(deletes);
        }
}

Level 3: Batch import data to HBase

Programming requirements

Write Java code to add data to the stu table of HBase (the table needs to be created by yourself) as follows:

Table name Row key Column family: column Value
stu 20181122 basic_info:name Archimonde
stu 20181122 basic_info:gender male
stu 20181122 basic_info:birthday 1987-05-23
stu 20181122 basic_info:connect tel:13974036666
stu 20181122 basic_info:address HuNan-ChangSha
stu 20181122 school_info:college ChengXing
stu 20181122 school_info:class class 1 grade 2
stu 20181122 school_info:object Software
stu 20181123 basic_info:name Sargeras
stu 20181123 basic_info:gender male
stu 20181123 basic_info:birthday 1986-05-23
stu 20181123 basic_info:connect tel:18774036666
stu 20181123 basic_info:address HuNan-ChangSha
stu 20181123 school_info:college ChengXing
stu 20181123 school_info:class class 2 grade 2
stu 20181123 school_info:object Software
package step3;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.TableDescriptors;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
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.client.TableDescriptor;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
import org.apache.hadoop.hbase.util.Bytes;

public class Task {

public void batchPut()throws Exception{
/********* Begin *********/
Configuration config=new Configuration();
        Connection conn = ConnectionFactory.createConnection(config);
        Admin admin = conn.getAdmin();
        //Create table
        TableName tableName = TableName.valueOf(Bytes.toBytes("stu"));
        TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
        ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("basic_info")).build();
ColumnFamilyDescriptor family2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("school_info")).build();
        builder.setColumnFamily(family);
        builder.setColumnFamily(family2);
        admin.createTable(builder.build());
        List<Put> puts = new ArrayList<>();
        String[] rows = {"20181122","20181123"};
        String[][] basic_infos = {<!-- -->{"Archimund","male","1987-05-23","tel:13974036666",\ "HUNan-ChangSha"},{"Sageras","male","1986-05-23","tel:18774036666","HUNan-ChangSha"}} ;
        String[] basic_colums = {"name","gender","birthday","connect","address"};
        String[][] school_infos = {<!-- -->{"ChengXing","class 1 grade 2","Software"},{"ChengXing","class 2 grade 2","Software"}};
        String[] school_colums = {"college","class","object"};
        for (int x=0;x<rows.length;x + + ){
            // Loop to add data
            Put put = new Put(Bytes.toBytes(rows[x]));
            for (int i=0;i<basic_infos.length;i + + ){
                byte[] columFamily = Bytes.toBytes("basic_info");
                byte[] qualifier = Bytes.toBytes(basic_colums[i]);
                byte[] value = Bytes.toBytes(basic_infos[x][i]);
                put.addColumn(columFamily, qualifier, value);
            }
            for (int i=0;i<school_infos.length;i + + ){
                byte[] columFamily = Bytes.toBytes("school_info");
                byte[] qualifier = Bytes.toBytes(school_colums[i]);
                byte[] value = Bytes.toBytes(school_infos[x][i]);
                put.addColumn(columFamily, qualifier, value);
            }
            puts.add(put);
        }
        Table table = conn.getTable(tableName);
        table.put(puts);
/********* End *********/
}
}