HBase创建数据
put ’表名’,’row key’,’列族名’,’值’
hbase(main):005:0> put 'emp','1','personal:name','raju'
0 row(s) in 0.6600 seconds
hbase(main):006:0> put 'emp','1','personal:city','hyderabad'
0 row(s) in 0.0410 seconds
hbase(main):007:0> put 'emp','1','professional:designation','manager'
0 row(s) in 0.0240 seconds
hbase(main):007:0> put 'emp','1','professional:salary','50000'
0 row(s) in 0.0240 seconds
使用Java API插入数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class InsertData{
public static void main(String[] args) throws IOException {
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
HTable hTable = new HTable(config, "emp");//初始化表名称
// Instantiating Put class
// accepts a row name.
Put p = new Put(Bytes.toBytes("row1")); //设置一个row key名称
// adding values using add() method
// accepts column family name, qualifier/row name ,value
p.add(Bytes.toBytes("personal"),Bytes.toBytes("name"),Bytes.toBytes("raju"));//指定属性和值
p.add(Bytes.toBytes("personal"),Bytes.toBytes("city"),Bytes.toBytes("hyderabad"));
p.add(Bytes.toBytes("professional"),Bytes.toBytes("designation"),Bytes.toBytes("manager"));
p.add(Bytes.toBytes("professional"),Bytes.toBytes("salary"),Bytes.toBytes("50000"));
// Saving the put Instance to the HTable.
hTable.put(p);
System.out.println("data inserted");
// closing HTable
hTable.close();
}
}
HBase更新数据
更新数据相当于重新put值 只是将新值替换旧值
put ‘table name’,’row ’,'Column family:column name',’new value
假设HBase中有一个表emp拥有下列数据
hbase(main):003:0> scan 'emp'
ROW COLUMN+CELL
row1 column=personal:name, timestamp=1418051555, value=raju
row1 column=personal:city, timestamp=1418275907, value=Hyderabad
row1 column=professional:designation, timestamp=14180555,value=manager
row1 column=professional:salary, timestamp=1418035791555,value=50000
1 row(s) in 0.0100 seconds
以下命令将更新名为“Raju’员工的城市值为’Delhi’。
hbase(main):002:0> put 'emp','row1','personal:city','Delhi'
0 row(s) in 0.0400 seconds
更新后的表如下所示,观察这个城市Raju的值已更改为“Delhi”。
hbase(main):003:0> scan 'emp'
ROW COLUMN+CELL
row1 column=personal:name, timestamp=1418035791555, value=raju
row1 column=personal:city, timestamp=1418274645907, value=Delhi
row1 column=professional:designation, timestamp=141857555,value=manager
row1 column=professional:salary, timestamp=1418039555, value=50000
1 row(s) in 0.0100 seconds
使用Java API更新数据
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class UpdateData{
public static void main(String[] args) throws IOException {
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
HTable hTable = new HTable(config, "emp");
// Instantiating Put class
//accepts a row name
Put p = new Put(Bytes.toBytes("row1"));
// Updating a cell value
p.add(Bytes.toBytes("personal"),
Bytes.toBytes("city"),Bytes.toBytes("Delih"));
// Saving the put Instance to the HTable.
hTable.put(p);
System.out.println("data Updated");
// closing HTable
hTable.close();
}
}
HBase读取数据
通过指定表名和row key
get ’<table name>’,’row1’
hbase(main):012:0> get 'emp', '1'
COLUMN CELL
personal : city timestamp=1417521848375, value=hyderabad
personal : name timestamp=1417521785385, value=ramu
professional: designation timestamp=1417521885277, value=manager
professional: salary timestamp=1417521903862, value=50000
4 row(s) in 0.0270 seconds
读取指定列
下面给出的是语法,使用get方法读取指定列。
hbase>get 'table name', ‘rowid’, {COLUMN => ‘column family:column name ’}
下面给出的示例,是用于读取HBase表中的特定列。
hbase(main):015:0> get 'emp', 'row1', {COLUMN=>'personal:name'}
COLUMN CELL
personal:name timestamp=1418035791555, value=raju
1 row(s) in 0.0080 seconds
使用Java API读取数据
从一个HBase表中读取数据,要使用HTable类的get()方法。这种方法需要Get类的一个实例。按照下面从HBase表中检索数据给出的步骤。
第1步:实例化Configuration类
Configuration类增加了HBase的配置文件到它的对象。使用HbaseConfiguration类的create()方法,如下图所示的配置对象。
Configuration conf = HbaseConfiguration.create();
第2步:实例化HTable类
有一类叫HTable,实现在HBase中的Table类。此类用于单个HBase的表进行通信。在这个类实例,它接受配置对象和表名作为参数。实例化HTable类,如下图所示。
HTable hTable = new HTable(conf, tableName);
第3步:实例化获得类
可以从HBase表使用HTable类的get()方法检索数据。此方法提取从一个给定的行的单元格。它需要一个 Get 类对象作为参数。创建如下图所示。
Get get = new Get(toBytes("row1"));
第4步:读取数据
当检索数据,可以通过ID得到一个单列,或得到一组行一组行ID,或者扫描整个表或行的子集。
可以使用Get类的add方法变种检索HBase表中的数据。
从特定的列族获取指定的列,使用下面的方法。
get.addFamily(personal)
要得到一个特定的列族的所有列,使用下面的方法。
get.addColumn(personal, name)
第5步:获取结果
获取结果通过Get类实例的HTable类的get方法。此方法返回Result类对象,其中保存所请求的结果。下面给出的是get()方法的使用。
Result result = table.get(g);
第6步:从Result实例读值
Result 类提供getValue()方法从它的实例读出值。如下图所示,使用它从Result 实例读出值。
byte [] value =
result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));
byte [] value1 =
result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));
下面给出的是从一个HBase表中读取值的完整程序
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.Result;
import org.apache.hadoop.hbase.util.Bytes;
public class RetriveData{
public static void main(String[] args) throws IOException, Exception{
// Instantiating Configuration class
Configuration config = HBaseConfiguration.create();
// Instantiating HTable class
HTable table = new HTable(config, "emp");
// Instantiating Get class
Get g = new Get(Bytes.toBytes("row1"));
// Reading the data
Result result = table.get(g);
// Reading values from Result class object
byte [] value = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("name"));
byte [] value1 = result.getValue(Bytes.toBytes("personal"),Bytes.toBytes("city"));
// Printing the values
String name = Bytes.toString(value);
String city = Bytes.toString(value1);
System.out.println("name: " + name + " city: " + city);
}
}
HBase删除数据
使用 delete 命令,可以在一个表中删除特定单元格。 delete 命令的语法如下:
delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’
下面是一个删除特定单元格和例子。在这里,我们删除salary
hbase(main):006:0> delete 'emp', '1', 'personal data:city',
1417521848375
0 row(s) in 0.0060 seconds
删除表的所有单元格
使用“deleteall”命令,可以删除一行中所有单元格。下面给出是 deleteall 命令的语法。
deleteall ‘<table name>’, ‘<row>’,
这里是使用“deleteall”命令删去 emp 表 row1 的所有单元的一个例子。
hbase(main):007:0> deleteall 'emp','1'
0 row(s) in 0.0240 seconds
使用scan命令验证表。表被删除后的快照如下。
hbase(main):022:0> scan 'emp'
ROW COLUMN+CELL
2 column=personal data:city, timestamp=1417524574905, value=chennai
2 column=personal data:name, timestamp=1417524556125, value=ravi
2 column=professional data:designation, timestamp=1417524204, value=sr:engg
2 column=professional data:salary, timestamp=1417524604221, value=30000
3 column=personal data:city, timestamp=1417524681780, value=delhi
3 column=personal data:name, timestamp=1417524672067, value=rajesh
3 column=professional data:designation, timestamp=1417523187, value=jr:engg
3 column=professional data:salary, timestamp=1417524702514, value=25000
使用Java API删除数据
可以从使用HTable类的delete()方法删除HBase表数据。按照下面给出从表中删除数据的步骤。
第1步:实例化Configuration类
Configuration类增加了HBase配置文件到它的对象。可以创建使用HbaseConfiguration类的create()方法,如下图所示的Configuration 对象。
Configuration conf = HbaseConfiguration.create();
第2步:实例化HTable类
有一个类叫HTable,实现在HBase中的Table类。此类用于单个HBase的表进行通信。在这个类实例,它接受配置对象和表名作为参数。实例化HTable类,如下图所示。
HTable hTable = new HTable(conf, tableName);
第3步:实例化Delete 类
通过传递将要删除的行的行ID,在字节数组格式实例化Delete类。也可以通过构造时间戳和Rowlock。
Delete delete = new Delete(toBytes("row1"));
第4步:选择删除数据
可以使用Delete类的delete方法删除数据。这个类有各种删除方法。选择使用这些方法来删除列或列族。这里显示Delete类方法的用法在下面的例子。
delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
delete.deleteFamily(Bytes.toBytes("professional"));
第5步:删除数据
通过HTable类实例的delete()方法,如下所示删除所选数据。
table.delete(delete);
第6步:关闭HTable实例
删除数据后,关闭HTable实例。
table.close();
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class DeleteData {
public static void main(String[] args) throws IOException {
// Instantiating Configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HTable class
HTable table = new HTable(conf, "employee");
// Instantiating Delete class
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
delete.deleteFamily(Bytes.toBytes("professional"));
// deleting the data
table.delete(delete);
// closing the HTable object
table.close();
System.out.println("data deleted.....");
}
}