我这是两次的实验用的一个项目,第二次的实验是在第一次上的实验的基础上增加而来
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