Exercise 25 - HBase CRUD operations

eclipse
apache-hbase
java
#1

Problem statement

  • Perform CRUD operations on HBase
  • Launch HBase shell (hbase shell)
  • Create namespace
  • Create table
  • Insert few rows with several columns
  • Perform CRUD operations using Java maven
  • Create dependencies in pom.xml or build.sbt
  <dependency>
  	<groupId>org.apache.hbase</groupId>
  	<artifactId>hbase-client</artifactId>
  	<version>1.1.2</version>
  </dependency>
  <dependency>
  	<groupId>org.apache.hbase</groupId>
  	<artifactId>hbase-common</artifactId>
  	<version>1.1.2</version>
  </dependency>
  • Write JDBC code using zookeeper quorum
        static Configuration conf = HBaseConfiguration.create();
  conf.set("hbase.zookeeper.quorum", 
  		"nn01.itversity.com,nn02.itversity.com,rm01.itversity.com");
  conf.set("hbase.zookeeper.property.clientPort", "2181");
  
  Connection connection = ConnectionFactory.createConnection(conf);
  Table table = connection.getTable(TableName.valueOf("demo"));

Please provide the following

  • Hbase shell commands executed
  • Java code to connect to HBase and get the results
0 Likes

#2

hbase code:
create_namespace 'sumanthsharma’
create ‘sumanthsharma:details’,‘name’

python code:
import happybase
connection = happybase.Connection(‘localhost’)

table = connection.table(‘sumanthsharma:details’)
//adding a row

table.put(b’1’, {b’name:first’: b’sumanth’,b’name:last’: b’sharma’})
//retriveing row on row key

row = table.row(b’1’)
//printing desired coloumn

print(row[b’name:first’])
sumanth
//deleting row by row key

row = table.delete(b’1’)
row = table.row(b’1’)
//verifying if row is deleted

print(row[b’name:first’])
Traceback (most recent call last):
File “”, line 1, in
KeyError: ‘name:first’

0 Likes

#3

#Hbase shell commands

hbase(main):001:0> create 'employee' , 'cf'
0 row(s) in 0.8190 seconds

=> Hbase::Table - employee
hbase(main):002:0> put 'employee', '1', 'cf:name', 'farhan'
0 row(s) in 0.1000 seconds

hbase(main):003:0> put 'employee', '1', 'cf:age', '24'
0 row(s) in 0.0100 seconds

hbase(main):004:0> put 'employee', '2', 'cf:name', 'saswat'
0 row(s) in 0.0070 seconds

hbase(main):005:0> put 'employee', '2', 'cf:age', '25'
0 row(s) in 0.0080 seconds

hbase(main):006:0> scan 'employee'
ROW                                           COLUMN+CELL                                                                                                                        
 1                                            column=cf:age, timestamp=1482405559573, value=24                                                                                   
 1                                            column=cf:name, timestamp=1482405549318, value=farhan                                                                              
 2                                            column=cf:age, timestamp=1482405592305, value=25                                                                                   
 2                                            column=cf:name, timestamp=1482405579097, value=saswat                                                                              
2 row(s) in 0.0270 seconds

#To connect to HBase and get the results

##Java code

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class OnJava {

	static Configuration conf = HBaseConfiguration.create();

	public static void main(String[] args) throws IOException {

		conf.set("hbase.zookeeper.quorum", "localhost");
		conf.set("hbase.zookeeper.property.clientPort", "2181");

		Connection connection = ConnectionFactory.createConnection(conf);

		Table table = connection.getTable(TableName.valueOf("employee"));

		// Insert data

		Put put = new Put(Bytes.toBytes("3"));

		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"),
				Bytes.toBytes("kamleshwar"));
		put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"),
				Bytes.toBytes("26"));

		table.put(put);

		// Retrieve Data

		Get get = new Get(Bytes.toBytes("3"));

		Result result = table.get(get);

		byte[] nameByte = result.getValue(Bytes.toBytes("cf"),
				Bytes.toBytes("name"));
		byte[] ageByte = result.getValue(Bytes.toBytes("cf"),
				Bytes.toBytes("age"));

		String nameString = Bytes.toString(nameByte);

		String ageString = Bytes.toString(ageByte);

		System.out.println("Name is " + nameString + ", age is " + ageString);

	}
}

###Output

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Name is kamleshwar, age is 26

##Scala code

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.util.HBaseConfTool
import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Connection
import org.apache.hadoop.hbase.client.ConnectionFactory
import org.apache.hadoop.hbase.TableName
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.client.Get

object HbaseBasics {

  val conf: Configuration = HBaseConfiguration.create()

  def main(args: Array[String]): Unit = {

    conf.set("hbase.zookeeper.quorum", "localhost")
    conf.set("hbase.zookeeper.property.clientPort", "2181")

    val connection: Connection = ConnectionFactory.createConnection(conf)

    val table = connection.getTable(TableName.valueOf("employee"))

    // Insert data

    val p = new Put(Bytes.toBytes("4"))
    p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("name"), Bytes.toBytes("arun"))
    p.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("age"), Bytes.toBytes("27"))
    table.put(p)

    //Retreive Data

    for (rowKey <- 1 to 4) {

      val result = table.get(new Get(Bytes.toBytes(rowKey.toString())))

      val nameByte = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("name"))
      val ageByte = result.getValue(Bytes.toBytes("cf"), Bytes.toBytes("age"))

      val name = Bytes.toString(nameByte)
      val age = Bytes.toString(ageByte)

      println("Name is " + name + ", age is " + age)

    }

  }
}

###Output

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Name is farhan, age is 24
Name is saswat, age is 25
Name is kamleshwar, age is 26
Name is arun, age is 27
1 Like

#4

create_namespace ‘parulshine92’

create ‘parulshine92:employee’ , ‘colfam’

put ‘parulshine92:employee’ , 3, ‘colfam:deptID’ , '1’
put ‘parulshine92:employee’ , 1, ‘colfam:deptname’ , 'Marketing’
put ‘parulshine92:employee’ , 1, ‘colfam:deptID’ , '1’
0 row(s) in 0.0090 seconds

get ‘parulshine92:employee’ , 3
hbase(main):005:0> scan 'parulshine92:employee’
ROW COLUMN+CELL
1 column=colfam:deptID, timestamp=1482405621803, value=1
1 column=colfam:deptname, timestamp=1482405532762, value=Marketing
2 column=colfam:deptID, timestamp=1482405678618, value=2
2 column=colfam:deptname, timestamp=1482405715457, value=Finance
3 column=colfam:deptID, timestamp=1482404223801, value=1
3 column=colfam:deptname, timestamp=1482404351231, value=revenue
3 row(s) in 0.0180 seconds

0 Likes