完成在本机远程连接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 ...
随机推荐
- Hive 数据类型 + Hive sql
Hive 数据类型 + Hive sql 基本类型 整型 int tinyint (byte) smallint(short) bigint(long) 浮点型 float double 布尔 boo ...
- hdu 3790 最短路径dijkstra(多重权值)
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- python 虚拟环境的安装
方式一 1. pip install virtualenv 2. virtualenv 虚拟环境的名字 3. mac上 source + 虚拟环境的目录/bin/activate win上 直接进入虚 ...
- 使用apply调用函数
实现函数 calllt,调用之后满足如下条件1.返回的结果为调用fn之后的结果2.fn的调用为calllt的第一个参数之后的全部参数 方法1 使用es6 function calllt(...para ...
- java floor,ceil和round方法
Math.floor():返回值是double类型的,返回的是不大于它的最大整数 举例: double x = Math.floor(5.8); System.out.println(x); //输出 ...
- 外部 Storage Provider【转】
如果 Kubernetes 部署在诸如 AWS.GCE.Azure 等公有云上,可以直接使用云硬盘作为 Volume,下面是 AWS Elastic Block Store 的例子: 要在 Pod 中 ...
- poj 3693 Maximum repetition substring
呵呵呵呵呵呵呵呵呵呵,sb(神犇)题看了一天,还是不懂 题目要求的是最多重复的,那么就来找重复的,可以先枚举一个重复的单元(比如ababab,就枚举ab)的长度, 然后再原串中,会有ch[0],ch[ ...
- GRADLE依赖的统一管理
参考链接:http://stormzhang.com/android/2016/03/13/gradle-config/ 我想大部分人应该都在使用Gradle来依赖管理,还没有使用的去面壁思过,Gra ...
- <强化学习>基本概念
马尔可夫决策过程MDP,是强化学习的基础. MDP --- <S,A,P,R,γ> AGENT STATE ENV REWARD ,由ENV给出.agent处于状态s下,采取acti ...
- 在线答题APP动工前的小总结
7月26号,确定了我们这个暑假的学习任务——做一个答题软件.目的就是为了让我们在做项目中学习,在学习中提高做项目的能力.但是,由于基础知识都还没掌握牢固,所以从本周三确定项目以来,基本没有一点进展. ...