Hbase servlet

本文详细介绍如何在Maven WAR项目中集成HBase和Servlet,包括pom配置、web.xml设置及Servlet代码实现,演示了HBase表的创建、读写操作。

1.创建maven war 项目

2.pom配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mulhyac</groupId>
    <artifactId>learn-hbase</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <repositories>
        <repository>
            <id>cloudera</id>
            <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.2.0-cdh5.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>2.6.0-mr1-cdh5.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b01</version>
        </dependency>
    </dependencies>
</project>

3.web.xml配置

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>qyq3</display-name>
	<servlet>
		<servlet-name>HBaseServlet</servlet-name>
		<servlet-class>com.mulhyac.learn.hbase.servlet.HBaseServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HBaseServlet</servlet-name>
		<url-pattern>/HBaseServlet</url-pattern>
	</servlet-mapping>
</web-app>

4.servlet代码

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.*;
import org.apache.hadoop.hbase.util.Bytes;

import javax.servlet.ServletException;
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-22. 
 */
public class HBaseServlet 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 {
        System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        String tableName = "test";  
        String columnFamily = "cf";  
        try {  
  
            if (true == HBaseServlet.delete(tableName)) {
                System.out.println("Delete Table " + tableName + " success!");  
  
            }  
            System.out.println("************start create table**********");
            HBaseServlet.create(tableName, columnFamily);
            HBaseServlet.put(tableName, "row1", columnFamily, "column1",
                    "data1");
            HBaseServlet.put(tableName, "row2", columnFamily, "column2",
                    "data2");
            HBaseServlet.put(tableName, "row3", columnFamily, "column3",
                    "data3");
            HBaseServlet.put(tableName, "row4", columnFamily, "column4",
                    "data4");
            HBaseServlet.put(tableName, "row5", columnFamily, "column5",
                    "data5");

            HBaseServlet.get(tableName, "row1");

            HBaseServlet.scan(tableName);
  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
  
        response.setCharacterEncoding("utf-8");  
        response.setContentType("text/html;charset=utf-8");  
        PrintWriter out = response.getWriter();  
        out.println("hello world");  
        out.flush();  
        out.close();  
    }  
  
    static Configuration cfg;  
  
    static {  
        cfg = HBaseConfiguration.create();  
        System.out.println(cfg.get("hbase.master"));  
    }  
  
    public static void create(String tableName, String columnFamily)  
            throws Exception {  
        HBaseAdmin admin = new HBaseAdmin(cfg);  
        if (admin.tableExists(tableName)) {  
            System.out.println(tableName + " exists!");  
        } else {  
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);  
            tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
            admin.createTable(tableDesc);  
            System.out.println(tableName + " create successfully!");  
        }  
    }  
  
    public static void put(String tablename, String row, String columnFamily,  
                           String column, String data) throws Exception {  
  
        HTable table = new HTable(cfg, tablename);  
        Put put = new Put(Bytes.toBytes(row));  
  
        put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),  
                Bytes.toBytes(data));  
  
        table.put(put);  
  
        System.out.println("put '" + row + "', '" + columnFamily + ":" + column  
                + "', '" + data + "'");  
  
    }  
  
    public static void get(String tablename, String row) throws Exception {  
        HTable table = new HTable(cfg, tablename);  
        Get get = new Get(Bytes.toBytes(row));  
        Result result = table.get(get);  
        System.out.println("Get: " + result);  
    }  
  
    public static void scan(String tableName) throws Exception {  
  
        HTable table = new HTable(cfg, tableName);  
        Scan s = new Scan();  
        ResultScanner rs = table.getScanner(s);  
  
        for (Result r : rs) {  
            System.out.println("Scan: " + r);  
  
        }  
    }  
  
    public static boolean delete(String tableName) throws IOException {  
  
        HBaseAdmin admin = new HBaseAdmin(cfg);  
        if (admin.tableExists(tableName)) {  
            try {  
                admin.disableTable(tableName);  
                admin.deleteTable(tableName);  
            } catch (Exception e) {  
                e.printStackTrace();  
                return false;  
            }  
        }  
        return true;  
  
    }  
} 

 

 

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值