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();
}
}
}
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包了