기본적인 컨넥션 연결하고, put, get, scan관련 코드다.
package com.sds.push.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
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 InsertTempData
{
public static void main(String[] args) throws IOException {
// keyspace
// String keySpace = "Platform";
//table
String devTable = "DeviceTable";
String usrTable = "UserTable";
// ColumnFamily
String ColFamInfo = "Info";
String ColFamConn = "Conn";
String ColFamCont = "Contact";
// Device Table
String DevRowKey = "dev";
// UserTable
String UsrRowKey = "user";
// Configuration
Configuration config;
config = HBaseConfiguration.create();
config.addResource(new Path("hbase-site.xml"));
HBaseAdmin admin = new HBaseAdmin(config);
// Device Table create
HTableDescriptor devTableDesc = new HTableDescriptor(devTable);
devTableDesc.addFamily(new HColumnDescriptor(Bytes.toBytes(ColFamInfo)));
devTableDesc.addFamily(new HColumnDescriptor(Bytes.toBytes(ColFamConn)));
if(!admin.tableExists(devTable))
{
admin.createTable(devTableDesc);
// Device Table
HTable hTableDev = new HTable(config, devTable);
for (int i = 0; i < 100; i++)
{
String rowKey = DevRowKey + i;
Put item = new Put(Bytes.toBytes(rowKey));
// ColumnFamily Info
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:type"),
Bytes.toBytes(String.valueOf(i)));
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:userId"),
Bytes.toBytes("user"+i));
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:imei"),
Bytes.toBytes("11-222222-222222-3"));
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:msisdn"),
Bytes.toBytes("821022223333"));
// ColumnFamily conn
item.add(Bytes.toBytes(ColFamConn),
Bytes.toBytes("conn:srv"),
Bytes.toBytes("cs" + i));
item.add(Bytes.toBytes(ColFamConn),
Bytes.toBytes("conn:presence"),
Bytes.toBytes(String.valueOf(i)));
hTableDev.put(item);
}
Get get = new Get(Bytes.toBytes("dev1"));
Result result = hTableDev.get(get);
byte[] value = result.getValue(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:userId"));
System.out.println("get result: " + Bytes.toString(value));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:userId"));
ResultScanner scanner = hTableDev.getScanner(scan);
try
{
while ((result = scanner.next()) != null)
{
System.out.println("scanned row: " + result);
}
}
catch (Exception e)
{
// TODO: handle exception
}finally {
scanner.close();
}
}else {
// Device Table
HTable hTableDev = new HTable(config, devTable);
Get get = new Get(Bytes.toBytes("dev1"));
Result result = hTableDev.get(get);
byte[] value = result.getValue(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:userId"));
System.out.println("get result: " + Bytes.toString(value));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:userId"));
ResultScanner scanner = hTableDev.getScanner(scan);
try
{
while ((result = scanner.next()) != null)
{
System.out.println("scanned row: " + result);
}
}
catch (Exception e)
{
// TODO: handle exception
}finally {
scanner.close();
}
}
// User Table create
HTableDescriptor usrTableDesc = new HTableDescriptor(usrTable);
usrTableDesc.addFamily(new HColumnDescriptor(Bytes.toBytes(ColFamInfo)));
usrTableDesc.addFamily(new HColumnDescriptor(Bytes.toBytes(ColFamCont)));
if (!admin.tableExists(usrTable))
{
admin.createTable(usrTableDesc);
// User Table
HTable hTableUsr = new HTable(config, usrTable);
for (int i = 0; i < 100 ; i++)
{
String rowKey = UsrRowKey + i;
Put item = new Put(Bytes.toBytes(rowKey));
// ColumnFamily Info
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:name"),
Bytes.toBytes("Tom" + i));
item.add(Bytes.toBytes(ColFamInfo),
Bytes.toBytes("info:avatar"),
Bytes.toBytes("EFE45F"+i));
// ColumnFamily conn
item.add(Bytes.toBytes(ColFamCont),
Bytes.toBytes("contact:user" + i),
Bytes.toBytes("<avatar image:xxxx.gif in bytes:100kb"));
item.add(Bytes.toBytes(ColFamCont),
Bytes.toBytes("contact:user" + (i+1)),
Bytes.toBytes("<avatar image:xxxx.gif in bytes:200kb"));
hTableUsr.put(item);
}
// user Table
HTable hTableDev = new HTable(config, usrTable);
Get get = new Get(Bytes.toBytes("user1"));
Result result = hTableDev.get(get);
byte[] value = result.getValue(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:name"));
System.out.println("get result: " + Bytes.toString(value));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:name"));
ResultScanner scanner = hTableDev.getScanner(scan);
try
{
while ((result = scanner.next()) != null)
{
System.out.println("scanned row: " + result);
}
}
catch (Exception e)
{
// TODO: handle exception
}finally {
scanner.close();
}
}else{
// usr Table
HTable hTableDev = new HTable(config, usrTable);
Get get = new Get(Bytes.toBytes("user1"));
Result result = hTableDev.get(get);
byte[] value = result.getValue(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:name"));
System.out.println("get result: " + Bytes.toString(value));
Scan scan = new Scan();
scan.addColumn(Bytes.toBytes(ColFamInfo), Bytes.toBytes("info:name"));
ResultScanner scanner = hTableDev.getScanner(scan);
try
{
while ((result = scanner.next()) != null)
{
System.out.println("scanned row: " + result);
}
}
catch (Exception e)
{
// TODO: handle exception
}finally {
scanner.close();
}
}
}
}