很長一段時間沒有關注hadoop,忽然間有興致,因而動手又研究一下
準備的基礎以下:
下載hadoop-1.0.4,tomcat,lucene因爲關於hadoop的基礎資料也挺多的,因此這裏只寫一段關於hadoop+lucene+web小綜合的demo設計思路, 但願對初學入門者有所啓示,以下圖:

本例子會引用最初hadoop提供的wordcount例子,具體的代碼能夠經過網上獲得,而後運行.
workcount輸入文件名稱inputfile.txt 包含內容:
Hello World Bye World goole
workcount輸出的文件名稱part-00000 ,包含內容:
Bye 1
Hello 1
World 2
goole 1
Hello 1
World 2
goole 1
假設:在生成part-00000 文件以後,立刻調用lucene建立索引文件
建立索引文件的java代碼以下:
// @author minn IndexWriter writer = new IndexWriter(PRE_PATH+ "index",new SimpleAnalyzer(),true); FileReader read = new FileReader(PRE_PATH+"part-00000"); BufferedReader br = new BufferedReader(read); String row; Document document =null; while((row = br.readLine())!=null){ String tmp[]=row.split("\\s+"); document=new Document(); document.add(new Field("name",tmp[0],Field.Store.YES,Field.Index.TOKENIZED)); document.add(new Field("count",tmp[1],Field.Store.YES,Field.Index.TOKENIZED)); writer.addDocument(document); } writer.optimize(); writer.close();
查找java代碼:
/** * * @author minn * */ public static String searchWord(String word) throws Exception { Analyzer analyzer = new StandardAnalyzer(); String result = ""; IndexSearcher indexSearcher = null; indexSearcher = new IndexSearcher(PRE_PATH + "index"); QueryParser queryParser = new QueryParser("name", analyzer); Query query = null; query = queryParser.parse(word); if (null != query && null != indexSearcher) { Hits hits = indexSearcher.search(query); result = hits.doc(0).get("count"); } return result; }
servlet調用lucene查找:
package servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lucene.SearchHadoopFile; /** * Servlet implementation class HadoopServlet * @author minn */ public class HadoopServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HadoopServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stubintl String result=""; PrintWriter out=response.getWriter(); try { result= SearchHadoopFile.searchWord(request.getParameter("word")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } out.write(result); out.flush(); out.close(); } }
web頁面:
寫道
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>hadoop lucene test</title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function queryWord(){
var word=$('#searchWord_id').val();
$.ajax({
type: "POST",
url: "hServlet",
data: { word: word }
}).done(function( msg ) {
$('#count_id').text('結果:'+msg);
});
}
</script>
</head>
<body>
<div>關鍵字:Hello,World bye,google</div>
查找字段:<input id="searchWord_id" type="text"><input type="button" value="查找">
<div id="count_id"></div>
</body>
</html>
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>hadoop lucene test</title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
function queryWord(){
var word=$('#searchWord_id').val();
$.ajax({
type: "POST",
url: "hServlet",
data: { word: word }
}).done(function( msg ) {
$('#count_id').text('結果:'+msg);
});
}
</script>
</head>
<body>
<div>關鍵字:Hello,World bye,google</div>
查找字段:<input id="searchWord_id" type="text"><input type="button" value="查找">
<div id="count_id"></div>
</body>
</html>
效果界面以下:
