视频-某hadoop高级应用-搜索提示
看了北风的免费视频,只有一个案例,苦逼买不起几百上千的视频教程
先搭建简单的web项目,基于struts,使用到了bootstrap。
界面:
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml
<struts> <constant name="struts.devMode" value="true" />
<constant name="struts.action.extension" value="action,," /> <package name="ajax" extends="json-default"> <action name="sug" class="org.admln.suggestion.sug">
<result type="json"></result>
</action>
</package> </struts>
index.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>search demo</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css"/>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css"/> <script type="text/javascript">
$(document).ready(function() {
$("#query").autocomplete({
source:function(request,response){
$.ajax({
url:"ajax/sug.action",
dataType:"json",
data:{
query:$("#query").val()
},
success:function(data) {
response($.map(data.result,function(item) {
return {value:item}
}));
}
})
},
minLength:1,
});
})
</script>
</head>
<body>
<div class="container">
<h1>Search Demo</h1>
<div class="well">
<form action="index.jsp">
<label>Search</label><br/>
<input id="query" name="query"/>
<input type="submit" value="查询"/>
</form>
</div>
</div>
</body>
</html>
sug.java
package org.admln.suggestion; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import redis.clients.jedis.Jedis; import com.opensymphony.xwork2.ActionSupport; public class sug extends ActionSupport{
String query; Set<String> result; public String execute() throws Exception {
return SUCCESS;
} public String getQuery() {
return query;
} public void setQuery(String query) {
this.query = query;
} public Set<String> getResult() {
System.out.println(query); Jedis jedis = new Jedis("192.168.126.133");
result = jedis.zrevrange(query, 0, 5); for(String t:result) {
System.out.println(t);
}
return result;
} public void setResult(Set<String> result) {
this.result = result;
}
}
在centos上下载安装tomcat,修改配置文件
设置日志生成格式和目录:
添加tomcat manager用户
然后部署war包
安装redis
。。。
编写hadoop代码
WordCount.java
package org.admln.mr; import java.io.IOException;
import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; public class WordCount extends Configured implements Tool { public static class Map extends
Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString(); String a[] = line.split("\"");
if (a[1].indexOf("index.jsp?query") > 0) {
String b[] = a[1].split("query=| ");
Text word = new Text(b[2]);
context.write(word, one);
}
}
} public static class Reduce extends
Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0; for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
} public static void main(String[] args) throws Exception {
int ret = ToolRunner.run(new WordCount(), args);
System.exit(ret);
} @Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "Load Redis");
job.setJarByClass(WordCount.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(RedisOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1;
}
}
RedisOutputFormat.java
package org.admln.mr; import java.io.IOException; import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import redis.clients.jedis.Jedis; public class RedisOutputFormat<K,V> extends FileOutputFormat<K,V>{ protected static class RedisRecordWriter<K,V> extends RecordWriter<K,V>{
private Jedis jedis; RedisRecordWriter(Jedis jedis) {
this.jedis=jedis;
}
public void close(TaskAttemptContext arg0) throws IOException,InterruptedException{
jedis.disconnect();
}
public void write(K key,V value) throws IOException,
InterruptedException{
boolean nullkey = key == null;
boolean nullvalue = value == null; if(nullkey||nullvalue){
return;
} String s = key.toString();
for(int i=0;i<s.length();i++) {
String k = s.substring(0,i+1);
int score = Integer.parseInt(value.toString());
jedis.zincrby(k,score,s);
}
} }
public RecordWriter<K,V> getRecordWriter(TaskAttemptContext arg0) throws IOException,
InterruptedException{
return new RedisRecordWriter<K,V>(new Jedis("127.0.0.1"));
}
}
(由于作者是1.X的hadoop,用了相比于现在旧的MR API)(跟着做了)
上传数据装载程序运行hadoop MR程序
查看结果
fatjar 在线安装地址:update site:http://kurucz-grafika.de/fatjar/ - http://kurucz-grafika.de/fatjar/
视频地址:http://pan.baidu.com/s/1c0s4zGs
工具地址:http://pan.baidu.com/s/1toy6q
疑问:为什么必须指定输出目录,不指定就会报错?
WordCount程序自定义了输出,OutputFormat类的主要职责是决定数据的存储位置以及写入的方式
RecordWriter 生成<key, value> 对到输出文件。
RecordWriter的实现把作业的输出结果写到 FileSystem。
为了把结果自定义到redis,所以有了RedisOutputFormat.java
它继承自抽象类FileOutputFormat,重写了抽象方法getRecordWriter。并自定义了要返回的RecordWriter类
实现了它的write和close两个方法(必须),用来自定义数据到redis。
hadoop自带OutputFormat类如TextOutputFormat.java,是将reduce产生的数据以行以文本的形式输出到格式好的目录中
所以自定义了OutputFormat后不会再往HDFS上写数据。但是还要写其他东西,好像是成功与否的标识文件
如我运行成功后的output目录:
这个文件里面什么内容都没有,就是个空文件,就是典型的标识文件。觉得意义不是很大,反而很麻烦,相信实际生产中应该自定义吧
(这估计就是为什么必须指定输出目录的原因)
在实际应用中应该是这样的,如果是小网站,日访问量不大的情况下这个项目就用不到hadoop,直接在java代码中没查询一次就把用户查询的词记录到redis一次;但是如果是日流量很大或者某一时间段特别集中的情况下应该就是使用hadoop在“空闲”时间比如晚上十二点去分析日志,批量加入redis
视频-某hadoop高级应用-搜索提示的更多相关文章
- 《Hadoop高级编程》之为Hadoop实现构建企业级安全解决方案
本章内容提要 ● 理解企业级应用的安全顾虑 ● 理解Hadoop尚未为企业级应用提供的安全机制 ● 考察用于构建企业级安全解决方案的方法 第10章讨论了Hadoop安全性以及Hado ...
- C# WinForm 技巧:COMBOBOX搜索提示
comboBox和textBox支持内置的搜索提示功能, 在form的InitializeComponent()中添加如下语句: this.comboBox1.AutoCompleteCustom ...
- Android--多选自动搜索提示
一. 效果图 常见效果,在搜素提示选中之后可以继续搜索添加,选中的词条用特殊字符分开 二. 布局代码 <MultiAutoCompleteTextView android:id="@+ ...
- 高德地图搜索提示获取信息回传activity刷新ui(二)
应用场景: 在主activity中点击进入到另一个activity搜索提示,获取经纬度,点确定返回到主activity,虽然说需求很奇葩,但是遇到了没办法.. 主要包含两部分,搜索提示+activit ...
- 讨论asp.net通过机器cookie仿百度(google)实现搜索input搜索提示弹出框自己主动
为实现自己主动弹出通过用户输入关键词相关的搜索结果,在这里,我举两个解决方案,对于两个不同的方案. 常用的方法是建立一个用户数据库中查找关系表.然后输入用户搜索框keyword异步调用数据表中的相关数 ...
- LanSoEditor_advance1.8.0 视频编辑的高级版本
------------------------------------------2017年1月11日11:18:33------------------------------------- 我们 ...
- lucene的suggest(搜索提示功能的实现)
1.首先引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-suggest --> <!-- ...
- java语言编程使用正则表达式来实现提取(美团 3-5年经验 15-30k 北京 hadoop高级工程)中的3-5和15-30
不多说,直接上干货! 如有这样的一条数据进来: 美团 3-5年经验 15-30k 北京 hadoop高级工程 //正则表达式提取工资值,因为15-30k后面有k,3-5年经验,不干净 public ...
- Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度
Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...
随机推荐
- wuzhicms 后台登录存留时间实现方式
在五指后台管理登录后,超过半小时不操作后台(不刷新页面或切换页面),就就会强制退出后台. 实现的思路是,在每次刷新页面都把当前时间存储下来,利用php的session机制来检测比对当前时间和数据库中存 ...
- Matlab中imshow()函数的使用
imread() 返回的图像类型是uint8类型, 这时用imshow显示图像的时候, imshow会认为输入矩阵的范围在0-255, 如果imshow的参数为double类型的,那么imshow认为 ...
- [HIve - LanguageManual] Joins
Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown ...
- 转】Maven学习总结(五)——聚合与继承
原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4058008.html 感谢! 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1. ...
- 【转】nginx的优缺点
原博文出自于:http://blog.csdn.net/a454211787/article/details/22494485 感谢! 1.nginx相对于apache优点: 轻量级同样起we ...
- internet访问局域网内部方法之----------路由器端口映射
很多人每天都问为什么要端口映射?例如:通过路由器上网的,网站自己可以访问,但是别人就不能:输入127.0.0.1可以访问,别人还是看不到:输入localhost可以看到,但是别人就是看不到,气人啊-没 ...
- C#类、静态类、静态变量,初始化执行顺序
执行顺序: 类的静态变量 ↓ 类的静态构造函数 ↓ 类的普通变量 ↓ 基类的静态变量 ↓ 基类的静态构造函数 ↓ 基类的普通变量 ↓ 基类的构造函数 ↓ 类的构造函数
- OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltas
OLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDel ...
- UVaLive 7267 Mysterious Antiques in Sackler Museum (if-else,枚举)
题意:给定四个矩形,要求从中选出三个,能不能拼成一个矩形. 析:说到这个题,我还坑了队友一次,读题读错了,我直接看的样例,以为是四个能不能组成,然后我们三个就拼命想有什么简便方法,后来没办法了,直接暴 ...
- dll开发中遇到的问题
刚碰到个问题,我的一个项目中引用了一个dll,这个dll又引用了另一个dll,我把这俩个都放在bin文件夹下,但是会报错,说第二个dll找不到.把它放到系统文件夹system32下就没事了. 但是遇到 ...