#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