java 访问部署在hdfs上hbase数据库
2013-12-23 21:04
330 查看
import java.io.IOException; 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.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.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; public class HbaseConnetion { Configuration cfg; HBaseAdmin admin; //初始化建立连接 public HbaseConnetion() throws MasterNotRunningException, ZooKeeperConnectionException{ cfg=HBaseConfiguration.create(); admin=new HBaseAdmin(cfg); } //关闭数据库连接 public void close() throws IOException{ if (admin != null) admin.close(); } //判断表是否存在 public boolean isTableExist(String tablename) throws IOException{ if(admin.tableExists(tablename)){ return true; } else{ System.out.println("Table not exists"); return false; } } //新建一个表 public void creat(String tablename,String[] culumnFamlilies) throws IOException{ if(admin==null) { System.out.println("Database not Connected"); return; } if(isTableExist(tablename)) return; HTableDescriptor desc =new HTableDescriptor(tablename); for(int i=0;i<culumnFamlilies.length;i++){ desc.addFamily(new HColumnDescriptor(culumnFamlilies[i])); } admin.createTable(desc); } //添加一条记录 public void put(String tablename,String row_key,String family,String value)throws Exception{ if (!isTableExist(tablename)) return; HTable table=new HTable(cfg, Bytes.toBytes(tablename)); Put put=new Put(Bytes.toBytes(row_key)); put.add(Bytes.toBytes(family),null,Bytes.toBytes(value)); table.put(put); table.close(); } //添加一条记录 public void put(String tablename,String row_key,String family,String column,String value)throws Exception{ if (!isTableExist(tablename)) return; HTable table=new HTable(cfg, Bytes.toBytes(tablename)); Put put=new Put(Bytes.toBytes(row_key)); put.add(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value)); table.put(put); table.close(); } //按照行键查询记录 public Result get(String tablename ,String row_key) throws IOException{ if(!isTableExist(tablename)) return null; Get get=new Get(Bytes.toBytes(row_key)); HTable table=new HTable(cfg, tablename); Result result=table.get(get); table.close(); System.out.println(result); return result; } //按行键删除记录 public boolean deleteRow(String tablename,String row_key) throws IOException{ if(!isTableExist(tablename)) return false; HTable table=new HTable(cfg,Bytes.toBytes(tablename)); Delete delete=new Delete(Bytes.toBytes(row_key)); table.delete(delete); table.close(); return true; } //删除table public boolean deleteTable(String tablename) throws IOException{ if(!isTableExist(tablename)) return false; admin.enableTable(tablename); admin.deleteTable(tablename); return true; } //显示所有数据 public void scan(String tablename) throws IOException{ if(!isTableExist(tablename)) return; Scan scan=new Scan(); HTable table=new HTable(cfg, tablename); ResultScanner rs=table.getScanner(scan); if(rs==null){System.out.println("No result");} for(Result r:rs){ System.out.println("Scan: "+r); } table.close(); } //测试 public static void main(String[] args) throws Exception{ HbaseConnetion hbaseConnetion=new HbaseConnetion(); hbaseConnetion.put("teachers", "no1", "name", "famlilyName", "bai"); hbaseConnetion.put("teachers", "no1", "age", "24"); hbaseConnetion.get("teachers", "no1"); hbaseConnetion.close(); } }
相关文章推荐
- Java 访问Hbase数据库
- windows访问Hbase数据库的配置
- java访问数据库
- HBase数据库访问封装
- 使用Hadoop的JAVA API远程访问HDFS
- 通过本地的eclipse中的java访问远程Linux中的HDFS需要注意的问题.
- 客串 java 开发:数据库访问的流程简化
- java访问数据库的代码
- HBase之java api接口调用与mapreduce整合即从hdfs中通过mapreduce来导入数据到hbase中
- 使用SpringMVC开发RestFul风格接口的java-web项目,tomcat部署并客户端访问
- 用Java访问SQL Server 2000数据库
- 【Java】Jdbc 访问数据库
- Java 使用JDBC、DBCP、C3P0访问数据库
- 5、Java数据库访问具体步骤==等待数据库连接池追加
- Java访问Hadoop分布式文件系统HDFS的配置说明
- Java 中访问数据库的步骤?Statement 和 PreparedStatement 之间的区别?
- 自学Java之Java编程(使用JDBC访问数据库)(018day)
- Java访问Sql Server 2008 数据库样例(完整例子)
- 使用hbase为数据库的java工程发布到tomcat