[java]
view plain
copy
- package web;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.hadoop.conf.Configuration;
- 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.MasterNotRunningException;
- import org.apache.hadoop.hbase.ZooKeeperConnectionException;
- import org.apache.hadoop.hbase.client.Delete;
- import org.apache.hadoop.hbase.client.Get;
- import org.apache.hadoop.hbase.client.HBaseAdmin;
- import org.apache.hadoop.hbase.client.HTable;
- import org.apache.hadoop.hbase.client.Put;
- import org.apache.hadoop.hbase.client.Result;
- import org.apache.hadoop.hbase.client.ResultScanner;
- import org.apache.hadoop.hbase.client.Scan;
- import org.apache.hadoop.hbase.util.Bytes;
- public class HbaseServlet extends HttpServlet{
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- PrintWriter writer = null;
- private static Configuration conf = null;
- /**
- * 初始化配置
- */
- static {
- Configuration HBASE_CONFIG = new Configuration();
- //与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同
- HBASE_CONFIG.set("hbase.zookeeper.quorum", "172.20.59.163");
- //与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同
- HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "2181");
- conf = HBaseConfiguration.create(HBASE_CONFIG);
- }
- public void creatTable(String tableName, String[] familys) throws Exception {
- HBaseAdmin admin = new HBaseAdmin(conf);
- if (admin.tableExists(tableName)) {
- System.out.println(writer==null);
- writer.println("table already exists!");
- writer.println("<br>");
- } else {
- HTableDescriptor tableDesc = new HTableDescriptor(tableName);
- for(int i=0; i<familys.length; i++){
- tableDesc.addFamily(new HColumnDescriptor(familys[i]));
- }
- admin.createTable(tableDesc);
- writer.println("create table " + tableName + " ok.");
- writer.println("<br>");
- }
- }
- /**
- * 删除表
- */
- public void deleteTable(String tableName) throws Exception {
- try {
- HBaseAdmin admin = new HBaseAdmin(conf);
- admin.disableTable(tableName);//删除表前应当丢弃该表
- admin.deleteTable(tableName);
- writer.println("delete table " + tableName + " ok.");
- writer.println("<br>");
- } catch (MasterNotRunningException e) {
- e.printStackTrace();
- } catch (ZooKeeperConnectionException e) {
- e.printStackTrace();
- }
- }
- /**
- * 插入一行记录
- */
- public void addRecord (String tableName, String rowKey, String family, String qualifier, String value)
- throws Exception{
- try {
- HTable table = new HTable(conf, tableName);
- Put put = new Put(Bytes.toBytes(rowKey));
- put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
- table.put(put);
- writer.println("insert recored " + rowKey + " to table " + tableName +" ok.");
- writer.println("<br>");
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 删除一行记录
- */
- public void delRecord (String tableName, String rowKey) throws IOException{
- HTable table = new HTable(conf, tableName);
- List list = new ArrayList();
- Delete del = new Delete(rowKey.getBytes());
- list.add(del);
- table.delete(list);
- writer.println("del recored " + rowKey + " ok.");
- writer.println("<br>");
- }
- /**
- * 查找一行记录
- */
- public void getOneRecord (String tableName, String rowKey) throws IOException{
- HTable table = new HTable(conf, tableName);
- Get get = new Get(rowKey.getBytes());
- Result rs = table.get(get);
- for(KeyValue kv : rs.raw()){
- writer.println(new String(kv.getRow()) + " " );
- writer.println("<br>");
- writer.println(new String(kv.getFamily()) + ":" );
- writer.println("<br>");
- writer.println(new String(kv.getQualifier()) + " " );
- writer.println("<br>");
- writer.println(kv.getTimestamp() + " " );
- writer.println("<br>");
- writer.println(new String(kv.getValue()));
- writer.println("<br>");
- }
- }
- /**
- * 显示所有数据
- */
- public void getAllRecord (String tableName) {
- try{
- HTable table = new HTable(conf, tableName);
- Scan s = new Scan();
- ResultScanner ss = table.getScanner(s);
- for(Result r:ss){
- for(KeyValue kv : r.raw()){
- writer.println(new String(kv.getRow()) + " ");
- writer.println("<br>");
- writer.println(new String(kv.getFamily()) + ":");
- writer.println("<br>");
- writer.println(new String(kv.getQualifier()) + " ");
- writer.println("<br>");
- writer.println(kv.getTimestamp() + " ");
- writer.println("<br>");
- writer.println(new String(kv.getValue()));
- writer.println("<br>");
- }
- }
- } catch (IOException e){
- e.printStackTrace();
- }
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=GBK");
- writer = response.getWriter();
- // TODO Auto-generated method stub
- try {
- String tablename = "scores";
- String[] familys = {"grade", "course"};
- this.creatTable(tablename, familys);
- //add record zkb
- this.addRecord(tablename,"zkb","grade","","5");
- this.addRecord(tablename,"zkb","course","","90");
- this.addRecord(tablename,"zkb","course","math","97");
- this.addRecord(tablename,"zkb","course","art","87");
- //add record baoniu
- this.addRecord(tablename,"baoniu","grade","","4");
- this.addRecord(tablename,"baoniu","course","math","89");
- writer.println("===========get one record========");
- writer.println("<br>");
- this.getOneRecord(tablename, "zkb");
- writer.println("===========show all record========");
- writer.println("<br>");
- this.getAllRecord(tablename);
- writer.println("===========del one record========");
- writer.println("<br>");
- this.delRecord(tablename, "baoniu");
- this.getAllRecord(tablename);
- writer.println("===========show all record========");
- writer.println("<br>");
- this.getAllRecord(tablename);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- doPost(request, response);
- }
- }
Eclipse开发web程序,在新建java工程后要补全web程序所需的文件WebRoot下的所有文件,配置启动项路径(tomcat中的配置文件server.xml)
server.xml中host标签内添加:<Context docBase="D:\hadoop1-0-2\work\HbaseTest\WebRoot" path="/hbase" reloadable="false"/>可以添加多个context
docBase是项目HbaseTtest的jsp所在路径,path是虚拟路径,http://localhost:8080/hbase即可。