Jsp显示Hbase的数据

 

软件:

Jdk1.7eclipse eevmware安装好的CentOs6.5Hadoop-2.6.0hbase-0.99.2

 

 

1、建一个普通的动态Web程序,用导jar包运行,不用mavenant

 

2、把HBasehadoop的相应的jar包导进工程中;

主要是运行HBase  API,把指定表名和行键的内容读出来。

并添加 log4j.properties 文件。

 

3、创建一个servlet类,并创建jsp文件,把HBaselib里的jar包拷进web/web_ceshi2/WebContent/WEB-INF/lib    里面去。

 

项目目录结构:

 Jsp显示HBase的数据

 

 代码:

Output_HBase.Java

[java] view plain copy
 print?
  1. package control;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import model.Article;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.hbase.KeyValue;  
  9. import org.apache.hadoop.hbase.client.Get;  
  10. import org.apache.hadoop.hbase.client.HBaseAdmin;  
  11. import org.apache.hadoop.hbase.client.HTable;  
  12. import org.apache.hadoop.hbase.client.HTableInterface;  
  13. import org.apache.hadoop.hbase.client.HTablePool;  
  14. import org.apache.hadoop.hbase.client.Result;  
  15. import org.apache.hadoop.hbase.client.ResultScanner;  
  16. import org.apache.hadoop.hbase.client.Scan;  
  17.   
  18.   
  19.   
  20. @SuppressWarnings("deprecation")  
  21. public class Output_HBase {  
  22.       
  23.     HBaseAdmin admin=null;  
  24.     Configuration conf=null;  
  25.     /** 
  26.      * 构造函数加载配置 
  27.      */  
  28.     public Output_HBase(){  
  29.         conf = new Configuration();  
  30.         conf.set("hbase.zookeeper.quorum""192.168.1.200:2181");  
  31.         conf.set("hbase.rootdir""hdfs://192.168.1.200:9000/hbase");  
  32.         System.out.println("初始化完毕");  
  33.         try {  
  34.             admin = new HBaseAdmin(conf);  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.       
  40.     public static void main(String[] args) {  
  41.         Output_HBase o=new Output_HBase();  
  42.         o.get("article""1");  
  43.     }  
  44.     public  Article  get(String tableName, String row) {  
  45.         System.out.println("get执行了1");  
  46.         @SuppressWarnings("resource")  
  47.         HTablePool hTablePool = new HTablePool(conf, 1000);  
  48.         HTableInterface table = hTablePool.getTable(tableName);  
  49.         System.out.println("get执行了2");  
  50.         Get get = new Get(row.getBytes());  
  51.         System.out.println("get执行了3");  
  52.         Article article = null;  
  53.         try {  
  54.             System.out.println("get执行了4");  
  55.             Result result = table.get(get);  
  56.             System.out.println("get执行了5");  
  57.             KeyValue[] raw = result.raw();  
  58.             System.out.println("get执行了6");  
  59.             if (raw.length == 5) {  
  60.                 System.out.println("get执行了7");  
  61.                 article = new Article();  
  62.                 article.setId(new String(raw[3].getValue()));   
  63.                 article.setTitle(new String(raw[4].getValue()));  
  64.                 article.setAuthor(new String(raw[0].getValue()));  
  65.                 article.setDescribe(new String(raw[2].getValue()));  
  66.                 article.setContent(new String(raw[1].getValue()));  
  67.             }  
  68.             //new Start(article.getId(), article.getTitle(), article.getAuthor(), article.getDescribe(), article.getContent());  
  69.             System.out.println("执行了啊--ID"+article.getId()+"\n");  
  70.             System.out.println("执行了啊--标题"+article.getTitle()+"\n");  
  71.             System.out.println("执行了啊--作者"+article.getAuthor()+"\n");  
  72.             System.out.println("执行了啊--描述"+article.getDescribe()+"\n");  
  73.             System.out.println("执行了啊--正文"+article.getContent()+"\n");  
  74.         } catch (IOException e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.         return article;  
  78.     }  
  79.       
  80.     /** 
  81.      * 获取表的所有数据 
  82.      * @param tableName 
  83.      */  
  84.     public void getALLData(String tableName) {  
  85.         try {  
  86.             @SuppressWarnings("resource")  
  87.             HTable hTable = new HTable(conf, tableName);  
  88.             Scan scan = new Scan();  
  89.             ResultScanner scanner = hTable.getScanner(scan);  
  90.             for (Result result : scanner) {  
  91.                 if(result.raw().length==0){  
  92.                     System.out.println(tableName+" 表数据为空!");  
  93.                 }else{  
  94.                     for (KeyValue kv: result.raw()){  
  95.                         System.out.println(new String(kv.getKey())+"\t"+new String(kv.getValue()));  
  96.                     }  
  97.                 }  
  98.             }  
  99.         } catch (IOException e) {  
  100.             e.printStackTrace();  
  101.         }  
  102.           
  103.     }  
  104. }  


 OutPrx.java:

[java] view plain copy
 print?
  1. package control;  
  2.   
  3. import model.Article;  
  4.   
  5. public class OutPrx {  
  6.     private String id;  
  7.     private String title;  
  8.     private String author;  
  9.     private String describe;  
  10.     private String content;  
  11.       
  12.     public OutPrx() {  
  13.           
  14.     }  
  15.   
  16.     public void get(){  
  17.         System.out.println("这里这行了1");  
  18.         Output_HBase out1=new Output_HBase();  
  19.         System.out.println("这里这行了2");  
  20.         Article article=out1.get("article""520");  
  21.           
  22.         System.out.println("这里这行了3");  
  23.         this.id=article.getId();  
  24.         this.title=article.getTitle();  
  25.         this.author=article.getAuthor();  
  26.         this.describe=article.getDescribe();  
  27.         this.content=article.getContent();  
  28.       
  29.         System.out.println("这里这行了4");  
  30.           
  31.     }  
  32.       
  33.       
  34.       
  35.     public String getId() {  
  36.         return id;  
  37.     }  
  38.   
  39.     public void setId(String id) {  
  40.         this.id = id;  
  41.     }  
  42.   
  43.     public String getTitle() {  
  44.         return title;  
  45.     }  
  46.   
  47.     public void setTitle(String title) {  
  48.         this.title = title;  
  49.     }  
  50.   
  51.     public String getAuthor() {  
  52.         return author;  
  53.     }  
  54.   
  55.     public void setAuthor(String author) {  
  56.         this.author = author;  
  57.     }  
  58.   
  59.     public String getDescribe() {  
  60.         return describe;  
  61.     }  
  62.   
  63.     public void setDescribe(String describe) {  
  64.         this.describe = describe;  
  65.     }  
  66.   
  67.     public String getContent() {  
  68.         return content;  
  69.     }  
  70.   
  71.     public void setContent(String content) {  
  72.         this.content = content;  
  73.     }  
  74.       
  75.       
  76. }  



 Article:

[java] view plain copy
 print?
  1. package model;  
  2.   
  3. public class Article {  
  4.     private String id;  
  5.     private String title;  
  6.     private String describe;  
  7.     private String content;  
  8.     private String author;  
  9.       
  10.     public Article(){  
  11.           
  12.     }  
  13.       
  14.     public Article(String id,String title,String describe,String content,String author){  
  15.         this.id=id;  
  16.         this.title=title;  
  17.         this.describe=describe;  
  18.         this.content=content;  
  19.         this.author=author;  
  20.     }  
  21.       
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.     public void setId(String id) {  
  26.         this.id = id;  
  27.     }  
  28.     public String getTitle() {  
  29.         return title;  
  30.     }  
  31.     public void setTitle(String title) {  
  32.         this.title = title;  
  33.     }  
  34.     public String getDescribe() {  
  35.         return describe;  
  36.     }  
  37.     public void setDescribe(String describe) {  
  38.         this.describe = describe;  
  39.     }  
  40.     public String getContent() {  
  41.         return content;  
  42.     }  
  43.     public void setContent(String content) {  
  44.         this.content = content;  
  45.     }  
  46.     public String getAuthor() {  
  47.         return author;  
  48.     }  
  49.     public void setAuthor(String author) {  
  50.         this.author = author;  
  51.     }  
  52.       
  53.     public String toString(){  
  54.         return this.id+"\t"+this.title+"\t"+this.author+"\t"+this.describe+"\t"+this.content;  
  55.     }  
  56. }  


 

(这个类跟显示无关,可以忽略)

Import_HBase:

[java] view plain copy
 print?
  1. package control;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.hbase.HBaseConfiguration;  
  7. import org.apache.hadoop.hbase.client.Mutation;  
  8. import org.apache.hadoop.hbase.client.Put;  
  9. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;  
  10. import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;  
  11. import org.apache.hadoop.hbase.mapreduce.TableReducer;  
  12. import org.apache.hadoop.io.LongWritable;  
  13. import org.apache.hadoop.io.NullWritable;  
  14. import org.apache.hadoop.io.Text;  
  15. import org.apache.hadoop.mapreduce.Job;  
  16. import org.apache.hadoop.mapreduce.Mapper;  
  17. import org.apache.hadoop.mapreduce.Reducer;  
  18. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;  
  20.   
  21. public class Import_HBase {  
  22.     public static class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text>{  
  23.         @Override  
  24.         protected void map(LongWritable key, Text value,  
  25.                 Mapper<LongWritable, Text, LongWritable, Text>.Context context)  
  26.                 throws IOException, InterruptedException {  
  27.             //设置行键+内容  
  28.             context.write(key, value);  
  29.         }  
  30.     }  
  31.       
  32.     public static class MyReduce extends TableReducer<LongWritable, Text, NullWritable>{  
  33.         private String family="info";  
  34.         @Override  
  35.         protected void reduce(LongWritable arg0, Iterable<Text> v2s,  
  36.                 Reducer<LongWritable, Text, NullWritable, Mutation>.Context context)  
  37.                 throws IOException, InterruptedException {  
  38.             for (Text value : v2s) {  
  39.                 String line=value.toString();  
  40.                 String[] splited=line.split("\t");  
  41.                 String rowkey=splited[0];  
  42.                 Put put = new Put(rowkey.getBytes());  
  43.                 put.add(family.getBytes(), "id".getBytes(), splited[0].getBytes());  
  44.                 put.add(family.getBytes(), "title".getBytes(), splited[1].getBytes());  
  45.                 put.add(family.getBytes(), "author".getBytes(), splited[2].getBytes());  
  46.                 put.add(family.getBytes(), "describe".getBytes(), splited[3].getBytes());  
  47.                 put.add(family.getBytes(), "content".getBytes(), splited[4].getBytes());  
  48.                 context.write(NullWritable.get(), put);  
  49.             }  
  50.               
  51.         }  
  52.     }  
  53.       
  54.     private static String tableName="article";  
  55.     @SuppressWarnings("deprecation")  
  56.     public static void main(String[] args) throws Exception {  
  57.         Configuration conf = HBaseConfiguration.create();  
  58.         conf.set("hbase.rootdir""hdfs://192.168.1.200:9000/hbase");  
  59.         conf.set("hbase.zookeeper.quorum""192.168.1.200:2181");  
  60.         conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);  
  61.           
  62.         Job job = new Job(conf, Import_HBase.class.getSimpleName());  
  63.         TableMapReduceUtil.addDependencyJars(job);  
  64.         job.setJarByClass(Import_HBase.class);  
  65.           
  66.         job.setMapperClass(MyMapper.class);  
  67.         job.setReducerClass(MyReduce.class);  
  68.           
  69.         job.setMapOutputKeyClass(LongWritable.class);  
  70.         job.setMapOutputValueClass(Text.class);  
  71.           
  72.         job.setInputFormatClass(TextInputFormat.class);  
  73.         job.setOutputFormatClass(TableOutputFormat.class);  
  74.           
  75.         FileInputFormat.setInputPaths(job, "hdfs://192.168.1.200:9000/hbase_solr");  
  76.           
  77.         job.waitForCompletion(true);  
  78.     }  
  79. }  


 

HttpServlet:

[java] view plain copy
 print?
  1. package servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import javax.servlet.annotation.WebServlet;   
  10.   
  11. import control.OutPrx;  
  12. /** 
  13.  * Servlet implementation class Test 
  14.  */  
  15. @WebServlet("/Test")  
  16. public class Test extends HttpServlet {  
  17.     private static final long serialVersionUID = 1L;  
  18.          
  19.     /** 
  20.      * @see HttpServlet#HttpServlet() 
  21.      */  
  22.     public Test() {  
  23.         super();  
  24.         // TODO Auto-generated constructor stub  
  25.     }  
  26.   
  27.     /** 
  28.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
  29.      */  
  30.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  31.         // TODO Auto-generated method stub  
  32.         OutPrx oo=new OutPrx();  
  33.         oo.get();  
  34.           
  35.         request.setAttribute("id",oo.getId());//存值  
  36.         request.setAttribute("title",oo.getTitle());//存值  
  37.         request.setAttribute("author",oo.getAuthor());//存值  
  38.         request.setAttribute("describe",oo.getDescribe());//存值  
  39.         request.setAttribute("content",oo.getContent());//存值  
  40.         
  41.         System.out.println("====================================================================================");  
  42.         System.out.println("执行了啊--ID"+oo.getId()+"\n");  
  43.         System.out.println("执行了啊--标题"+oo.getTitle()+"\n");  
  44.         System.out.println("执行了啊--作者"+oo.getAuthor()+"\n");  
  45.         System.out.println("执行了啊--描述"+oo.getDescribe()+"\n");  
  46.         System.out.println("执行了啊--正文"+oo.getContent()+"\n");  
  47.         request.getRequestDispatcher("/hello.jsp").forward(request,response);  
  48.           
  49.         System.out.println("-----------------------------------------------------------------------------");  
  50.     }  
  51.   
  52.     /** 
  53.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
  54.      */  
  55.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
  56.         // TODO Auto-generated method stub  
  57.     }  
  58.   
  59. }  

 

log4j.properties:

[java] view plain copy
 print?
  1. ### set log levels - for more verbose logging change 'info' to 'debug' ###  
  2. log4j.rootLogger=DEBUG,stdout,file  
  3.   
  4. ## Disable other log    
  5. #log4j.logger.org.springframework=OFF    
  6. #log4j.logger.org.apache.struts2=OFF    
  7. #log4j.logger.com.opensymphony.xwork2=OFF    
  8. #log4j.logger.com.ibatis=OFF    
  9. #log4j.logger.org.hibernate=OFF  
  10.   
  11. ### direct log messages to stdout ###  
  12. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  13. log4j.appender.stdout.Target=System.out  
  14. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  15. log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  16.   
  17. ### direct messages to file mylog.log ###  
  18. log4j.appender.file=org.apache.log4j.FileAppender  
  19. log4j.appender.file.File=logs/spider_web.log  
  20. log4j.appender.file.DatePattern = '.'yyyy-MM-dd  
  21. log4j.appender.file.layout=org.apache.log4j.PatternLayout  
  22. log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n  
  23.   
  24.   
  25.   
  26. ### direct messages to file mylog.log ###  
  27. log4j.logger.cn.superwu.crm.service=INFO, ServerDailyRollingFile  
  28. log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender   
  29. log4j.appender.ServerDailyRollingFile.File=logs/biapp-service.log   
  30. log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd   
  31. log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout  
  32. log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d{yyy-MM-dd HH:mm:ss } -[%r]-[%p] %m%n  
  33.   
  34. #log4j.logger.cn.superwu.crm.service.DrmService=INFO, ServerDailyRollingFile  
  35. #log4j.appender.drm=org.apache.log4j.RollingFileAppender  
  36. #log4j.appender.drm.File=logs/crm-drm.log  
  37. #log4j.appender.drm.MaxFileSize=10000KB  
  38. #log4j.appender.drm.MaxBackupIndex=10  
  39. #log4j.appender.drm.Append=true  
  40. #log4j.appender.drm.layout=org.apache.log4j.PatternLayout  
  41. #log4j.appender.drm.layout.ConversionPattern=[start]%d{yyyy/MM/dd/ HH:mm:ss}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n  
  42. #log4j.appender.drm.layout.ConversionPattern=[%5p]%d{yyyy-MM-dd HH:mm:ss}[%c](%F:%L)%n%m%n%n  


hello.jsp:

[java] view plain copy
 print?
  1. <pre name="code" class="java"><%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <% String id = (String)request.getAttribute("id");%>  
  11. <% String title = (String)request.getAttribute("title");%>  
  12. <% String author = (String)request.getAttribute("author");%>  
  13. <% String describe = (String)request.getAttribute("describe");%>  
  14. <% String content = (String)request.getAttribute("content");%>  
  15.   
  16.   
  17. <%="文章ID为:"+id %> <br><br>  
  18. <%="文章标题为:"+title %> <br><br>  
  19. <%="文章作者为:"+author %> <br><br>  
  20. <%="文章描述为:"+describe %> <br><br>  
  21. <%="文章正文为:"+content %> <br><br>  
  22. </body>  
  23. </html>  





右键servlet类运行就可以了,运行界面如下:


Eclipse 显示的整个界面:

 Jsp显示HBase的数据

 

Eclipse显示的web界面:

 Jsp显示HBase的数据

 

 

Eclipse显示的项目工程界面:

 Jsp显示HBase的数据

 

在网页显示的界面:

 Jsp显示HBase的数据

 

 

查看HBase的数据:

 Jsp显示HBase的数据

 

查看HBase表结构:

 Jsp显示HBase的数据

 

 

运行Web程序之前,必须确保HadoopHBase是开启的:

 Jsp显示HBase的数据



如果加载有错误,可以重新创建一个项目。

例如:@WebServlet("/Test")

还有显示提示找不到什么类的等错误,先自己重新创建个项目运行。

创建了还不好使,在具体情况具体分析。


这个纯属自己自娱自乐,当然用maven更好了,用springmvc更好了。

相关文章:

  • 2021-11-29
  • 2021-09-11
  • 2021-04-18
  • 2022-12-23
  • 2022-12-23
  • 2021-11-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-06-08
  • 2021-11-29
  • 2021-11-29
  • 2021-11-28
相关资源
相似解决方案