ตัวอย่างแบบสอบถาม HBase: ใส่(), รับ (), สแกน() คำสั่งใน HBase

เขียนข้อมูลลงในตาราง HBase: Shell

คำสั่ง put ใช้ในการเก็บข้อมูลลงในตาราง

Syntax:  put <'tablename'>,<'rowname'>,<'columnvalue'>,<'value'>

คำสั่งนี้ใช้สำหรับสิ่งต่อไปนี้

  • มันจะใส่ 'ค่า' ของเซลล์ไว้ที่ตารางหรือแถวหรือคอลัมน์ที่กำหนดหรือระบุ
  • มันจะเลือกประสานการประทับเวลา

ตัวอย่าง:

  • ที่นี่เรากำลังวางค่าลงในตาราง “guru99” ใต้แถว r1 และคอลัมน์ c1
    hbase> put 'guru99', 'r1', 'c1', 'value', 10
  • เราได้วางค่าสามค่า 10,15 และ 30 ไว้ในตาราง “guru99” ดังที่แสดงในภาพหน้าจอด้านล่าง

เขียนข้อมูลลงในตาราง HBase: Shell

  • สมมุติว่าถ้าตาราง “Guru99” มีตารางอ้างอิงเช่นว่า g นอกจากนี้เรายังสามารถรันคำสั่งบนการอ้างอิงตารางได้เช่นกัน
    hbase> g.put 'guru99', 'r1', 'c1', 'value', 10
  • ผลลัพธ์จะเป็นดังที่แสดงในภาพหน้าจอด้านบนหลังจากวางค่าลงใน “guru99”

อ่านข้อมูลจากตาราง HBase: Shell

ในส่วนนี้เราจะตรวจสอบสิ่งต่อไปนี้

  • ค่าที่แทรกลงในตาราง HBase “guru99”
  • ชื่อคอลัมน์ที่มีค่าอยู่ใน HBase Table guru99

อ่านข้อมูลจากตาราง HBase: Shell

จากภาพหน้าจอข้างต้น เราสามารถอนุมานได้

  • หากเรารันคำสั่ง “scan” ในเชลล์ HBase มันจะแสดงค่าที่แทรกใน “guru99” ดังต่อไปนี้
  • ในเชลล์ HBase มันจะแสดงค่าที่แทรกโดยโค้ดของเราพร้อมชื่อคอลัมน์และแถว
  • ที่นี่เราจะเห็นชื่อคอลัมน์ที่แทรกไว้คือ “การศึกษา” และ “โครงการ”
  • ค่าที่แทรกคือ “BigData” และ “HBase Tutorials” ลงในคอลัมน์ที่กล่าวถึง

คุณยังสามารถใช้คำสั่ง Get เพื่ออ่านข้อมูลจากตารางได้

Syntax: get <'tablename'>, <'rowname'>, {< Additional parameters>}

ที่นี่ รวมถึง TIMERANGE, TIMESTAMP, VERSIONS และ FILTERS

เมื่อใช้คำสั่งนี้ คุณจะได้รับเนื้อหาแถวหรือเซลล์ในตาราง นอกจากนั้น คุณยังสามารถเพิ่มพารามิเตอร์เพิ่มเติมได้ เช่น TIMESTAMP, TIMERANGE,VERSIONS, FILTERS เป็นต้น เพื่อรับเนื้อหาแถวหรือเซลล์ที่ต้องการ

อ่านข้อมูลจากตาราง HBase: Shell

ตัวอย่าง:-

hbase> get 'guru99', 'r1', {COLUMN => 'c1'}

สำหรับตาราง “guru99′ ค่าแถว r1 และคอลัมน์ c1 จะแสดงโดยใช้คำสั่งนี้ดังที่แสดงในภาพหน้าจอด้านบน

hbase> get 'guru99', 'r1'

สำหรับตาราง “guru99” แถว r1 จะแสดงค่าโดยใช้คำสั่งนี้

hbase> get 'guru99', 'r1', {TIMERANGE => [ts1, ts2]}

สำหรับตาราง “guru99” แถวที่ 1 ในช่วงเวลา ts1 และ ts2 จะแสดงโดยใช้คำสั่งนี้

hbase> get 'guru99', 'r1', {COLUMN => ['c1', 'c2', 'c3']}

สำหรับตาราง “guru99” แถว r1 และคอลัมน์ตระกูล c1, c2, c3 จะแสดงโดยใช้คำสั่งนี้

เขียนข้อมูลลงในตาราง HBase: JAVA API

ในขั้นตอนนี้ เราจะเขียนข้อมูลลงในตาราง HBase “guru99”

ก่อนอื่นเราต้องเขียนโค้ดเพื่อแทรกและดึงค่าจาก HBase โดยใช้-โปรแกรม HBaseLoading.java

สำหรับการสร้างและการแทรกค่าลงในตารางในระดับคอลัมน์ คุณจะต้องเขียนโค้ดตามด้านล่างนี้.

เขียนข้อมูลลงในตาราง HBase: JAVA API

จากภาพหน้าจอด้านบน

  1. เมื่อเราสร้างการกำหนดค่า HBase มันจะชี้ไปที่การกำหนดค่าใดก็ตามที่เราตั้งค่าไว้ในไฟล์ base-site.xml และ hbase-default.xml ในระหว่างนั้น การติดตั้ง HBase
  2. การสร้างตาราง “guru99” โดยใช้วิธี HTable
  3. การเพิ่มแถวที่ 1 ลงในตาราง “guru99”
  4. การระบุชื่อคอลัมน์ “การศึกษา” และ “โครงการ” และการแทรกค่าลงในชื่อคอลัมน์ในแถวที่ 1 ค่าที่แทรกที่นี่คือ “BigData” และ “HBaseTutorials”

อ่านข้อมูลจากตาราง HBase: Java API

ไม่ว่าค่าที่เราใส่ไว้ในตาราง HBase ในส่วนด้านบนจะเป็นอย่างไร เราจะดึงข้อมูลและแสดงค่าเหล่านั้น

สำหรับการดึงผลลัพธ์ที่เก็บไว้ใน “guru99”

อ่านข้อมูลจากตาราง HBase: Java API

ภาพหน้าจอด้านบนแสดงข้อมูลที่กำลังอ่านจากตาราง HBase 'guru99'

  1. ในที่นี้ เราจะดึงค่าที่จัดเก็บไว้ในตระกูลคอลัมน์ เช่น "การศึกษา" และ "โครงการ"
  2. การใช้คำสั่ง "get" เราจะดึงค่าที่เก็บไว้ในตาราง HBase
  3. การสแกนผลลัพธ์โดยใช้คำสั่ง “scan” ค่าที่เก็บไว้ใน row1 จะแสดงบนคอนโซล

เมื่อเขียนโค้ดเสร็จแล้วคุณต้องรัน ชวา แอปพลิเคชันเช่นนี้

  • คลิกขวาที่ HBaseLoading.java -> เรียกใช้เป็น -> Java การใช้งาน
  • หลังจากรัน “HBaseLoading .java” ค่าจะถูกแทรกลงใน “guru99” ในแต่ละคอลัมน์ใน HBase และในโปรแกรมเดียวกันก็สามารถดึงค่าได้เช่นกัน

นี่คือรหัสที่สมบูรณ์

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						
{							
/* When you create a HBaseConfiguration, it reads in whatever you've set into your hbase-site.xml and in hbase-default.xml, as long as these can be found on the CLASSPATH*/							

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"));

// Once you've adorned your Put instance with all the updates you want to  make, to commit it do the following 							

table.put(p);	

// Now, to retrieve the data we just wrote.	

  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							
{							
// Make sure you close your scanners when you are done!						

scanner.close();							
       }							
   }							
}

สรุป

ดังที่เราได้กล่าวถึงในบทช่วยสอนนี้ คุณสามารถใช้คำสั่ง put เพื่อแทรกข้อมูลลงในตารางได้ คุณสามารถใช้การสแกนรับคำสั่งเพื่ออ่านข้อมูลจากตาราง