1. HBaseAdmin —— 对表的创建、删除、显示以及修改等;
2. HTable —— 通过HTable的实例来访问表并进行数据的操作,获取表实例如下两种方法:
3.插入数据:
创建一个Put对象,并指定参数为一个行(Row)值(即指定给哪一列增加数据以及当前的时间戳等值),然后调用HTable.put(Put)。
4.获取数据
创建一个Get对象,在构造的时候传入行值,表示取第几行的数据,然后调用HTable.get(Get)。
5.删除数据
创建一个Delete对象,参数为一个行(Row)值,然后调用HTable.delete(Delete)。
6. 浏览数据
通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个。
通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。
HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。
Result是一个KeyValue的链表,遍历过程当中保存当前行信息。
以下是一个java对hbase的基本操作实例(hadoop-1.0.3 && hbase-0.90.3)
- package
tool; -
-
import
java.io.IOException; -
import
java.util.Scanner; -
-
import
org.apache.hadoop.conf.Configuration; -
import
org.apache.hadoop.hbase.HBaseConfiguration; -
import
org.apache.hadoop.hbase.HColumnDescriptor; -
import
org.apache.hadoop.hbase.HTableDescriptor; -
import
org.apache.hadoop.hbase.KeyValue; -
import
org.apache.hadoop.hbase.client.Delete; -
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.HTablePool; -
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 Hbase { -
-
Configuration null;config = -
{ -
config = HBaseConfiguration.create(); -
config.set( "192.168.21.20, 192.168.21.21, );192.168.21.22" -
config.set( "2181"); -
} -
-
a new table -
void createTable(String throwstableName, String[] familys) IOException { -
HBaseAdmin admin = HBaseAdmin(config); -
(admin.tableExists(tableName)) { -
System.out.println(tableName + is );already exists,Please create another table!" -
} { -
HTableDescriptor desc = HTableDescriptor(tableName); -
( inti 0;= i < familys.length; i++) { -
HColumnDescriptor family = HColumnDescriptor(familys[i]); -
desc.addFamily(family); -
} -
admin.createTable(desc); -
System.out.println( table \'" + "\'tableName + OK!" ); -
} -
-
} -
-
a table -
void deleteTable(String throwstableName) IOException { -
HBaseAdmin admin = HBaseAdmin(config); -
(!admin.tableExists(tableName)) { -
System.out.println(tableName + is );not exists!" -
} { -
Scanner s = Scanner(System.in); -
System.out.print( you );sure to delete(y/n)?" -
String str = s.nextLine(); -
(str.equals( "y")|| "Y"))str.equals( { -
admin.disableTable(tableName); -
admin.deleteTable(tableName); -
System.out.println(tableName + is );delete!" -
} { -
System.out.println(tableName + is );not delete!" -
} -
} -
} -
-
table example -
void getTable(String throwstableName) IOException { -
I: -
//
HTable table = new HTable(config, tableName); -
II: better than I. -
HTablePool pool = HTablePool(config, 1000); -
-
HTable table = (HTable) pool.getTable(tableName); -
} -
-
a data -
void insertData(String tableName, String rowKey, String family, String qualifier, String value) { -
{ -
HTablePool pool = HTablePool(config, 1000); -
HTable table = (HTable)pool.getTable(tableName); -
Put put = Put(Bytes.toBytes(rowKey)); -
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); -
table.put(put); -
System.out.println( a );data successful!" -
} (IOException e) { -
e.printStackTrace(); -
} -
} -
-
a data -
void deleteData(String tableName, String rowKey) { -
{ -
HTablePool pool = HTablePool(config, 1000); -
HTable table = (HTable)pool.getTable(tableName); -
Delete del = Delete(Bytes.toBytes(rowKey)); -
table.delete(del); -
System.out.println( a );data successful" -
table.close(); -
} (IOException e) { -
e.printStackTrace(); -
} -
} -
-
all data -
void queryAll(String tableName) { -
{ -
HTablePool pool = HTablePool(config, 1000); -
HTable table = (HTable) pool.getTable(tableName); -
Scan scan = Scan(); -
ResultScanner scanner = table.getScanner(scan); -
-
(Result row : scanner) { -
System.out.println( " + newString(row.getRow())); -
(KeyValue kv : row.raw()) { -
System.out.print( String(kv.getRow()) "+ " );as above -
System.out.print( String(kv.getFamily()) ":");+ -
System.out.print( String(kv.getQualifier()) "+ = );" -
System.out.print( String(kv.getValue())); -
System.out.print( timestamp = " + "\n");kv.getTimestamp() + -
} -
} -
table.close(); -
} (IOException e) { -
e.printStackTrace(); -
} -
} -
by rowkey -
void queryByRowKey(String tableName, String rowKey) { -
{ -
HTablePool pool = HTablePool(config, 1000); -
HTable table = (HTable)pool.getTable(tableName); -
Get get = Get(rowKey.getBytes()); -
Result row = table.get(get); -
(KeyValue kv : row.raw()) { -
System.out.print( String(kv.getRow()) "+ " ); -
System.out.print( String(kv.getFamily()) ":");+ -
System.out.print( String(kv.getQualifier()) "+ = );" -
System.out.print( String(kv.getValue())); -
System.out.print( timestamp = " + "\n");kv.getTimestamp() + -
} -
table.close(); -
} (IOException e) { -
e.printStackTrace(); -
} -
} -
-
-
static void main(String[] throwsargs) IOException { -
Hbase hbase = Hbase(); -
a new table -
String tableName = -
System.out.println( table======" ); -
hbase.deleteTable(tableName); -
-
System.out.println( table======" ); -
String[] familys = { "scores"}; -
hbase.createTable(tableName, familys); -
-
System.out.println( data=======" ); -
Jim -
hbase.insertData(tableName, "info", "sex", "male"); -
hbase.insertData(tableName, "info", "age", "18"); -
hbase.insertData(tableName, "scores", "Chinese", "98"); -
hbase.insertData(tableName, "scores", "English", "90"); -
hbase.insertData(tableName, "scores", "Math", "100"); -
Ann -
hbase.insertData(tableName, "info", "sex", "female"); -
hbase.insertData(tableName, "info", "age", "18"); -
hbase.insertData(tableName, "scores", "Chinese", "97"); -
hbase.insertData(tableName, "scores", "Math", "95"); -
-
System.out.println( all );data=======" -
hbase.queryAll(tableName); -
-
System.out.println( by );rowkey=======" -
String rowKey = -
hbase.queryByRowKey(tableName, rowKey); -
-
System.out.println( a );data=======" -
hbase.deleteData(tableName, rowKey); -
hbase.queryAll(tableName); -
} -
- }
程序运行结果:
=======delete table======
Are you sure to delete(y/n)?y
test is delete!
=======create table======
Create table 'test' OK!
=======insert data=======
insert a data successful!
=======query all data=======
Rowkey: Ann
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160
Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052
=======query by rowkey=======
Ann info:age = 18 timestamp = 1349857967109
Ann info:sex = female timestamp = 1349857967080
Ann scores:Chinese = 97 timestamp = 1349857967134
Ann scores:Math = 95 timestamp = 1349857967160
=======delete a data=======
delete a data successful
Rowkey: Jim
Jim info:age = 18 timestamp = 1349857966936
Jim info:sex = male timestamp = 1349857966908
Jim scores:Chinese = 98 timestamp = 1349857966961
Jim scores:English = 90 timestamp = 1349857967002
Jim scores:Math = 100 timestamp = 1349857967052