完成在本机远程连接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>
浙公网安备 33010602011771号