HBase: Implement Scan to query all, get to query a row or a column of data according to rowkey

The overall query process:
1. The client accesses zk, searches the -ROOT-table, and obtains the .META. table information
2. Searches from the .META. table to obtain the region information where the data is stored (find the region server)
3. Finally, obtain the searched data through the RegionServer Data


Understand

the structure of ROOT table and META table: META table contains region information (one row), region information includes: region name (corresponding to row_key), and region server (which server manages this region) and other information

META is stored There is too much region information, which may be too large and will be divided into multiple regions. Then, how to manage the regions in the META table? Let the ROOT table manage it. The ROOT table has the same structure, but only manages the region information of the META table. Of course, the ROOT table cannot be very large, and no other tables are needed to manage

it That is, the META table manages the region information of the actually created table, and the ROOT table manages the region information of the META table.
So first client--->root---->meta--->region sever this order.

Let's see what

the read and write operations of RegionServer data are all performed on RegionServer. On the one hand, RegionServer maintains the state of the region, provides management and services for the region (add, delete, modify, check, etc.), on the one hand, it exchanges with the Master and uploads the region load information, participate in the distributed coordination management of the Master (that is, load balancing)
Finally, the search has to be done through RegionServer. Here, the whole process of client--->root---->meta--->region sever----->getting data is completed.
In fact, when querying data, the menstore will be checked first, the block cache will not be checked, and the hfile will be checked last.

You can take a look at the HBase-Region detailed explanation

https://blog.csdn.net/wypersist/article/details/79820715


Code:

Scan queries all, get queries a row or column of data based on rowkey

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

/***
 * Query data from the table student
 * query all
 * Query a row based on the primary key
 * Query a column family/a column based on the primary key and column family
 *
 */
public class ShowRecord {
 
 private static Configuration hbaseconfig = null;
 static{
  Configuration conf = new Configuration();
  conf.set("hbase.zookeeper.quorum","*.*.*.*");
  conf.set("hbase.zookeeper.property.clientPort","2181");
  
  hbaseconfig = HBaseConfiguration.create(conf);
 }
 
 public static void main(String[] args) {
 //Note that field queries are case sensitive

  /***
   * query table student
   */
  // query all
  //ShowRecord.showAllRecords("student");
  
  //Query a row of data based on the primary key rowKey
  //ShowRecord.showOneRecordByRowKey("student", "200977100709");
  
  //Query a column in a row based on the primary key
  //ShowRecord.showOneRecordByRowKey_cloumn("student", "200977100709","name");
  //ShowRecord.showOneRecordByRowKey_cloumn("student", "200977100709","info:age");

 }
 /****
  * Use scan to query all data
  * @param tableName
  */
 public static void showAllRecords(String tableName)
 {
  System.out.println("start==============show All Records=============");
  
  HTablePool pool = new HTablePool(hbaseconfig,1000);
  //create table object
  HTable table = (HTable) pool.getTable(tableName);
  
  try {
   //Scan all data
   Scan scan = new Scan();
   ResultScanner rss = table.getScanner(scan);
   
   for(Result r:rss){
    System.out.println("\n row: "+new String(r.getRow()));
    
    for(KeyValue kv:r.raw()){
     
     System.out.println("family=>"+new String(kv.getFamily(),"utf-8")
           +"  value=>"+new String(kv.getValue(),"utf-8")
     +"  qualifer=>"+new String(kv.getQualifier(),"utf-8")
     +"  timestamp=>"+kv.getTimestamp());    
    }
   }
   rss.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace ();
  }  
  System.out.println("\n end==============show All Records=============");
 }
 
 /***
  * Query a row of data based on the primary key rowKey
  * get 'student','010'
  */
 public static void showOneRecordByRowKey(String tableName,String rowkey)
 {
  HTablePool pool = new HTablePool(hbaseconfig,1000);
  HTable table = (HTable) pool.getTable(tableName);
    
  try {
   Get get = new Get(rowkey.getBytes()); //Query according to the primary key
   Result r = table.get(get);
   System.out.println("start===showOneRecordByRowKey==row: "+"\n");
   System.out.println("row: "+new String(r.getRow(),"utf-8"));
   
   for(KeyValue kv:r.raw()){
    // Convert timestamp to date format
    String timestampFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(new Date(kv.getTimestamp()));
       //System.out.println("===:"+timestampFormat+"  ==timestamp: "+kv.getTimestamp());
    System.out.println("\nKeyValue: "+kv);
    System.out.println("key: "+kv.getKeyString());
    
    System.out.println("family=>"+new String(kv.getFamily(),"utf-8")
          +"  value=>"+new String(kv.getValue(),"utf-8")
    +"  qualifer=>"+new String(kv.getQualifier(),"utf-8")
    +"  timestamp=>"+timestampFormat);
 
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace ();
  }
  System.out.println("end===========showOneRecordByRowKey");
 }
 
 /**
  * Query a piece of data according to rowkey, a column cluster in a row
  * get 'student','010','info'  
  * student sid is the info column cluster of 010 (info:age,info:birthday)
  *
  * get 'student','010','info:age'  
  * student sid is info:age column of 010, quafilier is age
  */
 //public static void showOneRecordByRowKey_cloumn(String tableName,String rowkey,String column,String quafilier)
 public static void showOneRecordByRowKey_cloumn(String tableName,String rowkey,String column)
 {
  System.out.println("start===Query a column cluster according to the primary key showOneRecordByRowKey_cloumn");
  
  HTablePool pool = new HTablePool(hbaseconfig,1000);
  HTable table = (HTable) pool.getTable(tableName);
    
  try {
   Get get = new Get(rowkey.getBytes());
   get.addFamily(column.getBytes()); //Query a column family according to the primary key   
   //get.addColumn(Bytes.toBytes(column),Bytes.toBytes(quafilier)); ////Query the quafilier column in a column cluster according to the primary key
   Result r = table.get(get);
   
   for(KeyValue kv:r.raw()){
    System.out.println("KeyValue---"+kv);
    System.out.println("row=>"+new String(kv.getRow()));
    System.out.println("family=>"+new String(kv.getFamily(),"utf-8")+": "+new String(kv.getValue(),"utf-8"));
    System.out.println("qualifier=>"+new String(kv.getQualifier())+"\n");
 
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace ();
  }
  System.out.println("end===========showOneRecordByRowKey_cloumn");
 }
  
 //(1) Conversion of timestamp to time. A single timestamp cannot give an intuitive explanation.
 public String GetTimeByStamp(String timestamp)
 {
  long datatime= Long.parseLong(timestamp);
  Date date=new Date(datatime);
  SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
  String timeresult = format.format (date);
  System.out.println("Time : "+timeresult);
  return timeresult;
 
 }
 //(2) Time to timestamp conversion. Note that the time is in string format. The conversion between strings and time will not be described here.
 public String GetStampByTime(String time)
 {
  String Stamp="";
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  datedate;
  try
  {
   date=sdf.parse(time);
   Stamp=date.getTime()+"000";
   System.out.println(Stamp);
  
  }catch(Exception e){e.printStackTrace();}
  return Stamp;
 }

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325550252&siteId=291194637