Trending October 2023 # Hbase Query Example: Put(), Get(), Scan() Command In Hbase # Suggested November 2023 # Top 13 Popular | Khongconthamnam.com

Trending October 2023 # Hbase Query Example: Put(), Get(), Scan() Command In Hbase # Suggested November 2023 # Top 13 Popular

You are reading the article Hbase Query Example: Put(), Get(), Scan() Command In Hbase updated in October 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Hbase Query Example: Put(), Get(), Scan() Command In Hbase

In this tutorial, you will learn:

Write Data to HBase Table: Shell

The put command is used to store data into a table

This command is used for the following things

It will put a cell ‘value’ at a defined or specified table or row or column.

It will optionally coordinate time stamp.

Example:

Here we are placing values into table “guru99” under row r1 and column c1

We have placed three values, 10,15 and 30 in table “guru99” as shown in the screenshot below

Suppose if the table “Guru99” having some table reference like say g. We can also run the command on table reference also like

The output will be as shown in the above screenshot after placing values into “guru99”.

Read Data from HBase Table: Shell

In this section, we will check the following

Values that are inserted into HBase table “guru99”

Column names with values present in HBase Table guru99

From the above screenshot, we can infer

If we run “scan” command in HBase shell it will display the inserted values in “guru99” as follow

In HBase shell, it will display values inserted by our code with column and row names

Here we can see the column name inserted are “education” and “projects”

The values inserted are “BigData” and “HBase Tutorials” into mentioned columns

You can also use the Get command to read data from a table

By using this command, you will get a row or cell contents present in the table. In addition to that you can also add additional parameters to it like TIMESTAMP, TIMERANGE,VERSIONS, FILTERS, etc. to get a particular row or cell content.

Examples:-

For table “guru99′ row r1 and column c1 values will display using this command as shown in the above screenshot

For table “guru99” row r1 values will be displayed using this command

For table “guru99” row 1 values in the time range ts1 and ts2 will be displayed using this command

For table “guru99” row r1 and column families’ c1, c2, c3 values will be displayed using this command

Write Data to HBase Table: JAVA API

In this step, we are going to write data into HBase table “guru99”

First, we have to write code for insert and retrieve values from HBase by using-HBaseLoading.java program.

For creating and inserting values into a table at the column level, you have to code like below.

From the above screen shot

When we create HBase configuration, it will point to whatever the configurations we set in chúng tôi and chúng tôi files during HBase installations

Creation of table “guru99” using HTable method

Adding row1 to table “guru99”

Specifying column names “education” and “projects” and inserting values into column names in the respective row1. The values inserted here are “BigData” and “HBaseTutorials”.

Read Data from HBase Table: Java API

Whatever the values that we placed in HBase tables in the above section, here we are going to fetch and display those values.

For retrieving results stored in “guru99”

The above screenshot shows the data is being read from HBase table ‘guru99’

In this, we are going to fetch the values that are stored in column families i.e “education” and “projects”

Using “get” command we are going to fetch stored values in HBase table

Scanning results using “scan” command. The values that are stored in row1 it will display on the console.

Once writing code is done, you have to run java application like this

After running “HBaseLoading .java” the values going to insert into “guru99” in each column in HBase and in the same program it can retrieve values also.

Here is the complete code

import java.io.IOException; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; 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.util.Bytes; public class HBaseLoading { public static void main(String[] args) throws IOException { org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create(); /*This instantiates an HTable object that connects you to the "test" table*/ HTable table = new HTable(config, "guru99"); /* To add to a row, use Put. A Put constructor takes the name of the row you want to insert into as a byte array.*/ Put p = new Put(Bytes.toBytes("row1")); /*To set the value you'd like to update in the row 'row1', specify the column family, column qualifier, and value of the table cell you'd like to update. The column family must already exist in your table schema. The qualifier can be anything.*/ p.add(Bytes.toBytes("education"), Bytes.toBytes("col1"),Bytes.toBytes("BigData")); p.add(Bytes.toBytes("projects"),Bytes.toBytes("col2"),Bytes.toBytes("HBaseTutorials")); table.put(p); Get g = new Get(Bytes.toBytes("row1")); Result r = table.get(g); byte [] value = r.getValue(Bytes.toBytes("education"),Bytes.toBytes("col1")); byte [] value1 = r.getValue(Bytes.toBytes("projects"),Bytes.toBytes("col2")); String valueStr = Bytes.toString(value); String valueStr1 = Bytes.toString(value1); System.out.println("GET: " +"education: "+ valueStr+"projects: "+valueStr1); Scan s = new Scan(); s.addColumn(Bytes.toBytes("education"), Bytes.toBytes("col1")); s.addColumn(Bytes.toBytes("projects"), Bytes.toBytes("col2")); ResultScanner scanner = table.getScanner(s); try { for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { System.out.println("Found row : " + rr); } } finally { scanner.close(); } } } Summary:

As we discussed in this tutorial, you can use the put command to insert data into a table. You can use the scan, get command to read data from a table

You're reading Hbase Query Example: Put(), Get(), Scan() Command In Hbase

Update the detailed information about Hbase Query Example: Put(), Get(), Scan() Command In Hbase on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!