HBase java API 使用实例(一)——增加、修改、删除

HBase java API 使用实例(一)——增加、修改、删除

上一篇文章(http://blog.csdn.net/wild46cat/article/details/53288537)已经能够使用java api对HBase进行增加、删除、修改了,那么这篇文章主要是讲的是什么呢?这篇文章主要是对上一篇文章中的代码的重构,首先把一些已经废弃的方法进行了替换,然后是对生产环境的一个测试,最后在返回数据时,已经把数据转换成类似JSON的格式了。这样重构之后,在使用时非常方便。但是对于特殊的一些业务可能需要再次重构(读者可以在这个基础上修改,最后我会附上代码)。
注意:
1、这篇文章是第一部分,讲述的是增加、修改、删除。对于表的查询,等到下一篇文章再和大家分享。
2、因为已经有前一篇文章的铺垫,所以这里略过了使用java api访问HBase的条件(需要哪些jar包,配置文件等),如果多余这些前提条件还不清楚,建议看一下这篇文章:http://blog.csdn.net/wild46cat/article/details/53288537。

好啦,下面上货:
1、HBaseUtils.java
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> resList = new ArrayList>();
        for (Result r : rs) {
            Map tempmap = resultToMap(r);
            resList.add(tempmap);
        }
        return resList.toString();
    }

    //删除表
    public static boolean delete(String tableName) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        if (admin.tableExists(tableName)) {
            try {
                admin.disableTable(tableName);
                admin.deleteTable(tableName);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    //删除ColumnFamily
    public static boolean deleteColumnFamily(String tableName,String columnFamilyName) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        if (admin.tableExists(tableName)) {
            try {
                admin.deleteColumn(tableName,columnFamilyName);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
    //删除row
    public static boolean deleteRow(String tableName,String rowName) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        Table table = connection.getTable(TableName.valueOf(tableName));
        if (admin.tableExists(tableName)) {
            try {
                Delete delete = new Delete(rowName.getBytes());
                table.delete(delete);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    //删除qualifier
    public static boolean deleteQualifier(String tableName,String rowName,String columnFamilyName,String qualifierName) throws IOException {
        HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
        Table table = connection.getTable(TableName.valueOf(tableName));
        if (admin.tableExists(tableName)) {
            try {
                Delete delete = new Delete(rowName.getBytes());
                delete.addColumns(columnFamilyName.getBytes(),qualifierName.getBytes());
                table.delete(delete);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }
}

注释中已经写得比较清楚了,这个类可以用作工具类。下面的servlet是这个工具类的测试。

2、ServletCreate.java  ==>测试新建表
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();
    }
}

下面是测试截图:

查看表是否存在:
HBase java API 使用实例(一)——增加、修改、删除_第1张图片

3、ServletPut.java ==>测试插入数据
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();
    }
}

4、ServletGet.java ==>测试查看数据(get方式)
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();
    }
}

5、ServletScan.java ==>测试查看数据(scan方式)
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();
    }
}

这三个方法一起测试:
首先添加:




下面测试查询(get方式):

测试查询(scan方式):


6、ServletDeleteQualifier.java ==>测试删除修饰符
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();
    }
}

测试删除article 中的content


删除后再次查询:


7、ServletDeleteRow.java ==>测试删除row
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();
    }
}

测试删除test表中的r1行

再次查看表test


8、ServletDeleteColumn.java ==>测试删除columnfamily
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();
    }
}

测试删除test表中的article(article是创建表时的columnfamily)。

查看表的结构:
HBase java API 使用实例(一)——增加、修改、删除_第2张图片

9、ServletDelete.java ==>测试删除表
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();
    }
}

测试删除test表:

查看表是否存在:
HBase java API 使用实例(一)——增加、修改、删除_第3张图片

这样,基本的添加、修改、删除就能够使用了。

下面附上源码,不要你的积分噢!!!:
http://download.csdn.net/detail/wild46cat/9691298

下面是HBase java API 查询和分页的文章地址:

HBase java API 使用实例(二)——查询、分页


你可能感兴趣的:(linux,hbase)