第一个java hbase程序

 

package test;

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

public class HBaseTestCase {

 static Configuration cfg=null;
 static{
  cfg=HBaseConfiguration.create();
  cfg.setStrings("hbase.zookeeper.quorum", "192.168.211.129");
 }
 
 public static void createTable(String tableName,String columnFamily) throws Exception{
  HBaseAdmin admin=new HBaseAdmin(cfg);
  if(admin.tableExists(tableName)){
   System.out.println("table Exists");
   System.exit(0);
  }
  else{
   HTableDescriptor tableDesc=new HTableDescriptor(tableName);
   tableDesc.addFamily(new HColumnDescriptor(columnFamily));
   admin.createTable(tableDesc);
   System.out.println("create table success");
  }
 }
 
 public static void addData(String tableName,String row,
   String columnFamily,String column,String data) throws Exception{
  HTable table=new HTable(cfg,tableName);
  Put p=new Put(Bytes.toBytes(row));
  p.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(data));
  table.put(p);
 }
 
 public static void getData(String tableName,String row,
   String columnFamily,String column) throws IOException{
  HTable table=new HTable(cfg,tableName);
  Get g=new Get(Bytes.toBytes(row));
  Result rowResult=table.get(g);
  byte[] ret=rowResult.getValue(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
  
 }
 public static void main(String[] args) throws Exception {
  createTable("mydb","info");
  addData("mydb", "firstrow", "info", "c1", "hellodata");
 }
}
让hbase运行在Standalone 模式,该客户端程序运行在window7的eclipse中。

我的配置:window7中 C:\windows\system32\drivers\etc\hosts 文件追加一行 192.168.211.129  namenode(我的虚拟机地址)

ubuntu linux中 /etc/hosts 文件中增加一行192.168.211.129  namenode

hbase的conf/regionservers文件中的localhost替换成namenode

hbase的conf/hbase-site.xml增加下面的配置

<property>
    <name>hbase.rootdir</name>
    <value>file:///home/ming/hbase/hbasedata</value>
  </property>

执行 bin/start-hbase.sh

然后在eclipse中运行上面的程序

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值