首页 > 其他分享 >模糊查询并且对HBase进行CRUD的操作

模糊查询并且对HBase进行CRUD的操作

时间:2022-11-03 22:13:06浏览次数:47  
标签:szdy String jgqc req CRUD 查询 HBase public jsxqmc

  我这是两次的实验用的一个项目,第二次的实验是在第一次上的实验的基础上增加而来      

 

 

pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>demo</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.8.2</junit.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.4</version>
        </dependency>
<!--这个是csv形式的存储-->


        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.4.11</version>
<!--            <scope>provided</scope>-->
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.34</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.34</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-cdi2-se</artifactId>
            <version>2.34</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>3.1.8.Final</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

 

 

pojo

package bean;

public class pojo {
    private String jgqc;
    private String szdy;
    private String jsxqmc;
    private String jgsx;
    private String kjhdlx;

    public String getJgqc() {
        return jgqc;
    }

    public void setJgqc(String jgqc) {
        this.jgqc = jgqc;
    }

    public String getSzdy() {
        return szdy;
    }

    public void setSzdy(String szdy) {
        this.szdy = szdy;
    }

    public String getJsxqmc() {
        return jsxqmc;
    }

    public void setJsxqmc(String jsxqmc) {
        this.jsxqmc = jsxqmc;
    }

    public String getJgsx() {
        return jgsx;
    }

    public void setJgsx(String jgsx) {
        this.jgsx = jgsx;
    }

    public String getKjhdlx() {
        return kjhdlx;
    }

    public void setKjhdlx(String kjhdlx) {
        this.kjhdlx = kjhdlx;
    }

    public pojo() {
    }

    public pojo(String jgqc, String szdy, String jsxqmc, String jgsx, String kjhdlx){
        this.jgqc = jgqc;
        this.szdy = szdy;
        this.jsxqmc = jsxqmc;
        this.jgsx = jgsx;
        this.kjhdlx = kjhdlx;
    }

    @Override
    public String toString() {
        return "pojo{" +
                "jgqc='" + jgqc + '\'' +
                ", szdy='" + szdy + '\'' +
                ", jsxqmc='" + jsxqmc + '\'' +
                ", jgsx='" + jgsx + '\'' +
                ", kjhdlx='" + kjhdlx + '\'' +
                '}';
    }
}

 

Client(这个是用来创建表的)

package com.example;

import java.io.IOException;

public class Client {
    public static void main(String[] args) throws IOException {
        HbaseCRUD hbaseCRUD = new HbaseCRUD();
        String[] cols=new String[]{"S_No","S_Name","S_Sex","S_Age"};
        //建表
        //hbaseCRUD.createTable("student",cols);
        //插入
        /*hbaseCRUD.addRecord("student","s2020001",cols,new String[]{"2020001","Zhangsan","male","23"});
        hbaseCRUD.addRecord("student","s2020002",cols,new String[]{"2020002","Mary","female","22"});
        hbaseCRUD.addRecord("student","s2020003",cols,new String[]{"2020003","Lisi","male","24"});*/
        //查询
//      hbaseCRUD.scanColumn("student","S_Name");
        /*//修改
        hbaseCRUD.modifyData("student","2015001","S_Age","22");
        hbaseCRUD.scanColumn("student","S_Age");
        //删除
        hbaseCRUD.deleteRow("student","s001");
        hbaseCRUD.deleteRow("student","s002");
        hbaseCRUD.deleteRow("student","s003");*/
       // hbaseCRUD.scanColumn("student","S_No");
        hbaseCRUD.close();
        //System.out.println("记得一键三连~");
    }
}

 

HbaseCRUD

package com.example;


import bean.pojo;
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.util.ArrayList;
import java.util.List;

import java.io.IOException;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Result;

public class HbaseCRUD {
    private static Configuration configuration;
    private static Connection connection;
    private static Admin admin;

    public HbaseCRUD() {
        init();
    }

    /**
     * 建立连接
     */
    public void init(){
        configuration=HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","hadoop1,hadoop2,hadoop3"); // 换成你自己的IP
        configuration.set("hbase.zookeeper.property.clientPort","2181");
        try{
            connection=ConnectionFactory.createConnection(configuration);
            admin=connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 关闭连接
     */
    public void close(){
        try{
            if(admin!=null)
                admin.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 创建表
     * @param myTableName 表名
     * @param colFamily 列族数组
     * @throws IOException
     */
    public void createTable(String myTableName,String[]colFamily)throws IOException{
        TableName tablename = TableName.valueOf(myTableName);
        if(admin.tableExists(tablename)){
            System.out.println("表已存在,删除旧表");
            admin.disableTable(tablename);//使表无效
            admin.deleteTable(tablename);//删表
        }
        HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);
        for(String str:colFamily){  //增加一列
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor); //建表
        System.out.println("建表成功");
    }

    /**
     * 插入数据
     * @param tableName 表名
     * @param rowKey 行键
     * @param fields 列族(或列族:列限定符)
     * @param values  值
     * @throws IOException
     */
    public void addRecord(String tableName,String rowKey,String []fields,String [] values) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        for (int i = 0; i < fields.length; i++) {
            Put put = new Put(Bytes.toBytes(rowKey));
            String [] cols = fields[i].split(":");
            if(cols.length == 1)
                put.addColumn(Bytes.toBytes(cols[0]), Bytes.toBytes(""),Bytes.toBytes(values[i]));
            else
                put.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]),Bytes.toBytes(values[i]));
            table.put(put);
        }
        table.close();
    }

    /**
     * 查询数据
     * @param tableName 表名

     * @throws IOException
     */
    public static List<pojo> scanColumn (String tableName,String canshu) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        ArrayList<pojo> pojos = new ArrayList<>();
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            pojo pojo = new pojo();
            //将数据值放到一个数组中
            List<Cell> cells = result.listCells();
            System.out.println(Bytes.toString(cells.get(1).getValueArray(),cells.get(1).getValueOffset(),cells.get(1).getValueLength()));
            pojo.setJgqc(Bytes.toString(cells.get(1).getValueArray(),cells.get(1).getValueOffset(),cells.get(1).getValueLength()));
            System.out.println(pojo.getJgqc()+" wwwwwwwwwww"+canshu);
            if (pojo.getJgqc().equals(canshu)){
                pojo.setJgsx(Bytes.toString(cells.get(2).getValueArray(),cells.get(2).getValueOffset(),cells.get(2).getValueLength()));
                pojo.setJsxqmc(Bytes.toString(cells.get(3).getValueArray(),cells.get(3).getValueOffset(),cells.get(3).getValueLength()));
                pojo.setKjhdlx(Bytes.toString(cells.get(4).getValueArray(),cells.get(3).getValueOffset(),cells.get(4).getValueLength()));//Bytes.toString防止乱码
                pojo.setSzdy(Bytes.toString(cells.get(5).getValueArray(),cells.get(5).getValueOffset(),cells.get(5).getValueLength()));
                pojos.add(pojo);
            }
            System.out.println(pojo.toString());
        }
        table.close();
        return pojos;
    }

    /**
     * 修改数据
     * @param tableName 表名
     * @param rowKey 行键gqc='111', szdy='
     * @param column 列族(或列族:列限定符)
     * @param value 值
     * @throws IOException
     */
    public void modifyData(String tableName,String rowKey,String column,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        String [] cols = column.split(":");
        if(cols.length==1)
            put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());
        else
            put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());
        table.put(put);
        table.close();
    }

    /**
     * 删除数据
     * @param tableName 表名
     * @param rowKey 行键
     * @throws IOException
     */
    public void deleteRow(String tableName,String rowKey) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Delete delete = new Delete(rowKey.getBytes());
        table.delete(delete);
        table.close();
    }
}

 

dao

package dao;

import bean.pojo;
import com.example.HbaseCRUD;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class dao {

    public void input(String jgqc,String szdy,String jsxqmc,String jgsx,String kjhdlx) throws IOException {
        HbaseCRUD hbaseCRUD = new HbaseCRUD();
        String id=jgqc+szdy+jsxqmc;
        String[] cols=new String[]{"id","jgqc","szdy","jsxqmc","jgsx","kjhdlx"};
        //建表
//        hbaseCRUD.createTable("ceshi",cols);

        hbaseCRUD.addRecord("ceshi",id,cols,new String[]{id,jgqc,szdy,jsxqmc,jgsx,kjhdlx});
//        hbaseCRUD.scanColumn("ceshi","jgqc");
        System.out.println("11111111");

        hbaseCRUD.close();
    }
    public static List<pojo> select1(String jgqc,String szdy,String jsxqmc) throws IOException {
        HbaseCRUD hbaseCRUD = new HbaseCRUD();
        String[] cols=new String[]{"id","jgqc","szdy","jsxqmc","jgsx","kjhdlx"};
        //hbaseCRUD.scanColumn("ceshi","jgqc");
        List<pojo> pojoList=new ArrayList<>();   //需要改一下获取的顺序
        List<pojo> list=new ArrayList<>();
//        System.out.println(jgqc+"2222222222222");
        System.out.println("3");
        System.out.println(jgqc+"sssssssssss");
        pojoList =hbaseCRUD.scanColumn("ceshi",jgqc);
        System.out.println("4"+szdy);

        if (szdy.length()!=0){
            System.out.println("99999");
            for (int i=0;i<pojoList.size();i++){
                pojo sc=null;
                sc=pojoList.get(i);
                if (sc.getSzdy().equals(szdy)){
                    list.add(sc);
                }
            }
            return list;
        }

        return pojoList;



        /*if (szdy!=null&&jsxqmc==null){
            for (int i = 0; i < pojoList.size(); i++) {
                pojo sc=null;
                sc=pojoList.get(i);
                if (sc.getSzdy().equals(szdy)){
                    list.add(sc);
                }
            }
        }else {
            for (int i = 0; i < pojoList.size(); i++) {
                pojo sc=null;
                sc=pojoList.get(i);
                if (sc.getSzdy().equals(szdy)&&sc.getJsxqmc().equals(jsxqmc)){
                    list.add(sc);
                }
            }
        }
        return list;*/
    }
}

 

Servlt

package servlet;

import bean.pojo;
import dao.dao;

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.util.List;

@WebServlet("/r2")
public class Servlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
        return;
    }
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");// 设置字符集,避免乱码
        // 获取jsp界面需要进行的操作,
        String method = req.getParameter("method");
        //System.out.println("============");
        if (method.equals("input"))
        {
            input(req,resp);
        }
        if (method.equals("select1"))
        {
            select1(req,resp);
        }
    }
    private void input(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String jgqc=req.getParameter("jgqc");
        String szdy = req.getParameter("szdy");
        String jsxqmc = req.getParameter("jsxqmc");
        String jgsx = req.getParameter("jgsx");
        String kjhdlx = req.getParameter("kjhdlx");
//        System.out.println(jgqc+"    "+szdy+"      "+jsxqmc+"      "+jgsx+"      "+kjhdlx+"servlet");
        dao userdao = new dao();
        userdao.input(jgqc,szdy,jsxqmc,jgsx,kjhdlx);
        req.getRequestDispatcher("input.jsp").forward(req,resp);
    }
    private void select1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String jgqc=req.getParameter("jgqc");
        String szdy=req.getParameter("szdy");
        String jsxqmc=req.getParameter("jsxqmc");
        System.out.println(jgqc+"   1  "+szdy+"   2   "+jsxqmc+"   3   ");

       /* if (szdy==null){
            List<pojo> kc=dao.select1(jgmc);
            req.setAttribute("kc", kc);
            req.getRequestDispatcher("select.jsp").forward(req, resp);
        } else if (jsxqmc==null) {
            List<pojo> kc=dao.select1(jgmc,szdy);
            req.setAttribute("kc", kc);
            req.getRequestDispatcher("select.jsp").forward(req, resp);
        }*/
        System.out.println("1");
        List<pojo> kc=dao.select1(jgqc,szdy,jsxqmc);

        for(pojo pojo: kc) {

            System.out.println(pojo.toString());
        }
        System.out.println("2");

        req.setAttribute("kc", kc);
        req.getRequestDispatcher("search.jsp").forward(req, resp);
    }
}

 

 

 

 

标签:szdy,String,jgqc,req,CRUD,查询,HBase,public,jsxqmc
From: https://www.cnblogs.com/1774323810com/p/16856022.html

相关文章

  • MySQL连接分页查询问题
    问题两表连接查询分页,如果不进行排序,则分页不准确,这将导致第一页出现的数据会在后几页再次出现,而且每次查询第一页结果都不一致解决连接查询分页必须按主键分页才能保证......
  • Linux中tac命令倒序查询日志
    cat命令是正序开始查询日志比如:catxxx.log|grep"sssdsd"如果日志文件比较大,那么会很慢或者直接出错 可以使用tac命令,这个是cat反过来写tacxxx.log|grep"sssdsd"这样就......
  • MySQL—简单,分组,条件查询
    MySQL1.什么是SQL:结构化查询语言(StructuredQueryLanguage)。2.SQL的作用:客户端使用SQL来操作服务器。启动mysql.exe,连接服务器后,就可以使用sql来操作服务器了。将......
  • 数据库等值查询与统计信息
    简介: 统计信息是为优化器的cost估算提供数据支撑,其中很重要的一点需求便是等值查询(EQUALS,IN等)场景下的基数估算。概念统计信息是为优化器的cost估算提供数......
  • [HBase进阶]--rowkey设计要点(官方文档介绍)
    官方文档说明 ​​http://hbase.apache.org/book.html#rowkey.design​​一、Hotspotting(热点效应) 1、hbase是字典排序,这是一种优化扫描的方式,它允许你去把相关的行存储在......
  • 项目上线后,谈一下感触比较深的一点:查询优化
    开心一刻儿子有道题不会做,喊我过去教他我推了推一旁的老公:我头疼,你去吧老公不动,我:零花钱涨一千话音刚落,老公就屁颠屁颠跑去儿子房间进去不到几分钟,一声......
  • postgresql闪回查询及其问题
    其实早在2018年,PostgresProfessional就实现了一个闪回查询的原型,基于vacuum_defer_cleanup_age和track_commit_timestamp。这本质上和LZ所想一致。但是存在的问题是膨胀......
  • sqlserver查询语句SELECT
    SELECT查询语句数据查询是数据库的核心操作,其功能是指根据用户的需要从数据库中提取所需数据,通过SQL的数据操纵语言的SELECT语句可以实现数据库数据的查询。SELECT语句是......
  • day24 JDBC批处理(通用泛型查询方法 & 下划线转驼峰命名法)
    批处理publicstaticIntegeraddBatch(String[]sqls){ init(); try{ //设置关闭自动提交 conn.setAutoCommit(false); Statementstmt=conn.createState......
  • mysql 查询30分钟内的验证码
    mysql查询30分钟内的验证码 selectt.`value`as`value`fromt_message_infot wheret.user_name=13072281151ANDt.`key`=1ANDt.is_use=0ANDt.crea......