Java 连接hbase 进行简单操作

package com.paic.mario.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
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.util.Bytes;


public class  HbaseProbeJava{


private static Configuration conf = null;


static {
conf = HBaseConfiguration.create();
}


@SuppressWarnings("resource")
// one data
public static String getOneRow(String tableName, String row) throws Exception {
             //System.out.println("getOneRow--------1 "); 
String returnStr = "";
        try {
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
for (KeyValue rowKV : result.raw()) {
returnStr += "RowName:" + new String(rowKV.getRow()) +" ,";
returnStr += "Timestamp:" + rowKV.getTimestamp() +" ,";
returnStr += "FamilyName:" + new String(rowKV.getFamily()) +" ,";
returnStr += "ColomnName:" + new String(rowKV.getQualifier()) +" ,";
returnStr += "Value:" + new String(rowKV.getValue());
}
} catch (Exception e) {
writeFile("HTable data error.pls check java");
e.printStackTrace();
}
return returnStr;
}

@SuppressWarnings("resource")
// oneColumn data
public static String getOneColumn(String tableName, String FamilyColumn) throws Exception {
             //System.out.println("getOneColumn--------1 "); 
String returnStr = "";
//         try {
// HTable table = new HTable(conf, tableName);
// Get get = new Get(Bytes.toBytes(row));
// Result result = table.get(get);
// for (KeyValue rowKV : result.raw()) {
// returnStr += "RowName:" + new String(rowKV.getRow()) +" ,";
// returnStr += "Timestamp:" + rowKV.getTimestamp() +" ,";
// returnStr += "FamilyName:" + new String(rowKV.getFamily()) +" ,";
// returnStr += "ColomnName:" + new String(rowKV.getQualifier()) +" ,";
// returnStr += "Value:" + new String(rowKV.getValue());
// }
// } catch (Exception e) {
// writeFile("HTable data error.pls check java");
// e.printStackTrace();
// }
return returnStr;
}

//all data
@SuppressWarnings("resource")
public static String getAllRows(String tableName) throws Exception {
            String returnStr = "";
            try {
HTable table = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
//System.out.println("getAllRows--------------1 ");


for (Result result : results) {
//System.out.println("getAllRows--------------2 ");
for (KeyValue rowKV : result.raw()) {
//System.out.println("getAllRows--------------3 ");
returnStr += "RowName:" + new String(rowKV.getRow()) +" ,";
returnStr += "Timestamp:" + rowKV.getTimestamp() +" ,";
returnStr += "FamilyName:" + new String(rowKV.getFamily()) +" ,";
returnStr += "ColomnName:" + new String(rowKV.getQualifier()) +" ,";
returnStr += "Value:" + new String(rowKV.getValue())+" \n";
}
}
} catch (Exception e) {
writeFile("HTable data error.pls check java");
e.printStackTrace();
}
//System.out.println("returnStr--------------"+returnStr);
return returnStr;
}

// main
public static void main(String[] args) {
 //String argpar = "10.20.20.176:2181==username==password==scores:ztx:xxxx"
 String argpar = args[0];
     String[] argarray = argpar.split("==");


     String host_port = argarray[0]; 
     String username = argarray[1];
     String password = argarray[2];
     String sqlstatement = argarray[3];
     
     String[] host_portarray = host_port.split(":");
 conf.set("hbase.zookeeper.quorum", host_portarray[0]);
 conf.set("hbase.zookeeper.port", host_portarray[1]);

              //System.out.println("arg--------3 "+argpar);  
     String[] sqlstatementarray = sqlstatement.split(":");
 try {
   String saveReturnStr = "";
   if(sqlstatementarray.length == 1){
                        //System.out.println("arg--------4 "+argpar); 
    saveReturnStr = getAllRows(sqlstatementarray[0]);
   }else if(sqlstatementarray.length == 2){
    //System.out.println("arg--------5 "+argpar); 
    saveReturnStr = getOneRow(sqlstatementarray[0], sqlstatementarray[1]);
   }
   writeFile(saveReturnStr);
   
 } catch (Exception e) {
   writeFile("get data error.pls check java");
e.printStackTrace();
 }
}


public static void writeFile(String content){
       FileOutputStream out = null;
       File file;
      try {
          String rootFile = ".";
          file = new File(rootFile);
          if (!file.exists()) {
                  file.mkdirs();
          }
          
          //windows
          //File fileDat = new File(rootFile + "\\myemailfile.txt");
          //linux
          File fileDat = new File("myemailfileforHbase.txt");

          out = new FileOutputStream(fileDat);
          byte[] contentInBytes = content.getBytes();
          out.write(contentInBytes);
          out.flush();
          out.close();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              if (out != null) {
                  out.close();
              }
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
   }

}

 看了很多文章 都说需要加载hbase-site.xml 文件才能连,其实是不用,只要你的客户端包版本 与 连的服务端版本 一致就可以了,这个和Java连hive 非常相似

这里我就不晒出jar包了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值