Get the App
SLTechnology News&Howtos  ›  Servers  › 

Special HBase API usage

Shulou Source: shulou.com Published: 2022-05-31 21:26:13 09月28日 Update

Editor to share with you about the special usage of HBase API, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Go directly to the code:

Connection conn = ConnectionFactory.createConnection (conf); TableName tn = TableName.valueOf (strTN); Table table = conn.getTable (tn); RowFilter logFileTableFilter = new RowFilter (CompareFilter.CompareOp.EQUAL, new SubstringComparator ("0UAV0020109000574633450")); Scan s = new Scan (); s.setFilter (logFileTableFilter); ResultScanner ss = table.getScanner (s); for (Result r: ss) {for (Cell cell: r.listCells ()) {KeyValue kv = (KeyValue) cell System.out.print (new String (Arrays.copyOfRange (kv.getFamilyArray (), kv.getFamilyOffset (), kv.getFamilyOffset () + kv.getFamilyLength () + "); System.out.print (new String (Arrays.copyOfRange (kv.getQualifierArray (), kv.getQualifierOffset (), kv.getQualifierOffset () + kv.getQualifierLength () +") System.out.println (new String (Arrays.copyOfRange (kv.getValueArray (), kv.getValueOffset (), kv.getValueOffset () + kv.getValueLength ()) + ");}}

The first part is normal, layer 2 loops, the first layer is the loop result, which simply means the loop row, and the second layer loops each field.

Please note that the part I marked out looks easy to understand, which is to take the column family, column, and value, respectively.

But the truth is not like this. Take a look at the following picture:

All right, let's take another look at the source code:

Public byte [] getFamilyArray () {return this.bytes;} public byte [] getQualifierArray () {return this.bytes;} public byte [] getValueArray () {return this.bytes;}

Isn't this tmd the same thing?

All right, weird is weird, the key is not easy to use, it probably means to give you a byte array, all the data about this field is in this array, and then tell you the offset and length of families, columns, values, and so on, so you can dig it out yourself. If you have to do this every time, don't go crazy.

Arrays.copyOfRange (kv.getValueArray (), kv.getValueOffset (), kv.getValueOffset () + kv.getValueLength ())

Take a look at the following code, there is still a normal api, this is in line with human habits.

For (Result r: ss) {NavigableMap maps = r.getNoVersionMap (); for (byte [] fKey: maps.keySet ()) {NavigableMap map = maps.get (fKey); for (byte [] key: map.keySet ()) {System.out.print (new String (fKey) + ":"); System.out.print (new String (key) + "); System.out.println (new String (map.get (key) The above is all the content of this article "Special HBase API usage". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Loops things articles special code content weird that is arrays parts not so good not so good things two-tier human key most meaning data more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft NVidia Redmi Xiaomi Linux