HBase database design RowKey

Level 1: Financial RowKey Design

Knowledge points

1.RowKey design principles: uniqueness principle, sorting principle, length principle (the shorter the better), hashing principle

Programming requirements

Follow the prompts and add code in the editor on the right to complete the following requirements:

  • Design RowKey

  • Complete query of a seller’s transaction records within a certain period of time

package step1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {


    /**
     * Generate RowKey
     *
     * @param sellerId seller ID
     * @param timestamp timestamp
     * @param orderId order ID
     * @return RowKey
     */
    public String createRowKey(String sellerId, String timestamp, String orderId) {
        /************begin************/
        String rowKey=sellerId + "-" + timestamp + "-" + orderId;
        return rowKey;
        /********** end **********/
    }


    /**
     * Query the transaction records of a seller within a certain period of time
     *
     * @param sellerId seller ID
     * @param startTimestamp start timestamp
     * @param endTimestamp expiration timestamp
     * @return map storage (rowkey,value)
     */
    public Map<String, String> findLogByTimestampRange(String sellerId, String startTimestamp, String endTimestamp) throws Exception {
        Map<String, String> map = new HashMap<>();
        /************begin************/
        Configuration conf=new Configuration();
        conf.set("hbase.zookeeper.quorum","127.0.0.1:2181");
        Connection connection=ConnectionFactory.createConnection(conf);
        Scan scan=new Scan();

        String startRow=sellerId + "-" + startTimestamp;
        String stopRow=sellerId + "-" + (endTimestamp + 1);

        scan.withStartRow(startRow.getBytes());
        scan.withStopRow(stopRow.getBytes());
        Table table=connection.getTable(TableName.valueOf("deal"));
        ResultScanner scanner=table.getScanner(scan);
        for (Result result:scanner){
            String key = Bytes.toString(result.getRow());
            List<Cell> cells = result.listCells();
            for(Cell c:cells){
                String value = Bytes.toString(CellUtil.cloneValue(c));
                map.put(key, value);
            }
        }
  
        /********** end ************/
        return map;
    }


}

Level 2: Internet of Vehicles RowKey Design

Knowledge points

1. Ways to avoid hot spots

(1) Pre-partitioning

(2) Add salt: Assign a random prefix to RowKey so that it is different from the beginning of the previous RowKey

(3) Hash: Make the same line always salted with a prefix

(4) Reverse: Reverse the fixed-length or numeric format RowKey so that the frequently changing part of the RowKey is placed in front

(5) Timestamp reversal

Programming requirements

Follow the prompts and add code in the editor on the right to complete the following requirements:

  • Use hash to avoid hot spots –> Prefix: prefix = substr(md5(uid),0,5)
  • Query the transaction records of a certain vehicle within a certain time range
package step2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Task {

    static String[] chars = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a" , "b", "c", "d", "e", "f"};

    /**
* MD5 encryption
     * @param str The text that needs to be encrypted
     * @return encrypted content
     */
    public static String StringInMd5(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("md5");
            byte[] result = md5.digest(str.getBytes());
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < result.length; i + + ) {
                byte x = result[i];
                int h = 0x0f & amp; (x >>> 4);
                int l = 0x0f & x;
                sb.append(chars[h]).append(chars[l]);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Generate row
     *
     * @param carId car ID
     * @param timestamp timestamp
     * @return rowkey
     */
    public String createRowKey(String carId, String timestamp) {
        /************begin************/
        String prefix = StringInMd5(carId);
        String rowKey = prefix.substring(0, 5) + "-" + carId + "-" + timestamp;
        return rowKey;
        /********** end **********/
    }

    /**
     * Query the transaction records of a certain vehicle within a certain time range
     *
     * @param carId vehicle ID
     * @param startTimestamp start timestamp
     * @param endTimestamp expiration timestamp
     * @return map storage (rowkey,value)
     */
    public Map<String, String> findLogByTimestampRange(String carId, String startTimestamp, String endTimestamp) throws Exception {
        Map<String, String> map = new HashMap<>();
        /************begin************/
        Configuration conf = new Configuration();
        conf.set("hbase.zookeeper.quorum", "127.0.0.1:2181");
        Connection connection = ConnectionFactory.createConnection(conf);
        Scan scan = new Scan();
        String startRow = createRowKey(carId, startTimestamp);
        String stopRow = createRowKey(carId, endTimestamp + 1);
        scan.withStartRow(startRow.getBytes());
        scan.withStopRow(stopRow.getBytes());
        Table table = connection.getTable(TableName.valueOf("deal"));
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            String key = Bytes.toString(result.getRow());
            List<Cell> cells = result.listCells();
            for (Cell c : cells) {
                String value = Bytes.toString(CellUtil.cloneValue(c));
                map.put(key, value);
            }
        }
        /********** end **********/
        return map;
    }


}

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. MySQL entry skill treeDesign optimizationAnti-paradigm design 78103 people are learning the system