1.进行hbase与本机远程连接测试连接

1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字

1.2修改虚拟机hosts文件,把以前的注释掉,并加上ip地址与主机名的映射关系

1.3修改主机hosts文件 同上

1.4 测试连接

  1. package model;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.*;
  7. import org.apache.hadoop.hbase.Cell;
  8. import org.apache.hadoop.hbase.CellUtil;
  9. import org.apache.hadoop.hbase.HBaseConfiguration;
  10. import org.apache.hadoop.hbase.HColumnDescriptor;
  11. import org.apache.hadoop.hbase.HTableDescriptor;
  12. import org.apache.hadoop.hbase.TableName;
  13. import org.apache.hadoop.hbase.client.*;
  14. import org.apache.hadoop.hbase.client.Connection;
  15. import org.apache.hadoop.hbase.client.ConnectionFactory;
  16. import org.apache.hadoop.hbase.client.Put;
  17. import org.apache.hadoop.hbase.client.Scan;
  18. import org.apache.hadoop.hbase.client.Table;
  19. import org.apache.hadoop.hbase.util.Bytes;
  20. import org.apache.hadoop.io.IOUtils;
  21. import org.apache.hadoop.util.Progressable;
  22. public class HelloHBase {
  23. public static Configuration configuration;
  24. public static Connection connection;
  25. public static Admin admin;
  26. public static long ts;
  27.  
  28. //建立连接
  29. public static void init(){
  30. configuration = HBaseConfiguration.create();
  31. configuration.set("hbase.zookeeper.quorum","192.168.146.128");
  32. try{
  33. connection = ConnectionFactory.createConnection(configuration);
  34. admin = connection.getAdmin();
  35. }catch (IOException e){
  36. e.printStackTrace();
  37. }
  38. }
  39. //关闭连接
  40. public static void close(){
  41. try{
  42. if(admin != null){
  43. admin.close();
  44. }
  45. if(null != connection){
  46. connection.close();
  47. }
  48. }catch (IOException e){
  49. e.printStackTrace();
  50. }
  51. }
  52.  
  53. /**
  54. * 建表。HBase的表中会有一个系统默认的属性作为主键,主键无需自行创建,默认为put命令操作中表名后第一个数据,因此此处无需创建id列
  55. * @param myTableName 表名
  56. * @param colFamily 列族名
  57. * @throws IOException
  58. */
  59. public static void createTable(String myTableName,String[] colFamily) throws IOException {
  60.  
  61. init();
  62. TableName tableName = TableName.valueOf(myTableName);
  63.  
  64. if(admin.tableExists(tableName)){
  65. System.out.println("talbe is exists!");
  66. }else {
  67. HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
  68. for(String str:colFamily){
  69. HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
  70. hTableDescriptor.addFamily(hColumnDescriptor);
  71. }
  72. admin.createTable(hTableDescriptor);
  73. System.out.println("create table success");
  74. }
  75. close();
  76. }
  77. /**
  78. * 向某一行的某一列插入数据
  79. * @param tableName 表名
  80. * @param rowKey 行键
  81. * @param colFamily 列族名
  82. * @param col 列名(如果其列族下没有子列,此参数可为空)
  83. * @param val 值
  84. * @throws IOException
  85. */
  86. public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  87. init();
  88. Table table = connection.getTable(TableName.valueOf(tableName));
  89. Put put = new Put(rowKey.getBytes());
  90. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  91. table.put(put);
  92. table.close();
  93. close();
  94. }
  95.  
  96. public static void main(String [] args)throws IOException {
  97. createTable("platform",new String[]{"article","PId","Pnum","approval_num","techField","PLevel","city","JJJGJ","Pform","faren_type","unite"});
  98. //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
  99. //等价命令:put 'Score','95001','sname','Mary'
  100. //insertRow("platform", "2", "Pname", "", "KYL");
  101. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
  102. //等价命令:put 'Score','95001','score:Math','88'
  103. //insertRow("platform", "2", "PId", "", "2");
  104. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
  105. //等价命令:put 'Score','95001','score:English','85'
  106. //insertRow("platform", "2", "Pnum", "", "2019-1-25");
  107. //insertRow("platform", "2", "approval_num", "", "123-456");
  108. //insertRow("platform", "2", "techField", "", "心理");
  109. //insertRow("platform", "2", "PLevel", "", "国家级");
  110. //insertRow("platform", "2", "city", "", "湖南长沙岳麓区");
  111. //insertRow("platform", "2", "JJJGJ", "", "是");
  112. //insertRow("platform", "2", "Pform", "", "内设机构相对独立");
  113. //insertRow("platform", "2", "faren_type", "", "事业法人");
  114. //insertRow("platform", "2", "unite", "", "多单位联合共建");
  115. }
  116. }

成功会控制台输出 create table success

2.Java api实例操作增删改查

系统流程为:填表->根据平台编号查看平台相关信息->根据平台编号修改信息->根据平台编号删除信息

遇到的问题见上个博客,一定要注意HBase与tomcat的包会有冲突

还有几个值得注意的是:hbase的包在写web界面要放到WEB-INF/lib下

几个界面截图如下:

model包

  1. package model;
  2.  
  3. import java.sql.Date;
  4.  
  5. public class platform {
  6. private String Pname; //楠炲啿褰撮崥宥囆�
  7. private String PId; //楠炲啿褰寸紓鏍у娇
  8. private String Pnum;//閹电懓鍣獮瀛樻箑
  9. private String approval_num; //閹电懓鍣弬鍥у娇
  10. private String techField; //閹讹拷閺堫垶顣崺锟�
  11. private String PLevel; //楠炲啿褰寸痪褍鍩�
  12. private String city;//閹碉拷閸︺劌绔堕崠锟�
  13. private String JJJGJ;//娴滎剚瑙﹂崘锟介崗鍗炵紦
  14. private String Pform;//楠炲啿褰寸紒鍕矏瑜般垹绱�
  15. private String faren_type;//濞夋洑姹夌猾璇茬��
  16. private String unite; //閼辨柨鎮庨崗鍗炵紦
  17. public platform(String Pname, String Pnum, String approval_num, String techField, String PLevel, String city, String Pform, String faren_type, String unite, String JJJGJ) {
  18. this.Pname = Pname;
  19. this.Pnum = Pnum;
  20. this.approval_num = approval_num;
  21. this.techField = techField;
  22. this.PLevel = PLevel;
  23. this.city = city;
  24. this.JJJGJ = JJJGJ;
  25. this.Pform = Pform;
  26. this.faren_type = faren_type;
  27. this.unite = unite;
  28. }
  29. public platform() {
  30.  
  31. }
  32. public String getPname() {
  33. return Pname;
  34. }
  35. public void setPname(String pname) {
  36. Pname = pname;
  37. }
  38.  
  39. public String getPId() {
  40. return PId;
  41. }
  42. public void setPId(String pId) {
  43. PId = pId;
  44. }
  45. public String getPnum() {
  46. return Pnum;
  47. }
  48. public void setPnum(String pnum) {
  49. Pnum = pnum;
  50. }
  51. public String getApproval_num() {
  52. return approval_num;
  53. }
  54. public void setApproval_num(String approval_num) {
  55. this.approval_num = approval_num;
  56. }
  57. public String getTechField() {
  58. return techField;
  59. }
  60. public void setTechField(String techField) {
  61. this.techField = techField;
  62. }
  63. public String getCity() {
  64. return city;
  65. }
  66. public void setCity(String city) {
  67. this.city = city;
  68. }
  69. public String getPLevel() {
  70. return PLevel;
  71. }
  72. public void setPLevel(String pLevel) {
  73. PLevel = pLevel;
  74. }
  75. public String getJJJGJ() {
  76. return JJJGJ;
  77. }
  78. public void setJJJGJ(String jJJGJ) {
  79. JJJGJ = jJJGJ;
  80. }
  81. public String getPform() {
  82. return Pform;
  83. }
  84. public void setPform(String pform) {
  85. Pform = pform;
  86. }
  87. public String getFaren_type() {
  88. return faren_type;
  89. }
  90. public void setFaren_type(String faren_type) {
  91. this.faren_type = faren_type;
  92. }
  93. public String getUnite() {
  94. return unite;
  95. }
  96. public void setUnite(String unite) {
  97. this.unite = unite;
  98. }
  99.  
  100. }

  

dao层

  1. package dao;
  2. import java.io.IOException;
  3.  
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.*;
  6. import org.apache.hadoop.hbase.Cell;
  7. import org.apache.hadoop.hbase.CellUtil;
  8. import org.apache.hadoop.hbase.HBaseConfiguration;
  9. import org.apache.hadoop.hbase.HColumnDescriptor;
  10. import org.apache.hadoop.hbase.HTableDescriptor;
  11. import org.apache.hadoop.hbase.KeyValue;
  12. import org.apache.hadoop.hbase.TableName;
  13. import org.apache.hadoop.hbase.client.*;
  14. import org.apache.hadoop.hbase.util.Bytes;
  15. import org.apache.hadoop.io.IOUtils;
  16. import org.apache.hadoop.util.Progressable;
  17.  
  18. import model.platform;
  19. public class PlatformDao {
  20. public static Configuration configuration;
  21. public static Connection connection;
  22. public static Admin admin;
  23. public static long ts;
  24.  
  25. //建立连接
  26. public static void init(){
  27. configuration = HBaseConfiguration.create();
  28. configuration.set("hbase.zookeeper.quorum","192.168.146.128");
  29. try{
  30. connection = ConnectionFactory.createConnection(configuration);
  31. admin = connection.getAdmin();
  32. }catch (IOException e){
  33. e.printStackTrace();
  34. }
  35. }
  36. //关闭连接
  37. public static void close(){
  38. try{
  39. if(admin != null){
  40. admin.close();
  41. }
  42. if(null != connection){
  43. connection.close();
  44. }
  45. }catch (IOException e){
  46. e.printStackTrace();
  47. }
  48. }
  49.  
  50. @SuppressWarnings("deprecation")
  51. public platform get(String tableName,String rowKey) {
  52. init();
  53. Table table = null;
  54. platform platform = new platform();
  55. try {
  56. table = connection.getTable(TableName.valueOf(tableName));
  57. Get get = new Get(rowKey.getBytes());
  58.  
  59. Result result = table.get(get);
  60.  
  61. KeyValue[] raw = result.raw();
  62. platform.setPId(new String(raw[1].getValue()) );
  63. platform.setPname(new String(raw[4].getValue()) );
  64. platform.setPnum(new String(raw[5].getValue()) );
  65. platform.setApproval_num(new String(raw[6].getValue()) );
  66. platform.setTechField(new String(raw[9].getValue()) );
  67. platform.setPLevel(new String(raw[2].getValue()) );
  68. platform.setCity(new String(raw[7].getValue()) );
  69. platform.setJJJGJ(new String(raw[0].getValue()) );
  70. platform.setPform(new String(raw[3].getValue()) );
  71. platform.setFaren_type(new String(raw[8].getValue()) );
  72. platform.setUnite(new String(raw[10].getValue()) );
  73. System.out.println("平台编号:"+platform.getPId());
  74. System.out.println("平台姓名:"+platform.getPname());
  75. System.out.println("批准年月:"+platform.getPnum());
  76. System.out.println("批准文号:"+platform.getApproval_num());
  77. System.out.println("技术领域:"+platform.getTechField());
  78. System.out.println("平台等级:"+platform.getPLevel());
  79. System.out.println("所在市区:"+platform.getCity());
  80. System.out.println("京津冀共建:"+platform.getJJJGJ());
  81. System.out.println("平台组织形态:"+platform.getPform());
  82. System.out.println("法人类型:"+platform.getFaren_type());
  83. System.out.println("联合共建:"+platform.getUnite());
  84. } catch (IOException e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }finally {
  88. try {
  89. table.close();
  90. } catch (IOException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. }
  94. close();
  95. }
  96. return platform;
  97.  
  98. }
  99.  
  100. public static void main(String [] args)throws IOException {
  101. //createTable("platform",new String[]{"article","PId","Pnum","approval_num","techField","PLevel","city","JJJGJ","Pform","faren_type","unite"});
  102. //在Score表中插入一条数据,其行键为95001,sname为Mary(因为sname列族下没有子列所以第四个参数为空)
  103. //等价命令:put 'Score','95001','sname','Mary'
  104. //insertRow("platform", "2", "Pname", "", "KYL");
  105. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
  106. //等价命令:put 'Score','95001','score:Math','88'
  107. //insertRow("platform", "2", "PId", "", "2");
  108. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
  109. //等价命令:put 'Score','95001','score:English','85'
  110. //insertRow("platform", "2", "Pnum", "", "2019-1-25");
  111. //insertRow("platform", "2", "approval_num", "", "123-456");
  112. //insertRow("platform", "2", "techField", "", "心理");
  113. //insertRow("platform", "2", "PLevel", "", "国家级");
  114. //insertRow("platform", "2", "city", "", "湖南长沙岳麓区");
  115. //insertRow("platform", "2", "JJJGJ", "", "是");
  116. //insertRow("platform", "2", "Pform", "", "内设机构相对独立");
  117. //insertRow("platform", "2", "faren_type", "", "事业法人");
  118. //insertRow("platform", "2", "unite", "", "多单位联合共建");
  119. //查询Score表中,行键为95001,列族为course,列为Math的值
  120. //PlatformDao platformDao = new PlatformDao();
  121. //platformDao.get("platform", "123456");
  122. //查询Score表中,行键为95001,列族为sname的值(因为sname列族下没有子列所以第四个参数为空)
  123. //getData("Score", "95001", "sname", "");
  124. }
  125. }

  

servlet

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Date;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. import org.apache.hadoop.conf.Configuration;
  19. import org.apache.hadoop.fs.*;
  20. import org.apache.hadoop.hbase.Cell;
  21. import org.apache.hadoop.hbase.CellUtil;
  22. import org.apache.hadoop.hbase.HBaseConfiguration;
  23. import org.apache.hadoop.hbase.HColumnDescriptor;
  24. import org.apache.hadoop.hbase.HTableDescriptor;
  25. import org.apache.hadoop.hbase.TableName;
  26. import org.apache.hadoop.hbase.client.*;
  27. import org.apache.hadoop.hbase.util.Bytes;
  28. import org.apache.hadoop.io.IOUtils;
  29. import org.apache.hadoop.util.Progressable;
  30.  
  31. @SuppressWarnings("serial")
  32. public class FillinServlet extends HttpServlet {
  33.  
  34. public static Configuration configuration;
  35. public static Connection connection;
  36. public static Admin admin;
  37. public static long ts;
  38.  
  39. //建立连接
  40. public static void init1(){
  41. configuration = HBaseConfiguration.create();
  42. configuration.set("hbase.zookeeper.quorum","192.168.146.128");
  43. try{
  44. connection = ConnectionFactory.createConnection(configuration);
  45. admin = connection.getAdmin();
  46. }catch (IOException e){
  47. e.printStackTrace();
  48. }
  49. }
  50. //关闭连接
  51. public static void close(){
  52. try{
  53. if(admin != null){
  54. admin.close();
  55. }
  56. if(null != connection){
  57. connection.close();
  58. }
  59. }catch (IOException e){
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. @Override
  65. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  66. // TODO Auto-generated method stub
  67. super.doGet(req, resp);
  68. }
  69.  
  70. @Override
  71. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  72. // TODO Auto-generated method stub
  73.  
  74. request.setCharacterEncoding("utf-8");
  75. /**平台锟斤拷锟斤拷锟斤拷息*/
  76. String Pname = request.getParameter("Pname");//平台名称
  77. String PId = request.getParameter("PId");//平台编号
  78. String Pnum = request.getParameter("Pnum"); //批准年月
  79. String approval_num = request.getParameter("approval_num"); //批准文号
  80. String tech_field = request.getParameter("tech_field"); //技术领域
  81. String platform_grade = request.getParameter("platform_grade"); //平台级别
  82. String platform_province = request.getParameter("cmbProvince"); //省
  83. String platform_city = request.getParameter("cmbCity"); //市
  84. String platform_district = request.getParameter("cmbArea"); //区
  85. String platform_form = request.getParameter("platform_form"); //平台组织形式
  86. String platform_faren_type = request.getParameter("faren_type"); //法人类型
  87. String platform_unite = request.getParameter("unite"); //联合共建
  88. String platform_jjjgj = request.getParameter("jjjgj"); //京津冀共建
  89. /**
  90. * 锟斤拷锟斤拷锟斤拷锟捷匡拷
  91. */
  92. System.out.println(Pname);
  93. String city = platform_province+platform_city+platform_district;
  94. try {
  95. //等价命令:put 'Score','95001','sname','Mary'
  96. insertRow("platform", PId, "Pname", "", Pname);
  97. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
  98. //等价命令:put 'Score','95001','score:Math','88'
  99. insertRow("platform", PId, "PId", "", PId);
  100. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
  101. //等价命令:put 'Score','95001','score:English','85'
  102. insertRow("platform", PId, "Pnum", "", Pnum);
  103. insertRow("platform", PId, "approval_num", "", approval_num);
  104. insertRow("platform", PId, "techField", "", tech_field);
  105. insertRow("platform", PId, "PLevel", "", platform_grade);
  106. insertRow("platform", PId, "city", "",city );
  107. insertRow("platform", PId, "JJJGJ", "", platform_jjjgj);
  108. insertRow("platform", PId, "Pform", "", platform_form);
  109. insertRow("platform", PId, "faren_type", "", platform_faren_type);
  110. insertRow("platform", PId, "unite", "", platform_unite);
  111. }catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. response.setCharacterEncoding("GBK");
  115. PrintWriter out = response.getWriter();
  116. out.println("<script language='javaScript'>alert(\"添加成功!\");</script>");
  117. response.setHeader("refresh","1;url = /HBase/index.jsp");
  118. Bytes.toBytes("Pname");
  119. Bytes.toBytes("PId");
  120. Bytes.toBytes("Pnum");
  121. Bytes.toBytes("approval_num");
  122. Bytes.toBytes("techField");
  123. Bytes.toBytes("city");
  124. Bytes.toBytes("JJJGJ");
  125. Bytes.toBytes("Pform");
  126. Bytes.toBytes("faren_type");
  127. Bytes.toBytes("unite");
  128. }
  129.  
  130. /**
  131. * 向某一行的某一列插入数据
  132. * @param tableName 表名
  133. * @param rowKey 行键
  134. * @param colFamily 列族名
  135. * @param col 列名(如果其列族下没有子列,此参数可为空)
  136. * @param val 值
  137. * @throws IOException
  138. */
  139. public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  140. init1();
  141. Table table = connection.getTable(TableName.valueOf(tableName));
  142. Put put = new Put(rowKey.getBytes());
  143. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  144. table.put(put);
  145. table.close();
  146. close();
  147. }
  148. }

  

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Date;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. import org.apache.hadoop.conf.Configuration;
  19. import org.apache.hadoop.fs.*;
  20. import org.apache.hadoop.hbase.Cell;
  21. import org.apache.hadoop.hbase.CellUtil;
  22. import org.apache.hadoop.hbase.HBaseConfiguration;
  23. import org.apache.hadoop.hbase.HColumnDescriptor;
  24. import org.apache.hadoop.hbase.HTableDescriptor;
  25. import org.apache.hadoop.hbase.TableName;
  26. import org.apache.hadoop.hbase.client.*;
  27. import org.apache.hadoop.hbase.util.Bytes;
  28. import org.apache.hadoop.io.IOUtils;
  29. import org.apache.hadoop.util.Progressable;
  30.  
  31. public class DeleteServlet extends HttpServlet {
  32.  
  33. public static Configuration configuration;
  34. public static Connection connection;
  35. public static Admin admin;
  36. public static long ts;
  37.  
  38. //建立连接
  39. public static void init1(){
  40. configuration = HBaseConfiguration.create();
  41. configuration.set("hbase.zookeeper.quorum","192.168.146.128");
  42. try{
  43. connection = ConnectionFactory.createConnection(configuration);
  44. admin = connection.getAdmin();
  45. }catch (IOException e){
  46. e.printStackTrace();
  47. }
  48. }
  49. //关闭连接
  50. public static void close(){
  51. try{
  52. if(admin != null){
  53. admin.close();
  54. }
  55. if(null != connection){
  56. connection.close();
  57. }
  58. }catch (IOException e){
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. @Override
  64. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  65. // TODO Auto-generated method stub
  66.  
  67. }
  68.  
  69. @Override
  70. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  71. // TODO Auto-generated method stub
  72.  
  73. request.setCharacterEncoding("utf-8");
  74. /**平台锟斤拷锟斤拷锟斤拷息*/
  75. String PId = request.getParameter("PId");//平台编号
  76.  
  77. try {
  78. //等价命令:put 'Score','95001','sname','Mary'
  79. deleteRow("platform", PId);
  80. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
  81. //等价命令:put 'Score','95001','score:Math','88'
  82. //deleteRow("platform", PId, "PId", "");
  83. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
  84. //等价命令:put 'Score','95001','score:English','85'
  85. //deleteRow("platform", PId, "Pnum", "");
  86. //deleteRow("platform", PId, "approval_num", "");
  87. //deleteRow("platform", PId, "techField", "");
  88. // deleteRow("platform", PId, "PLevel", "");
  89. //deleteRow("platform", PId, "city", "");
  90. //deleteRow("platform", PId, "JJJGJ", "");
  91. //deleteRow("platform", PId, "Pform", "");
  92. //deleteRow("platform", PId, "faren_type", "");
  93. //deleteRow("platform", PId, "unite", "");
  94. }catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. System.out.println("删除成功");
  98. response.setCharacterEncoding("GBK");
  99. PrintWriter out = response.getWriter();
  100. out.println("<script language='javaScript'>alert(\"删除成功!\");</script>");
  101. response.setHeader("refresh","1;url = /HBase/index.jsp");
  102.  
  103. //request.getRequestDispatcher("/index1.jsp").forward(request,response);
  104. }
  105. public static void deleteRow(String tableName, String row) throws IOException {
  106. init1();
  107. Table table = connection.getTable(TableName.valueOf(tableName));
  108. Delete delete=new Delete(row.getBytes());
  109. table.delete(delete);
  110. System.out.println(6666);
  111. table.close();
  112. close();
  113. }
  114.  
  115. }

  

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Date;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. import org.apache.hadoop.conf.Configuration;
  19. import org.apache.hadoop.fs.*;
  20. import org.apache.hadoop.hbase.Cell;
  21. import org.apache.hadoop.hbase.CellUtil;
  22. import org.apache.hadoop.hbase.HBaseConfiguration;
  23. import org.apache.hadoop.hbase.HColumnDescriptor;
  24. import org.apache.hadoop.hbase.HTableDescriptor;
  25. import org.apache.hadoop.hbase.TableName;
  26. import org.apache.hadoop.hbase.client.*;
  27. import org.apache.hadoop.hbase.util.Bytes;
  28. import org.apache.hadoop.io.IOUtils;
  29. import org.apache.hadoop.util.Progressable;
  30.  
  31. import dao.PlatformDao;
  32. import model.platform;
  33.  
  34. @SuppressWarnings("serial")
  35. public class ModifyServlet extends HttpServlet {
  36.  
  37. public static Configuration configuration;
  38. public static Connection connection;
  39. public static Admin admin;
  40. public static long ts;
  41.  
  42. //建立连接
  43. public static void init1(){
  44. configuration = HBaseConfiguration.create();
  45. configuration.set("hbase.zookeeper.quorum","192.168.146.128");
  46. try{
  47. connection = ConnectionFactory.createConnection(configuration);
  48. admin = connection.getAdmin();
  49. }catch (IOException e){
  50. e.printStackTrace();
  51. }
  52. }
  53. //关闭连接
  54. public static void close(){
  55. try{
  56. if(admin != null){
  57. admin.close();
  58. }
  59. if(null != connection){
  60. connection.close();
  61. }
  62. }catch (IOException e){
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. @Override
  68. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  69. // TODO Auto-generated method stub
  70.  
  71. }
  72.  
  73. @Override
  74. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  75. // TODO Auto-generated method stub
  76.  
  77. request.setCharacterEncoding("utf-8");
  78. /**平台锟斤拷锟斤拷锟斤拷息*/
  79. String PId = request.getParameter("PId");
  80. //String condition = request.getParameter("Condition");
  81. String content = request.getParameter("content");
  82. System.out.println(PId);
  83. //System.out.println(condition);
  84. System.out.println(content);
  85.  
  86. try {
  87. //等价命令:put 'Score','95001','sname','Mary'
  88. //deleteRow("platform", PId);
  89. //在Score表中插入一条数据,其行键为95001,course:Math为88(course为列族,Math为course下的子列)
  90. //等价命令:put 'Score','95001','score:Math','88'
  91. //deleteRow("platform", PId, "PId", "");
  92. //在Score表中插入一条数据,其行键为95001,course:English为85(course为列族,English为course下的子列)
  93. //等价命令:put 'Score','95001','score:English','85'
  94. //deleteRow("platform", PId, "Pnum", "");
  95. //deleteRow("platform", PId, "approval_num", "");
  96. //deleteRow("platform", PId, "techField", "");
  97. // deleteRow("platform", PId, "PLevel", "");
  98. //deleteRow("platform", PId, "city", "");
  99. //deleteRow("platform", PId, "JJJGJ", "");
  100. //deleteRow("platform", PId, "Pform", "");
  101. //deleteRow("platform", PId, "faren_type", "");
  102. //deleteRow("platform", PId, "unite", "");
  103.  
  104. insertRow("platform",PId,"approval_num","",content);
  105. }catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. response.setCharacterEncoding("GBK");
  109. PrintWriter out = response.getWriter();
  110. out.println("<script language='javaScript'>alert(\"修改成功!\");</script>");
  111. response.setHeader("refresh","1;url = /HBase/index.jsp");
  112.  
  113. //request.getRequestDispatcher("/index1.jsp").forward(request,response);
  114. }
  115. /**
  116. * 向某一行的某一列插入数据
  117. * @param tableName 表名
  118. * @param rowKey 行键
  119. * @param colFamily 列族名
  120. * @param col 列名(如果其列族下没有子列,此参数可为空)
  121. * @param val 值
  122. * @throws IOException
  123. */
  124.  
  125. public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
  126. init1();
  127. System.out.println("1"+rowKey);
  128. System.out.println("2"+colFamily);
  129. System.out.println("3"+col);
  130. System.out.println("4"+val);
  131. Table table = connection.getTable(TableName.valueOf(tableName));
  132. Put put = new Put(rowKey.getBytes());
  133. put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
  134. table.put(put);
  135. table.close();
  136. close();
  137. }
  138.  
  139. }

  

  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Date;
  6. import java.text.ParseException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16. import javax.servlet.http.HttpSession;
  17.  
  18. import org.apache.hadoop.conf.Configuration;
  19. import org.apache.hadoop.fs.*;
  20. import org.apache.hadoop.hbase.Cell;
  21. import org.apache.hadoop.hbase.CellUtil;
  22. import org.apache.hadoop.hbase.HBaseConfiguration;
  23. import org.apache.hadoop.hbase.HColumnDescriptor;
  24. import org.apache.hadoop.hbase.HTableDescriptor;
  25. import org.apache.hadoop.hbase.TableName;
  26. import org.apache.hadoop.hbase.client.*;
  27. import org.apache.hadoop.hbase.util.Bytes;
  28. import org.apache.hadoop.io.IOUtils;
  29. import org.apache.hadoop.util.Progressable;
  30.  
  31. import dao.PlatformDao;
  32. import model.platform;
  33.  
  34. @SuppressWarnings("serial")
  35. public class SearchServlet extends HttpServlet {
  36.  
  37. public static Configuration configuration;
  38. public static Connection connection;
  39. public static Admin admin;
  40. public static long ts;
  41.  
  42. @Override
  43. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44. // TODO Auto-generated method stub
  45.  
  46. }
  47.  
  48. @Override
  49. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  50. // TODO Auto-generated method stub
  51. request.setCharacterEncoding("utf-8");
  52. String PId = request.getParameter("PId");
  53. platform platform = new platform();
  54. PlatformDao platformDao = new PlatformDao();
  55. platform = platformDao.get("platform", PId);
  56.  
  57. request.setAttribute("PId", platform.getPId());
  58. request.setAttribute("Pname", platform.getPname());
  59. request.setAttribute("Pnum", platform.getPnum());
  60. request.setAttribute("approval_num", platform.getApproval_num());
  61. request.setAttribute("techField", platform.getTechField());
  62. request.setAttribute("PLevel", platform.getTechField());
  63. request.setAttribute("city", platform.getCity());
  64. request.setAttribute("JJJGJ", platform.getJJJGJ());
  65. request.setAttribute("Pform", platform.getPform());
  66. request.setAttribute("faren_type", platform.getFaren_type());
  67. request.setAttribute("unite", platform.getUnite());
  68. System.out.println("平台编号:"+platform.getPId());
  69.  
  70. request.getRequestDispatcher("/show.jsp").forward(request,response);
  71.  
  72. }
  73.  
  74. }

  

jsp

modify.jsp

  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2. pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>修改内容</title>
  8. </head>
  9. <body>
  10. <form action="servlet/ModifyServlet" method="post">
  11. 请选择要修改的平台编号:<input type="text" name="PId">
  12. 请选择要修改的内容:<br>
  13. <select name="Condition">
  14. <option value="Approval_num">批准文号</option>
  15. <option value="PLevel">平台级别</option>
  16. </select>
  17. <input type="text" name="content">
  18. <button type="submit">提交</button>
  19. </form>
  20. </body>
  21. </html>

  Fill_in.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="servlet/SearchServlet" method="post">
  11. <div>
  12. <input type="text" name="PId">
  13. <button type="submit">提交</button>
  14. </div>
  15. </form>
  16.  
  17. </body>
  18. </html>

  show.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <% String PId = (String)request.getAttribute("PId");%>
  11. <% String Pname = (String)request.getAttribute("Pname");%>
  12. <% String Pnum = (String)request.getAttribute("Pnum");%>
  13. <% String approval_num = (String)request.getAttribute("approval_num");%>
  14. <% String techField = (String)request.getAttribute("techField");%>
  15. <% String PLevel = (String)request.getAttribute("PLevel");%>
  16. <% String city = (String)request.getAttribute("city");%>
  17. <% String JJJGJ = (String)request.getAttribute("JJJGJ");%>
  18. <% String Pform = (String)request.getAttribute("Pform");%>
  19. <% String faren_type = (String)request.getAttribute("faren_type");%>
  20. <% String unite = (String)request.getAttribute("unite");%>
  21.  
  22. <%="平台编号为:"+PId %> <br><br>
  23. <%="平台名称为:"+Pname %> <br><br>
  24. <%="批准年月为:"+Pnum %> <br><br>
  25. <%="批准文号为:"+approval_num %> <br><br>
  26. <%="平台编号为:"+techField %> <br><br>
  27. <%="平台编号为:"+PLevel %> <br><br>
  28. <%="平台编号为:"+city %> <br><br>
  29. <%="平台编号为:"+JJJGJ %> <br><br>
  30. <%="平台编号为:"+Pform %> <br><br>
  31. <%="平台编号为:"+faren_type %> <br><br>
  32. <%="平台编号为:"+unite %> <br><br>
  33. <button type="submit"><a href="index.jsp">回到首页</a></button>
  34. </body>
  35. </html>

  delete.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="servlet/DeleteServlet" method="post">
  11. <div>
  12. <input type="text" name="PId">
  13. <button type="submit">提交</button>
  14. </div>
  15. </form>
  16.  
  17. </body>
  18. </html>

  

完成在本机远程连接HBase进行数据增删改查的更多相关文章

  1. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  2. Hbase常用操作(增删改查)

    Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区  作者:net19880504 [字体:大 中 小]     运行Eclipse,创建一个新的Java工程“HBa ...

  3. Node 连接Mysql并进行增删改查

    NPM: NPM的全称是Node Package Manager,类似于ruby的gem,Python的PyPL.setuptools,PHP的pear,是Nodejs中的包管理器.Nodejs自身提 ...

  4. java-数据库连接,分层实现增删改查测试

    成员属性类: public class Dog { private int number; private String name; private String strain; private St ...

  5. sql server连接oracle并实现增删改查

    需要一个软件ODAC112040Xcopy_64bit 我连接的oracle是11g r2  sqlserver 是 2016 软件下载 https://pan.baidu.com/s/1OpYmpR ...

  6. C++ API方式连接mysql数据库实现增删改查

    这里复制的 http://www.bitscn.com/pdb/mysql/201407/226252.html 一.环境配置 1,装好mysql,新建一个C++控制台工程(从最简单的弄起,这个会了, ...

  7. python3.4连接mysql5.7数据库增删改查

    #!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...

  8. 【C#】使用NHibernate连接MySQL数据库及增删改查

    学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...

  9. 通过jdbc连接MySql数据库的增删改查操作

    一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...

随机推荐

  1. 吴裕雄--天生自然C++语言学习笔记:C++ 信号处理

    信号是由操作系统传给进程的中断,会提早终止一个程序.在 UNIX.LINUX.Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断. 有些信号不能被程序捕获,但是下表所列信 ...

  2. js基础学习之-js全局对象

    声明的三种方式: 第一种: var test; //或var test = 5; 第二种: test = 5; 第三种: window.test; //或window.test = 5; //只是使用 ...

  3. mysql安装--window版

    一.下载 二.解压 三.配置 四.环境变量 五.安装MySQL服务 六.启动MySQL服务 七.停止MySQL 一.下载 第一步:打开网址,https://www.mysql.com,点击downlo ...

  4. PyCharm下创建并运行我们的第一个Django项目

    PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...

  5. Java工程师面试题

    1. J2EE 是什么?它包括哪些技术?解答:从整体上讲,J2EE 是使用 Java 技术开发企业级应用的工业标准,它是 Java 技术不断适应和促进企业级应用过程中的产物.适用于企业级应用的 J2E ...

  6. 使用软件模拟spi 时序时注意点

    软件模拟 spi 时序有以下几个点需要注意: cs 使能后到第一个 sck 边沿需要延时. 最后一个sck 边沿到下一个 cs 需要延时. sck 的高电平和低电平本身需要维持时间. mosi 需要先 ...

  7. 一文说透 Spring 循环依赖问题

    https://zhuanlan.zhihu.com/p/62382615 循环依赖发生的时机 Bean 实例化主要分为三步,如图: 问题出现在:第一步和第二步的过程中,也就是填充属性 / 方法的过程 ...

  8. 安装scrapy 爬虫框架

    安装scrapy 爬虫框架 个人根据学习需要,在Windows搭建scrapy爬虫框架,搭建过程种遇到个别问题,共享出来作为记录. 1.安装python 2.7 1.1下载 下载地址 1.2配置环境变 ...

  9. 一名资深架构师规划Java程序员五年职业生涯指南

    每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天我给大家分享的是一篇来自阿里大牛对五年工作经验程序员的职业建议,希望 ...

  10. VUE.js入门学习(4)-动画特效

    1.VUE中CSS动画原理(more是  v-enter 具体的根据 name的来决定) 动画是通过在某一时间段来添加样式决定的. 要通过 transition进行包裹. 2.在VUE中使用 anim ...