2014-11-12 22 views
10

Ad esempio per la tabella HBase 'TEST_TABLE', valori inseriti sono:Scansione basata sul timestamp in HBase?

Row1 - Val1 => t 
Row1 - Val2 => t + 3 
Row1 - Val3 => t + 5 

Row2 - Val1 => t 
Row2 - Val2 => t + 3 
Row2 - Val3 => t + 5 

sulla scansione 'TEST_TABLE' dove version = t + 4 devono tornare

Row1 - Val1 => t + 3 
Row2 - Val2 => t + 3 

Come faccio a ottenere scansioni di base di data e ora (Basato sull'ultimo valore disponibile inferiore o uguale al timestamp) in HBase?

+0

Vuoi farlo nella shell HBase o scrivere un programma per questo? Dai un'occhiata qui su come ottenere l'elenco delle chiavi dalla tabella: http://stackoverflow.com/questions/5218085/how-to-list-all-row-keys-in-an-hbase-table, per ogni puoi rilasciare 'get' e ottenere le voci con timestamp http://stackoverflow.com/questions/8321741/retrieving-timestamp-from-hbase-row, quindi filtrarle in base alla tua condizione – 0x0FFF

risposta

23

Considerate questa tabella:

hbase(main):009:0> create 't1', { NAME => 'f1', VERSIONS => 100 } 
hbase(main):010:0> put 't1', 'key1', 'f1:a', 'value1' 
hbase(main):011:0> put 't1', 'key1', 'f1:a', 'value2' 
hbase(main):012:0> put 't1', 'key1', 'f1:a', 'value3' 
hbase(main):013:0> put 't1', 'key2', 'f1:a', 'value4' 
hbase(main):014:0> put 't1', 'key2', 'f1:a', 'value5' 
hbase(main):015:0> put 't1', 'key1', 'f1:a', 'value6' 

Ecco la scansione in guscio con tutte le versioni:

hbase(main):003:0> scan 't1', {VERSIONS => 100 } 
ROW    COLUMN+CELL 
key1   column=f1:a, timestamp=1416083314098, value=value6 
key1   column=f1:a, timestamp=1416083294981, value=value3 
key1   column=f1:a, timestamp=1416083293273, value=value2 
key1   column=f1:a, timestamp=1416083291009, value=value1 
key2   column=f1:a, timestamp=1416083305050, value=value5 
key2   column=f1:a, timestamp=1416083299840, value=value4 

Ecco la scansione limitato ad un timestamp specifica, come da voi richiesto:

hbase(main):002:0> scan 't1', { TIMERANGE => [0, 1416083300000] } 
ROW    COLUMN+CELL 
key1   column=f1:a, timestamp=1416083294981, value=value3 
key2   column=f1:a, timestamp=1416083299840, value=value4 

Ecco lo stesso nel codice Java:

package org.example.test; 
import org.apache.hadoop.hbase.HBaseConfiguration; 
import org.apache.hadoop.hbase.client.*; 
import org.apache.hadoop.hbase.util.Bytes; 
import java.io.IOException; 

public class test { 
    public static void main (String[] args) throws IOException { 
     HTable table = new HTable(HBaseConfiguration.create(), "t1"); 
     Scan s = new Scan(); 
     s.setMaxVersions(1); 
     s.setTimeRange (0L, 1416083300000L); 
     ResultScanner scanner = table.getScanner(s); 
     for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { 
      System.out.println(Bytes.toString(rr.getRow()) + " => " + 
        Bytes.toString(rr.getValue(Bytes.toBytes("f1"), Bytes.toBytes("a")))); 
     } 
    } 
} 

essere consapevoli del fatto che specificare l'intervallo di valore massimo il tempo è excluded, il che significa che se si vuole ottenere l'ultimo valore per tutte le chiavi con la massima timestamp T, è necessario specificare limite superiore del range di T + 1

+0

in Scala, non funziona. vedi http://stackoverflow.com/questions/38887556/hbase-scan-timerange-does-not-work-in-scala – anunixercoder

+0

Funziona bene .. Possiamo pulire ulteriormente l'output usando shell o qualsiasi altro linguaggio di scripting – minhas23

Problemi correlati