HBase的java操作源代码

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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.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;

public class HBaseDemo001 {
 public static void main(String[] args) throws Exception{
  Configuration conf = HBaseConfiguration.create();
  conf.set("hbase.rootdir", "hdfs://chaigy:9000/hbase");
  //使用eclipse时必须添加这个,否则无法定位
  conf.set("hbase.zookeeper.quorum", "chaigy");
  HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
  //创建表
  if(!hBaseAdmin.isTableAvailable("test".getBytes())){
   createTable(hBaseAdmin);
  }else{
   //deleteTable(hBaseAdmin);
  }
  //增加数据
  HTable hTable = new HTable(conf, "test");
 
  //putData(hTable);
  //查询数据
  //findAll(hTable);
  //修改数据
  //updateData(hTable);
  //查询单挑数据
  Get get = new Get("xiaoming".getBytes());
  get.addColumn("testaddress".getBytes(), "city".getBytes());
  Result result = hTable.get(get);
  System.out.println(new String (result.getValue("testaddress".getBytes(), "city".getBytes())));
 }

 private static void updateData(HTable hTable) throws IOException {
  Put put= new Put("xiaoming".getBytes());
  put.add("testaddress".getBytes(),"city".getBytes(), "shanghai".getBytes());
  hTable.put(put);
 }

 private static void findAll(HTable hTable) throws IOException {
  Scan scan = new Scan();
  ResultScanner result = hTable.getScanner(scan);
  for (Result result2 : result) {
   byte[] value = result2.getValue("testaddress".getBytes(), "city".getBytes());
   System.out.println(result2+"****"+new String(value));
  }
 }

 private static void putData(HTable hTable) throws IOException {
  List<Put> list = new ArrayList<Put>();
  Put puts =null;
  for(int i=0;i<5;i++){
   puts= new Put(("xiaoming"+i).getBytes());
   puts.add("testaddress".getBytes(), ("city"+i).getBytes(), ("beijing"+i).getBytes());
   list.add(puts);
  }
  hTable.put(list);
 }

 private static void deleteTable(HBaseAdmin hBaseAdmin) throws IOException {
  System.out.println("0000000000");
  //删除表
  hBaseAdmin.disableTable("test".getBytes());
  hBaseAdmin.deleteTable("test".getBytes());
 }

 private static void createTable(HBaseAdmin hBaseAdmin) throws IOException {
  HTableDescriptor desc = new HTableDescriptor("test");
  HColumnDescriptor family = new HColumnDescriptor("testinfo".getBytes());
  HColumnDescriptor family1 = new HColumnDescriptor("testaddress".getBytes());
  desc.addFamily(family);
  desc.addFamily(family1);
  hBaseAdmin.createTable(desc);
 }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值