'
将第一行的值插入到emp表如下所示。
hbase(main):005:0> put 'emp','1','personal data:name','raju'
0 row(s) in 0.6600 seconds
hbase(main):006:0> put 'emp','1','personal data:city','hyderabad'
0 row(s) in 0.0410 seconds
hbase(main):007:0> put 'emp','1','professional data:designation','manager'
0 row(s) in 0.0240 seconds
hbase(main):007:0> put 'emp','1','professional data:salary','50000'
0 row(s) in 0.0240 seconds
以相同的方式使用put命令插入剩余的行。如果插入完成整个表格,会得到下面的输出。
hbase(main):022:0> scan 'emp'
ROW COLUMN+CELL
1 column=personal data:city, timestamp=1417524216501, value=hyderabad
1 column=personal data:name, timestamp=1417524185058, value=ramu
1 column=professional data:designation, timestamp=1417524232601,value=manager
1 column=professional data:salary, timestamp=1417524244109, value=50000
2 column=personal data:city, timestamp=1417524574905, value=chennai
2 column=personal data:name, timestamp=1417524556125, value=ravi
2 column=professional data:designation, timestamp=1417524592204,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=1417524693187,value=jr:engg
3 column=professional data:salary, timestamp=1417524702514,value=25000
【2】JavaAPI 使用
可以使用Put 类的add()方法将数据插入到HBase。可以使用HTable类的put()方法保存数据。这些类属于org.apache.hadoop.hbase.client包。下面给出的步骤是在一个HBase表创建数据。
第1步:实例化配置类
//Configuration类增加了 HBase 配置文件到它的对象。使用HbaseConfiguration类的create()方法,如下图所示的配置对象。
Configuration conf = HbaseConfiguration.create();
第2步:实例化HTable类
//有一类名为HTable,在HBase中实现了Table。这个类用于单个HBase表进行通信。在这个类实例接受配置对象和表名作为参数。可以实例HTable类,如下图所示。
HTable hTable = new HTable(conf, tableName);
第3步:实例化Put类
//为了将数据插入到HBase表中,需要使用add()方法和变体。这种方法属于Put类,因此实例化Put类。这个类必须要以字符串格式的列名插入数据。可以实例Put类,如下图所示。
Put p = new Put(Bytes.toBytes("row1"));
第4步:插入数据
//Put类的add()方法用于插入数据。它需要代表列族,分别为:列限定符(列名称)3字节阵列,以及要插入的值。使用add()方法将数据插入HBase表如下图所示。
p.add(Bytes.toBytes("coloumn family "), Bytes.toBytes("column name"),Bytes.toBytes("value"));
第5步:保存数据到表中
//插入所需的行后,HTable类put实例的put()方法添加,如下所示保存更改。
hTable.put(p);
第6步:关闭HTable实例
//创建在HBase的表数据之后,使用close()方法,如下所示关闭HTable实例。
hTable.close();
下面给出的是在HBase的表创建数据的完整程序。
import java.io.IOException;
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();
config
.set(
"hbase.zookeeper.quorum"
,
"140.143.236.169:2181"
);
config
.set(
"hbase.rootdir"
,
"hdfs://140.143.236.169:8020/hbase"
);
// Instantiating HTable class
HTable hTable = new HTable(config, "emp");
// Instantiating Put class
// accepts a row name.
Put p = new Put(Bytes.toBytes("row1"));
// 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();
}
}
编译和执行上述程序如下所示。
data inserted
二、更新数据
【1】Shell 使用
可以使用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
【2】JavaAPI 使用
使用put()方法将特定单元格更新数据。按照下面给出更新表的现有单元格值的步骤。
第1步:实例化Configuration类
//Configuration类增加了HBase的配置文件到它的对象。使用HbaseConfiguration类的create()方法,如下图所示的配置对象。
Configuration conf = HbaseConfiguration.create();
第2步:实例化HTable类
//有一类叫HTable,实现在HBase中的Table类。此类用于单个HBase的表进行通信。在这个类实例,它接受配置对象和表名作为参数。实例化HTable类,如下图所示。
HTable hTable = new HTable(conf, tableName);
第3步:实例化Put类
//要将数据插入到HBase表中,使用add()方法和它的变体。这种方法属于Put类,因此实例化Put类。这个类必须以字符串格式的列名插入数据。可以实例化Put类,如下图所示。
Put p = new Put(Bytes.toBytes("row1"));
第4步:更新现有的单元格
//Put 类的add()方法用于插入数据。它需要表示列族,列限定符(列名称)3字节阵列,并要插入的值。将数据插入HBase表使用add()方法,如下图所示。
p.add(Bytes.toBytes("coloumn family "), Bytes.toBytes("columnname"),Bytes.toBytes("value"));
p.add(Bytes.toBytes("personal"),Bytes.toBytes("city"),Bytes.toBytes("Delih"));
第5步:保存表数据
//插入所需的行后,HTable类实例的put()方法添加如下所示保存更改。
hTable.put(p);
第6步:关闭HTable实例
//创建在HBase的表数据之后,使用close()方法,如下所示关闭HTable实例。
hTable.close();
下面给出的是完整的程序,在一个特定的表更新数据。
import java.io.IOException;
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();
conf
.set(
"hbase.zookeeper.quorum"
,
"140.143.236.169:2181"
);
conf
.set(
"hbase.rootdir"
,
"hdfs://140.143.236.169:8020/hbase"
);
// 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();
}
}
编译和执行上述程序如下所示。
data Updated
三、读取数据
【1】Shell 使用
1、按指定RowKey获取唯一一条记录,get方法(org.apache.hadoop.hbase.client.Get)
2、按指定的条件获取一批记录,scan方法(org.apache.hadoop.hbase.client.Scan) 实现条件查询功能使用的就是scan方式
get命令和HTable类的get()方法用于从HBase表中读取数据。使用 get 命令,可以同时获取一行数据。它的语法如下:
get '
','row1'
下面的例子说明如何使用get命令。扫描emp表的第一行。
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
【2】JavaAPI 使用
从一个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.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();
config
.set(
"hbase.zookeeper.quorum"
,
"140.143.236.169:2181"
);
config
.set(
"hbase.rootdir"
,
"hdfs://140.143.236.169:8020/hbase"
);
// 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);
}
}
编译和执行上述程序如下所示。
name: Raju city: Delhi
四、删除数据
【1】Shell 使用
使用 delete 命令,可以在一个表中删除特定单元格。 delete 命令的语法如下:
delete '', '', '', '