HBase Java API使用操作例子
2014-12-13 17:40
525 查看
通过对HBase API的使用,下面例子举例了常见对HBase的操作,如下所示:
package net.linuxidc.www;
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.HBaseAdmin;
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;
import org.apache.hadoop.hbase.io.BatchUpdate;
public class HBaseDBDao {
//定义配置对象HBaseConfiguration
static HBaseConfiguration cfg =null;
static {
Configuration configuration = new Configuration();
cfg = new HBaseConfiguration(configuration);
}
//创建一张表,指定表名,列族
public static void createTable(String tableName,String columnFarily)throws Exception{
HBaseAdmin admin = new HBaseAdmin(cfg);
if(admin.tableExists(tableName)){
System.out.println(tableName+"不存在!");
System.exit(0);
}else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFarily+":"));
System.out.println("创建表成功!");
}
}
//添加数据,通过HTable。和BatchUpdate为已经存在的表添加数据data
public static void addData(String tableName,String row,String columnFamily,String column,String data)throws Exception{
HTable table = new HTable(cfg,tableName);
BatchUpdate update = new BatchUpdate(row);
update.put(columnFamily+":"+column, data.getBytes());
table.commit(update);
System.out.println("添加成功!");
}
//显示所有数据,通过HTable Scan类获取已有表的信息
public static void getAllData(String tableName)throws Exception{
HTable table = new HTable(cfg,tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for(Result r:rs){
for(KeyValue kv:r.raw()){
System.out.println(new String(kv.getColumn())+new String(kv.getValue()));
}
}
}
//测试???数
public static void main(String[] args){
try{
String tableName = "student";
HBaseDBDao.createTable(tableName, "c1");
HBaseDBDao.addData(tableName, "row1", "c1", "1", "this is row 1 column c1:c1");
HBaseDBDao.getAllData(tableName);
}catch(Exception e){
e.printStackTrace();
}
}
}
package net.linuxidc.www;
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.HBaseAdmin;
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;
import org.apache.hadoop.hbase.io.BatchUpdate;
public class HBaseDBDao {
//定义配置对象HBaseConfiguration
static HBaseConfiguration cfg =null;
static {
Configuration configuration = new Configuration();
cfg = new HBaseConfiguration(configuration);
}
//创建一张表,指定表名,列族
public static void createTable(String tableName,String columnFarily)throws Exception{
HBaseAdmin admin = new HBaseAdmin(cfg);
if(admin.tableExists(tableName)){
System.out.println(tableName+"不存在!");
System.exit(0);
}else{
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
tableDesc.addFamily(new HColumnDescriptor(columnFarily+":"));
System.out.println("创建表成功!");
}
}
//添加数据,通过HTable。和BatchUpdate为已经存在的表添加数据data
public static void addData(String tableName,String row,String columnFamily,String column,String data)throws Exception{
HTable table = new HTable(cfg,tableName);
BatchUpdate update = new BatchUpdate(row);
update.put(columnFamily+":"+column, data.getBytes());
table.commit(update);
System.out.println("添加成功!");
}
//显示所有数据,通过HTable Scan类获取已有表的信息
public static void getAllData(String tableName)throws Exception{
HTable table = new HTable(cfg,tableName);
Scan scan = new Scan();
ResultScanner rs = table.getScanner(scan);
for(Result r:rs){
for(KeyValue kv:r.raw()){
System.out.println(new String(kv.getColumn())+new String(kv.getValue()));
}
}
}
//测试???数
public static void main(String[] args){
try{
String tableName = "student";
HBaseDBDao.createTable(tableName, "c1");
HBaseDBDao.addData(tableName, "row1", "c1", "1", "this is row 1 column c1:c1");
HBaseDBDao.getAllData(tableName);
}catch(Exception e){
e.printStackTrace();
}
}
}
相关文章推荐
- HBase Java API使用操作例子
- eclipse下使用java api 进行hbase的常用的操作所需的jar包,以及如何查看java build path下的jar是否使用
- 使用java api操作hbase数据库
- 使用Java Api 对HBase进行简单操作
- 使用Java API对HBase进行CRUD操作
- 在Hbase Endpoint Coprocessor中使用coprocessorProxy操作例子与问题解析
- HBase Java API使用操作例子
- 使用Java API对HBase进行操作
- 使用Java Api 对HBase进行简单操作
- 使用Jython操作HBase
- 在eclipse3.7中配置hadoop1.0.1插件 Hadoop 源码分析 使用java api操作Hadoop文件
- 使用C++(通过Thrift)访问/操作/读写Hbase
- 使用C++(通过Thrift)访问/操作/读写Hbase
- 使用java api操作Hadoop文件
- 使用java api操作Hadoop文件
- 使用java api操作HDFS文件
- 使用C++(通过Thrift)访问/操作/读写Hbase
- HBase使用例子
- Spring的JdbcTemplate使用update或insert操作的三种使用例子
- 在HBase里使用MapReduce例子