Hbase API使用(二)
来源:互联网 发布:南宁扶贫云数据平台 编辑:程序博客网 时间:2024/06/01 08:29
1.使用Java API插入数据
可以使用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("columnname"),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(); // 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(); }}
2.使用Java API更新数据
使用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(); // 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(); }}3.使用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); }}
4.使用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();下面给出的是从HBase表中删除的数据的完整程序。
import java.io.IOException;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....."); }}
5.使用Java API扫描
使用Java API扫描整个表的数据的完整程序如下:
import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;public class ScanTable{ public static void main(String args[]) throws IOException{ // Instantiating Configuration class Configuration config = HBaseConfiguration.create(); // Instantiating HTable class HTable table = new HTable(config, "emp"); // Instantiating the Scan class Scan scan = new Scan(); // Scanning the required columns scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name")); scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("city")); // Getting the scan result ResultScanner scanner = table.getScanner(scan); // Reading values from scan result for (Result result = scanner.next(); result != null; result = Scanner.next()) System.out.println("Found row : " + result); //closing the scanner scanner.close(); }}示例代码:
package cn.itcast.hbase;import java.io.IOException;import java.io.InputStream;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.MasterNotRunningException;import org.apache.hadoop.hbase.ZooKeeperConnectionException;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.HTableInterface;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;import org.apache.hadoop.hbase.zookeeper.HQuorumPeer;import org.junit.Before;import org.junit.Test;public class HbaseDemo {private static Configuration conf = null;@Before/*<property><name>hbase.rootdir</name><value>hdfs://master:9000/hbase</value></property>*//*<property><name>hbase.zookeeper.quorum</name><value>master:2181,slave1:2181,slave2:2181,slave3:2181</value></property>*/ // 初始化public static void init(){conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://master:9000/hbase");conf.addResource("hbase-site.xml");conf.set("hbase.zookeeper.property.clientPort","2181");//使用eclipse时必须添加这个,否则无法定位conf.set("hbase.zookeeper.quorum", "master:2181,slave1:2181,slave2:2181,slave3:2181");}// 创建一个表public static void create(String tableName, String columnFamily) {HBaseAdmin admin;try {admin = new HBaseAdmin(conf );if (admin.tableExists(tableName)) {System.out.println("table exists!");}else{HTableDescriptor tableDesc = new HTableDescriptor(tableName);tableDesc.addFamily(new HColumnDescriptor(columnFamily));admin.createTable(tableDesc);System.out.println("create table success!");}} catch (MasterNotRunningException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ZooKeeperConnectionException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 添加记录public void testPut() throws Exception{HTable table = new HTable(conf, "user");/* * * add(byte[] family, byte[] qualifier, byte[] value)将指定的列和对应的值添加到Put实例中add(byte[] family, byte[] qualifier, long ts, byte[] value)将指定的列和对应的值及时间戳添加到Put实例中***/Put put = new Put(Bytes.toBytes("rk0001"));put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));table.put(put);table.close();}//读取一条记录public void testGet() throws Exception{HTable table = new HTable(conf, "user");Get get = new Get(Bytes.toBytes("rk0001"));//get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));get.setMaxVersions(5);Result result = table.get(get);//result.getValue(family, qualifier)for(KeyValue kv : result.list()){String family = new String(kv.getFamily());System.out.println(family);String qualifier = new String(kv.getQualifier());System.out.println(qualifier);System.out.println(new String(kv.getValue()));}table.close();}// 显示所有数据public void testScan() throws Exception{HTablePool pool = new HTablePool(conf, 10);HTableInterface table = pool.getTable("user");Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));scan.addFamily(Bytes.toBytes("info"));ResultScanner scanner = table.getScanner(scan);for(Result r : scanner){byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));System.out.println(new String(value));}pool.close();}// 删除表public void testDrop() throws Exception{HBaseAdmin admin = new HBaseAdmin(conf);if(admin.tableExists("user")){try { admin.disableTable("user"); admin.deleteTable("user");} catch (IOException e) { e.printStackTrace(); System.out.println("Delete "+"user"+" 失败");}}System.out.println("Delete "+"user"+" 成功");admin.close();}// 删除某一列public void testDel() throws Exception{HTable table = new HTable(conf, "user");Delete del = new Delete(Bytes.toBytes("rk0001"));del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));table.delete(del);table.close();}public static void main(String[] args) throws Exception {init();HBaseAdmin admin = new HBaseAdmin(conf);HTableDescriptor td = new HTableDescriptor("user");HColumnDescriptor cd = new HColumnDescriptor("info");cd.setMaxVersions(10);td.addFamily(cd);admin.createTable(td);admin.close();}}
- Hbase API使用(二)
- Hbase API使用(一)
- [hbase]使用HBase的Java API查询HBase(2)
- JAVA API 操作HBASE(二)
- HBase java API 使用实例(二)——查询、分页
- 使用传统hbase的api创建hbase表(scala)
- hbase api 使用
- Hbase的API使用
- HBase客户端API使用
- HBase客户端API使用
- Hbase Java API使用
- HBase API使用
- Hbase API使用总结
- HBase--客户端API(基础知识)二
- hbase学习教程(二):HBase容错性和Hbase使用场景、Hbase读写过程详解
- HBase概念学习(二)JAVA API操作概览
- Java API 与HBase交互实例(二)
- hbase学习教程(三):HBase优化技巧、HBase基本命令、使用Java API对HBase服务器进行操作
- Android源码笔记——Camera系统架构
- Failed to resolve:com.android.support:support-annotation:26.0.2
- C#个人重构之按照日期查询
- hdu 1879(并查集+克鲁斯卡尔)
- Egyptian Fractions (HARD version) UVA
- Hbase API使用(二)
- css中float属性
- Java 反射——把表单值映射到 model 中
- 刷题笔记:C/C++专项练习1
- POJ2264---Advanced Fruits(dp,LCS变着花样记录路径,POJ2250的升级版)
- 与或树的盲目搜索和启发式搜索
- NOIP2017
- bat脚本常见命令意义
- 分别在windows和linux上编译cjson为动态链接库,用在loadrunner中解析json