package com.xueyoucto.hbasett;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2016-11-23.
*/
public class HBaseUtils {
static Configuration cfg;
static Connection connection;
static {
cfg = HBaseConfiguration.create();
try {
connection = ConnectionFactory.createConnection(cfg);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(cfg.get("hbase.master"));
}
//新建表
public static boolean create(String tableName, String columnFamily)
throws Exception {
HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
if (admin.tableExists(tableName)) {
System.out.println(tableName + " exists!");
return false;
} else {
String[] columnFamilyArray = columnFamily.split(",");
HColumnDescriptor[] hColumnDescriptor = new HColumnDescriptor[columnFamilyArray.length];
for (int i = 0; i < hColumnDescriptor.length; i++) {
hColumnDescriptor[i] = new HColumnDescriptor(columnFamilyArray[i]);
}
HTableDescriptor familyDesc = new HTableDescriptor(TableName.valueOf(tableName));
for (HColumnDescriptor columnDescriptor : hColumnDescriptor) {
familyDesc.addFamily(columnDescriptor);
}
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName), familyDesc);
admin.createTable(tableDesc);
System.out.println(tableName + " create successfully!");
return true;
}
}
//插入数据
public static boolean put(String tablename, String row, String columnFamily,
String qualifier, String data) throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Put put = new Put(Bytes.toBytes(row));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(qualifier),
Bytes.toBytes(data));
table.put(put);
System.out.println("put '" + row + "', '" + columnFamily + ":" + qualifier
+ "', '" + data + "'");
return true;
}
//把result转换成map,方便返回json数据
public static Map resultToMap(Result result) {
Map resMap = new HashMap();
List listCell = result.listCells();
Map tempMap = new HashMap();
String rowname = "";
List familynamelist = new ArrayList();
for (Cell cell : listCell) {
byte[] rowArray = cell.getRowArray();
byte[] familyArray = cell.getFamilyArray();
byte[] qualifierArray = cell.getQualifierArray();
byte[] valueArray = cell.getValueArray();
int rowoffset = cell.getRowOffset();
int familyoffset = cell.getFamilyOffset();
int qualifieroffset = cell.getQualifierOffset();
int valueoffset = cell.getValueOffset();
int rowlength = cell.getRowLength();
int familylength = cell.getFamilyLength();
int qualifierlength = cell.getQualifierLength();
int valuelength = cell.getValueLength();
byte[] temprowarray = new byte[rowlength];
System.arraycopy(rowArray, rowoffset, temprowarray, 0, rowlength);
String temprow= Bytes.toString(temprowarray);
// System.out.println(Bytes.toString(temprowarray));
byte[] tempqulifierarray = new byte[qualifierlength];
System.arraycopy(qualifierArray, qualifieroffset, tempqulifierarray, 0, qualifierlength);
String tempqulifier= Bytes.toString(tempqulifierarray);
// System.out.println(Bytes.toString(tempqulifierarray));
byte[] tempfamilyarray = new byte[familylength];
System.arraycopy(familyArray, familyoffset, tempfamilyarray, 0, familylength);
String tempfamily= Bytes.toString(tempfamilyarray);
// System.out.println(Bytes.toString(tempfamilyarray));
byte[] tempvaluearray = new byte[valuelength];
System.arraycopy(valueArray, valueoffset, tempvaluearray, 0, valuelength);
String tempvalue= Bytes.toString(tempvaluearray);
// System.out.println(Bytes.toString(tempvaluearray));
tempMap.put(tempfamily + ":" + tempqulifier, tempvalue);
// long t= cell.getTimestamp();
// tempMap.put("timestamp",t);
rowname = temprow;
String familyname = tempfamily;
if (familynamelist.indexOf(familyname) < 0) {
familynamelist.add(familyname);
}
}
resMap.put("rowname", rowname);
for (String familyname : familynamelist) {
HashMap tempFilterMap = new HashMap();
for (String key : tempMap.keySet()) {
String[] keyArray = key.split(":");
if(keyArray[0].equals(familyname)){
tempFilterMap.put(keyArray[1],tempMap.get(key));
}
}
resMap.put(familyname, tempFilterMap);
}
return resMap;
}
//查看某行
public static String get(String tablename, String row) throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
System.out.println("Get: " + result);
return resultToMap(result).toString();
}
//查看全表
public static String scan(String tablename) throws Exception {
Table table = connection.getTable(TableName.valueOf(tablename));
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
List |
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletCreate",urlPatterns = "/HbaseServletCreate")
public class ServletCreate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
String tname = request.getParameter("tablename");
//多个familyName 用逗号分隔
String familyName = request.getParameter("familyname");
String msg = "-";
try{
if(HBaseUtils.create(tname,familyName)){
msg="success";
}else{
msg="error";
}
}catch (Exception e){
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletPut",urlPatterns = "/HbaseServletPut")
public class ServletPut extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String row = request.getParameter("row");
String familyName = request.getParameter("familyname");
String qualifier = request.getParameter("qualifier");
String data = request.getParameter("data");
String msg = "-";
try{
if(HBaseUtils.put(tname,row,familyName,qualifier,data)){
msg="success";
}else{
msg="error";
}
}catch (Exception e){
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletGet",urlPatterns = "/HBaseServletGet")
public class ServletGet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String row = request.getParameter("row");
String msg = "-";
try{
msg=HBaseUtils.get(tname,row);
}catch (Exception e){
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletScan",urlPatterns = "/HBaseServletScan")
public class ServletScan extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String msg = "-";
try{
msg = HBaseUtils.scan(tname);
}catch (Exception e){
e.printStackTrace();
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletDeleteQualifier",urlPatterns = "/HBaseServlertDeleteQualifier")
public class ServletDeleteQualifier extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String rowName = request.getParameter("rowname");
String columnfamilynameName = request.getParameter("columnfamilyname");
String qualifierName = request.getParameter("qualifiername");
String msg = "-";
if(HBaseUtils.deleteQualifier(tname,rowName,columnfamilynameName,qualifierName)){
msg = "success";
}else{
msg = "error";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletDeleteRow",urlPatterns = "/HbaseServletDeleteRow")
public class ServletDeleteRow extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String rowName = request.getParameter("rowname");
String msg = "-";
if(HBaseUtils.deleteRow(tname, rowName)){
msg = "success";
}else{
msg = "error";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletDeleteColumn",urlPatterns = "/HbaseServletDeleteColumn")
public class ServletDeleteColumn extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String columnName = request.getParameter("columnname");
String msg = "-";
if(HBaseUtils.deleteColumnFamily(tname, columnName)){
msg = "success";
}else{
msg = "error";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
package com.xueyoucto.hbasett;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2016-11-23.
*/
@WebServlet(name = "ServletDelete",urlPatterns = "/HbaseServletDelete")
public class ServletDelete extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String tname = request.getParameter("tablename");
String msg = "-";
if(HBaseUtils.delete(tname)){
msg = "success";
}else{
msg = "error";
}
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(msg);
out.flush();
out.close();
}
}
下面是HBase java API 查询和分页的文章地址:
HBase java API 使用实例(二)——查询、分页