1.進行hbase與本機遠程鏈接測試鏈接html
1.1 修改虛擬機文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost換成你的虛擬機主機名字java
1.2修改虛擬機hosts文件,把之前的註釋掉,並加上ip地址與主機名的映射關係web
1.3修改主機hosts文件 同上sql
1.4 測試鏈接apache
package model; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.client.Table; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; public class HelloHBase { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; //創建鏈接 public static void init(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.146.128"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //關閉鏈接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } /** * 建表。HBase的表中會有一個系統默認的屬性做爲主鍵,主鍵無需自行建立,默認爲put命令操做中表名後第一個數據,所以此處無需建立id列 * @param myTableName 表名 * @param colFamily 列族名 * @throws IOException */ public static void createTable(String myTableName,String[] colFamily) throws IOException { init(); TableName tableName = TableName.valueOf(myTableName); if(admin.tableExists(tableName)){ System.out.println("talbe is exists!"); }else { HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName); for(String str:colFamily){ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str); hTableDescriptor.addFamily(hColumnDescriptor); } admin.createTable(hTableDescriptor); System.out.println("create table success"); } close(); } /** * 向某一行的某一列插入數據 * @param tableName 表名 * @param rowKey 行鍵 * @param colFamily 列族名 * @param col 列名(若是其列族下沒有子列,此參數可爲空) * @param val 值 * @throws IOException */ public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { init(); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); close(); } public static void main(String [] args)throws IOException { createTable("platform",new String[]{"article","PId","Pnum","approval_num","techField","PLevel","city","JJJGJ","Pform","faren_type","unite"}); //在Score表中插入一條數據,其行鍵爲95001,sname爲Mary(由於sname列族下沒有子列因此第四個參數爲空) //等價命令:put 'Score','95001','sname','Mary' //insertRow("platform", "2", "Pname", "", "KYL"); //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列) //等價命令:put 'Score','95001','score:Math','88' //insertRow("platform", "2", "PId", "", "2"); //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列) //等價命令:put 'Score','95001','score:English','85' //insertRow("platform", "2", "Pnum", "", "2019-1-25"); //insertRow("platform", "2", "approval_num", "", "123-456"); //insertRow("platform", "2", "techField", "", "心理"); //insertRow("platform", "2", "PLevel", "", "國家級"); //insertRow("platform", "2", "city", "", "湖南長沙嶽麓區"); //insertRow("platform", "2", "JJJGJ", "", "是"); //insertRow("platform", "2", "Pform", "", "內設機構相對獨立"); //insertRow("platform", "2", "faren_type", "", "事業法人"); //insertRow("platform", "2", "unite", "", "多單位聯合共建"); } }
成功會控制檯輸出 create table successapi
2.Java api實例操做增刪改查tomcat
系統流程爲:填表->根據平臺編號查看平臺相關信息->根據平臺編號修改信息->根據平臺編號刪除信息app
遇到的問題見上個博客,必定要注意HBase與tomcat的包會有衝突jsp
還有幾個值得注意的是:hbase的包在寫web界面要放到WEB-INF/lib下ide
幾個界面截圖以下:
model包
package model; import java.sql.Date; public class platform { private String Pname; //楠炲啿褰撮崥宥囆� private String PId; //楠炲啿褰寸紓鏍у嬌 private String Pnum;//閹電懓鍣獮瀛樻箑 private String approval_num; //閹電懓鍣弬鍥у嬌 private String techField; //閹訛拷閺堫垶顣崺錕� private String PLevel; //楠炲啿褰寸瘓褍鍩� private String city;//閹碉拷閸︺劌絝墮崠錕� private String JJJGJ;//嫺滎剚瑙﹂崘錕介崗鍗炵紦 private String Pform;//楠炲啿褰寸紒鍕矏瑜般垹鞝� private String faren_type;//濞夋洑奼夌猾璇茬�� private String unite; //閼辨柨鎮庨崗鍗炵紦 public platform(String Pname, String Pnum, String approval_num, String techField, String PLevel, String city, String Pform, String faren_type, String unite, String JJJGJ) { this.Pname = Pname; this.Pnum = Pnum; this.approval_num = approval_num; this.techField = techField; this.PLevel = PLevel; this.city = city; this.JJJGJ = JJJGJ; this.Pform = Pform; this.faren_type = faren_type; this.unite = unite; } public platform() { } public String getPname() { return Pname; } public void setPname(String pname) { Pname = pname; } public String getPId() { return PId; } public void setPId(String pId) { PId = pId; } public String getPnum() { return Pnum; } public void setPnum(String pnum) { Pnum = pnum; } public String getApproval_num() { return approval_num; } public void setApproval_num(String approval_num) { this.approval_num = approval_num; } public String getTechField() { return techField; } public void setTechField(String techField) { this.techField = techField; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPLevel() { return PLevel; } public void setPLevel(String pLevel) { PLevel = pLevel; } public String getJJJGJ() { return JJJGJ; } public void setJJJGJ(String jJJGJ) { JJJGJ = jJJGJ; } public String getPform() { return Pform; } public void setPform(String pform) { Pform = pform; } public String getFaren_type() { return faren_type; } public void setFaren_type(String faren_type) { this.faren_type = faren_type; } public String getUnite() { return unite; } public void setUnite(String unite) { this.unite = unite; } }
dao層
package dao; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; import model.platform; public class PlatformDao { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; //創建鏈接 public static void init(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.146.128"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //關閉鏈接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } @SuppressWarnings("deprecation") public platform get(String tableName,String rowKey) { init(); Table table = null; platform platform = new platform(); try { table = connection.getTable(TableName.valueOf(tableName)); Get get = new Get(rowKey.getBytes()); Result result = table.get(get); KeyValue[] raw = result.raw(); platform.setPId(new String(raw[1].getValue()) ); platform.setPname(new String(raw[4].getValue()) ); platform.setPnum(new String(raw[5].getValue()) ); platform.setApproval_num(new String(raw[6].getValue()) ); platform.setTechField(new String(raw[9].getValue()) ); platform.setPLevel(new String(raw[2].getValue()) ); platform.setCity(new String(raw[7].getValue()) ); platform.setJJJGJ(new String(raw[0].getValue()) ); platform.setPform(new String(raw[3].getValue()) ); platform.setFaren_type(new String(raw[8].getValue()) ); platform.setUnite(new String(raw[10].getValue()) ); System.out.println("平臺編號:"+platform.getPId()); System.out.println("平臺姓名:"+platform.getPname()); System.out.println("批准年月:"+platform.getPnum()); System.out.println("批准文號:"+platform.getApproval_num()); System.out.println("技術領域:"+platform.getTechField()); System.out.println("平臺等級:"+platform.getPLevel()); System.out.println("所在市區:"+platform.getCity()); System.out.println("京津冀共建:"+platform.getJJJGJ()); System.out.println("平臺組織形態:"+platform.getPform()); System.out.println("法人類型:"+platform.getFaren_type()); System.out.println("聯合共建:"+platform.getUnite()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { table.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } close(); } return platform; } public static void main(String [] args)throws IOException { //createTable("platform",new String[]{"article","PId","Pnum","approval_num","techField","PLevel","city","JJJGJ","Pform","faren_type","unite"}); //在Score表中插入一條數據,其行鍵爲95001,sname爲Mary(由於sname列族下沒有子列因此第四個參數爲空) //等價命令:put 'Score','95001','sname','Mary' //insertRow("platform", "2", "Pname", "", "KYL"); //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列) //等價命令:put 'Score','95001','score:Math','88' //insertRow("platform", "2", "PId", "", "2"); //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列) //等價命令:put 'Score','95001','score:English','85' //insertRow("platform", "2", "Pnum", "", "2019-1-25"); //insertRow("platform", "2", "approval_num", "", "123-456"); //insertRow("platform", "2", "techField", "", "心理"); //insertRow("platform", "2", "PLevel", "", "國家級"); //insertRow("platform", "2", "city", "", "湖南長沙嶽麓區"); //insertRow("platform", "2", "JJJGJ", "", "是"); //insertRow("platform", "2", "Pform", "", "內設機構相對獨立"); //insertRow("platform", "2", "faren_type", "", "事業法人"); //insertRow("platform", "2", "unite", "", "多單位聯合共建"); //查詢Score表中,行鍵爲95001,列族爲course,列爲Math的值 //PlatformDao platformDao = new PlatformDao(); //platformDao.get("platform", "123456"); //查詢Score表中,行鍵爲95001,列族爲sname的值(由於sname列族下沒有子列因此第四個參數爲空) //getData("Score", "95001", "sname", ""); } }
servlet
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; 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 javax.servlet.http.HttpSession; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; @SuppressWarnings("serial") public class FillinServlet extends HttpServlet { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; //創建鏈接 public static void init1(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.146.128"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //關閉鏈接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doGet(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); /**平臺錕斤拷錕斤拷錕斤拷息*/ String Pname = request.getParameter("Pname");//平臺名稱 String PId = request.getParameter("PId");//平臺編號 String Pnum = request.getParameter("Pnum"); //批准年月 String approval_num = request.getParameter("approval_num"); //批准文號 String tech_field = request.getParameter("tech_field"); //技術領域 String platform_grade = request.getParameter("platform_grade"); //平臺級別 String platform_province = request.getParameter("cmbProvince"); //省 String platform_city = request.getParameter("cmbCity"); //市 String platform_district = request.getParameter("cmbArea"); //區 String platform_form = request.getParameter("platform_form"); //平臺組織形式 String platform_faren_type = request.getParameter("faren_type"); //法人類型 String platform_unite = request.getParameter("unite"); //聯合共建 String platform_jjjgj = request.getParameter("jjjgj"); //京津冀共建 /** * 錕斤拷錕斤拷錕斤拷錕捷匡拷 */ System.out.println(Pname); String city = platform_province+platform_city+platform_district; try { //等價命令:put 'Score','95001','sname','Mary' insertRow("platform", PId, "Pname", "", Pname); //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列) //等價命令:put 'Score','95001','score:Math','88' insertRow("platform", PId, "PId", "", PId); //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列) //等價命令:put 'Score','95001','score:English','85' insertRow("platform", PId, "Pnum", "", Pnum); insertRow("platform", PId, "approval_num", "", approval_num); insertRow("platform", PId, "techField", "", tech_field); insertRow("platform", PId, "PLevel", "", platform_grade); insertRow("platform", PId, "city", "",city ); insertRow("platform", PId, "JJJGJ", "", platform_jjjgj); insertRow("platform", PId, "Pform", "", platform_form); insertRow("platform", PId, "faren_type", "", platform_faren_type); insertRow("platform", PId, "unite", "", platform_unite); }catch (IOException e) { e.printStackTrace(); } response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.println("<script language='javaScript'>alert(\"添加成功!\");</script>"); response.setHeader("refresh","1;url = /HBase/index.jsp"); Bytes.toBytes("Pname"); Bytes.toBytes("PId"); Bytes.toBytes("Pnum"); Bytes.toBytes("approval_num"); Bytes.toBytes("techField"); Bytes.toBytes("city"); Bytes.toBytes("JJJGJ"); Bytes.toBytes("Pform"); Bytes.toBytes("faren_type"); Bytes.toBytes("unite"); } /** * 向某一行的某一列插入數據 * @param tableName 表名 * @param rowKey 行鍵 * @param colFamily 列族名 * @param col 列名(若是其列族下沒有子列,此參數可爲空) * @param val 值 * @throws IOException */ public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { init1(); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); close(); } }
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; 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 javax.servlet.http.HttpSession; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; public class DeleteServlet extends HttpServlet { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; //創建鏈接 public static void init1(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.146.128"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //關閉鏈接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); /**平臺錕斤拷錕斤拷錕斤拷息*/ String PId = request.getParameter("PId");//平臺編號 try { //等價命令:put 'Score','95001','sname','Mary' deleteRow("platform", PId); //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列) //等價命令:put 'Score','95001','score:Math','88' //deleteRow("platform", PId, "PId", ""); //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列) //等價命令:put 'Score','95001','score:English','85' //deleteRow("platform", PId, "Pnum", ""); //deleteRow("platform", PId, "approval_num", ""); //deleteRow("platform", PId, "techField", ""); // deleteRow("platform", PId, "PLevel", ""); //deleteRow("platform", PId, "city", ""); //deleteRow("platform", PId, "JJJGJ", ""); //deleteRow("platform", PId, "Pform", ""); //deleteRow("platform", PId, "faren_type", ""); //deleteRow("platform", PId, "unite", ""); }catch (IOException e) { e.printStackTrace(); } System.out.println("刪除成功"); response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.println("<script language='javaScript'>alert(\"刪除成功!\");</script>"); response.setHeader("refresh","1;url = /HBase/index.jsp"); //request.getRequestDispatcher("/index1.jsp").forward(request,response); } public static void deleteRow(String tableName, String row) throws IOException { init1(); Table table = connection.getTable(TableName.valueOf(tableName)); Delete delete=new Delete(row.getBytes()); table.delete(delete); System.out.println(6666); table.close(); close(); } }
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; 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 javax.servlet.http.HttpSession; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; import dao.PlatformDao; import model.platform; @SuppressWarnings("serial") public class ModifyServlet extends HttpServlet { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; //創建鏈接 public static void init1(){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.146.128"); try{ connection = ConnectionFactory.createConnection(configuration); admin = connection.getAdmin(); }catch (IOException e){ e.printStackTrace(); } } //關閉鏈接 public static void close(){ try{ if(admin != null){ admin.close(); } if(null != connection){ connection.close(); } }catch (IOException e){ e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); /**平臺錕斤拷錕斤拷錕斤拷息*/ String PId = request.getParameter("PId"); //String condition = request.getParameter("Condition"); String content = request.getParameter("content"); System.out.println(PId); //System.out.println(condition); System.out.println(content); try { //等價命令:put 'Score','95001','sname','Mary' //deleteRow("platform", PId); //在Score表中插入一條數據,其行鍵爲95001,course:Math爲88(course爲列族,Math爲course下的子列) //等價命令:put 'Score','95001','score:Math','88' //deleteRow("platform", PId, "PId", ""); //在Score表中插入一條數據,其行鍵爲95001,course:English爲85(course爲列族,English爲course下的子列) //等價命令:put 'Score','95001','score:English','85' //deleteRow("platform", PId, "Pnum", ""); //deleteRow("platform", PId, "approval_num", ""); //deleteRow("platform", PId, "techField", ""); // deleteRow("platform", PId, "PLevel", ""); //deleteRow("platform", PId, "city", ""); //deleteRow("platform", PId, "JJJGJ", ""); //deleteRow("platform", PId, "Pform", ""); //deleteRow("platform", PId, "faren_type", ""); //deleteRow("platform", PId, "unite", ""); insertRow("platform",PId,"approval_num","",content); }catch (IOException e) { e.printStackTrace(); } response.setCharacterEncoding("GBK"); PrintWriter out = response.getWriter(); out.println("<script language='javaScript'>alert(\"修改爲功!\");</script>"); response.setHeader("refresh","1;url = /HBase/index.jsp"); //request.getRequestDispatcher("/index1.jsp").forward(request,response); } /** * 向某一行的某一列插入數據 * @param tableName 表名 * @param rowKey 行鍵 * @param colFamily 列族名 * @param col 列名(若是其列族下沒有子列,此參數可爲空) * @param val 值 * @throws IOException */ public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException { init1(); System.out.println("1"+rowKey); System.out.println("2"+colFamily); System.out.println("3"+col); System.out.println("4"+val); Table table = connection.getTable(TableName.valueOf(tableName)); Put put = new Put(rowKey.getBytes()); put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); table.put(put); table.close(); close(); } }
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; 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 javax.servlet.http.HttpSession; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; import org.apache.hadoop.util.Progressable; import dao.PlatformDao; import model.platform; @SuppressWarnings("serial") public class SearchServlet extends HttpServlet { public static Configuration configuration; public static Connection connection; public static Admin admin; public static long ts; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String PId = request.getParameter("PId"); platform platform = new platform(); PlatformDao platformDao = new PlatformDao(); platform = platformDao.get("platform", PId); request.setAttribute("PId", platform.getPId()); request.setAttribute("Pname", platform.getPname()); request.setAttribute("Pnum", platform.getPnum()); request.setAttribute("approval_num", platform.getApproval_num()); request.setAttribute("techField", platform.getTechField()); request.setAttribute("PLevel", platform.getTechField()); request.setAttribute("city", platform.getCity()); request.setAttribute("JJJGJ", platform.getJJJGJ()); request.setAttribute("Pform", platform.getPform()); request.setAttribute("faren_type", platform.getFaren_type()); request.setAttribute("unite", platform.getUnite()); System.out.println("平臺編號:"+platform.getPId()); request.getRequestDispatcher("/show.jsp").forward(request,response); } }
jsp
modify.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>修改內容</title> </head> <body> <form action="servlet/ModifyServlet" method="post"> 請選擇要修改的平臺編號:<input type="text" name="PId"> 請選擇要修改的內容:<br> <select name="Condition"> <option value="Approval_num">批准文號</option> <option value="PLevel">平臺級別</option> </select> <input type="text" name="content"> <button type="submit">提交</button> </form> </body> </html>
Fill_in.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet/SearchServlet" method="post"> <div> <input type="text" name="PId"> <button type="submit">提交</button> </div> </form> </body> </html>
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String PId = (String)request.getAttribute("PId");%> <% String Pname = (String)request.getAttribute("Pname");%> <% String Pnum = (String)request.getAttribute("Pnum");%> <% String approval_num = (String)request.getAttribute("approval_num");%> <% String techField = (String)request.getAttribute("techField");%> <% String PLevel = (String)request.getAttribute("PLevel");%> <% String city = (String)request.getAttribute("city");%> <% String JJJGJ = (String)request.getAttribute("JJJGJ");%> <% String Pform = (String)request.getAttribute("Pform");%> <% String faren_type = (String)request.getAttribute("faren_type");%> <% String unite = (String)request.getAttribute("unite");%> <%="平臺編號爲:"+PId %> <br><br> <%="平臺名稱爲:"+Pname %> <br><br> <%="批准年月爲:"+Pnum %> <br><br> <%="批准文號爲:"+approval_num %> <br><br> <%="平臺編號爲:"+techField %> <br><br> <%="平臺編號爲:"+PLevel %> <br><br> <%="平臺編號爲:"+city %> <br><br> <%="平臺編號爲:"+JJJGJ %> <br><br> <%="平臺編號爲:"+Pform %> <br><br> <%="平臺編號爲:"+faren_type %> <br><br> <%="平臺編號爲:"+unite %> <br><br> <button type="submit"><a href="index.jsp">回到首頁</a></button> </body> </html>
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="servlet/DeleteServlet" method="post"> <div> <input type="text" name="PId"> <button type="submit">提交</button> </div> </form> </body> </html>