HBase Java API使用操作例子
2014-07-14 12:58
609 查看
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();
}
}
}
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使用操作例子
- 使用用Phoenix的Java api操作HBase
- java API 操作 hbase 数据库(这不是简单的例子,是可以用于运营系统的高性能源码)
- 使用 Java API 操作 HBase
- 使用svnkit api,纯java操作svn,实现svn提交,更新等操作(修正版)
- java使用jxl,jExcelAPI操作excel代码总结
- java操作redis。jedis使用api
- HBase的JavaAPI使用
- Java代码通过API操作HBase的最佳实践
- 使用java对操作数据库的封装例子
- JAVA操作Hbase基础例子
- JAVA操作Hbase基础例子
- 用JAVA的API操作HBASE
- HBase Java API使用(一)
- 使用EJB3 Java 持久化API来标准化Java的持久化操作
- 使用EJB3 Java 持久化API来标准化Java的持久化操作
- JAVA 使用哈希表操作数据库的例子 Using Hashtables to Store & Extract results from a Database.
- HBase入门篇2-Java操作HBase例子
- Hbase的java_API操作
- 在集群中java 通过调用API操作HBase 0.98