完成在本机远程连接HBase进行数据增删改查
1.进行hbase与本机远程连接测试连接
1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字
1.2修改虚拟机hosts文件,把以前的注释掉,并加上ip地址与主机名的映射关系
1.3修改主机hosts文件 同上
1.4 测试连接
- 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 success
2.Java api实例操作增删改查
系统流程为:填表->根据平台编号查看平台相关信息->根据平台编号修改信息->根据平台编号删除信息
遇到的问题见上个博客,一定要注意HBase与tomcat的包会有冲突
还有几个值得注意的是:hbase的包在写web界面要放到WEB-INF/lib下
几个界面截图如下:
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>
完成在本机远程连接HBase进行数据增删改查的更多相关文章
- java jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- Hbase常用操作(增删改查)
Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区 作者:net19880504 [字体:大 中 小] 运行Eclipse,创建一个新的Java工程“HBa ...
- Node 连接Mysql并进行增删改查
NPM: NPM的全称是Node Package Manager,类似于ruby的gem,Python的PyPL.setuptools,PHP的pear,是Nodejs中的包管理器.Nodejs自身提 ...
- java-数据库连接,分层实现增删改查测试
成员属性类: public class Dog { private int number; private String name; private String strain; private St ...
- sql server连接oracle并实现增删改查
需要一个软件ODAC112040Xcopy_64bit 我连接的oracle是11g r2 sqlserver 是 2016 软件下载 https://pan.baidu.com/s/1OpYmpR ...
- C++ API方式连接mysql数据库实现增删改查
这里复制的 http://www.bitscn.com/pdb/mysql/201407/226252.html 一.环境配置 1,装好mysql,新建一个C++控制台工程(从最简单的弄起,这个会了, ...
- python3.4连接mysql5.7数据库增删改查
#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = "blzhu" """ pyt ...
- 【C#】使用NHibernate连接MySQL数据库及增删改查
学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...
- 通过jdbc连接MySql数据库的增删改查操作
一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...
随机推荐
- 吴裕雄--天生自然C++语言学习笔记:C++ 信号处理
信号是由操作系统传给进程的中断,会提早终止一个程序.在 UNIX.LINUX.Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断. 有些信号不能被程序捕获,但是下表所列信 ...
- js基础学习之-js全局对象
声明的三种方式: 第一种: var test; //或var test = 5; 第二种: test = 5; 第三种: window.test; //或window.test = 5; //只是使用 ...
- mysql安装--window版
一.下载 二.解压 三.配置 四.环境变量 五.安装MySQL服务 六.启动MySQL服务 七.停止MySQL 一.下载 第一步:打开网址,https://www.mysql.com,点击downlo ...
- PyCharm下创建并运行我们的第一个Django项目
PyCharm下创建并运行我们的第一个Django项目 准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm 1. 创建一个新的工程 第一次运行Pycha ...
- Java工程师面试题
1. J2EE 是什么?它包括哪些技术?解答:从整体上讲,J2EE 是使用 Java 技术开发企业级应用的工业标准,它是 Java 技术不断适应和促进企业级应用过程中的产物.适用于企业级应用的 J2E ...
- 使用软件模拟spi 时序时注意点
软件模拟 spi 时序有以下几个点需要注意: cs 使能后到第一个 sck 边沿需要延时. 最后一个sck 边沿到下一个 cs 需要延时. sck 的高电平和低电平本身需要维持时间. mosi 需要先 ...
- 一文说透 Spring 循环依赖问题
https://zhuanlan.zhihu.com/p/62382615 循环依赖发生的时机 Bean 实例化主要分为三步,如图: 问题出现在:第一步和第二步的过程中,也就是填充属性 / 方法的过程 ...
- 安装scrapy 爬虫框架
安装scrapy 爬虫框架 个人根据学习需要,在Windows搭建scrapy爬虫框架,搭建过程种遇到个别问题,共享出来作为记录. 1.安装python 2.7 1.1下载 下载地址 1.2配置环境变 ...
- 一名资深架构师规划Java程序员五年职业生涯指南
每个程序员.或者说每个工作者都应该有自己的职业规划,如果你不是富二代,不是官二代,也没有职业规划,希望你可以思考一下自己的将来.今天我给大家分享的是一篇来自阿里大牛对五年工作经验程序员的职业建议,希望 ...
- VUE.js入门学习(4)-动画特效
1.VUE中CSS动画原理(more是 v-enter 具体的根据 name的来决定) 动画是通过在某一时间段来添加样式决定的. 要通过 transition进行包裹. 2.在VUE中使用 anim ...