---
title: "Trident实战之计算网站PV"
author: "javafu"
date: "2017-05-24T13:24:33Z"
tags: []
categories: ["企业架构"]
slug: "trident-pv"
draft: false
lastmod: "2017-05-24T13:24:33Z"
id: 2376068
url: "https://javafu.iteye.com/blog/2376068"
---

\[size=large]1、Trident实战之计算网站PV\[/size] \[code="java"] /\** * Trident实战之计算网站PV \*/ public class TridentPVTopo { public static StormTopology buildTopology(LocalDRPC drpc) { Random random = new Random(); String\[] hosts = { "www.taobao.com" }; String\[] session\_id = { "ABYH6Y4V4SCVXTG6DPB4VH9U123", "XXYH6YCGFJYERTT834R52FDXV9U34", "BBYH61456FGHHJ7JL89RG5VV9UYU7", "CYYH6Y2345GHI899OFG4V9U567", "VVVYH6Y4V4SFXZ56JIPDPB4V678" }; String\[] time = { "2014-01-07 08:40:50", "2014-01-07 08:40:51", "2014-01-07 08:40:52", "2014-01-07 08:40:53", "2014-01-07 09:40:49", "2014-01-07 10:40:49", "2014-01-07 11:40:49", "2014-01-07 12:40:49" }; FixedBatchSpout spout = new FixedBatchSpout(new Fields("eachLog"), 3, // 第一个参数表示输出类型，与topo的输入类型对应，第二个参数表示以三行作为一个批次 new Values(hosts\[0] + "\\t" + session\_id\[random.nextInt(5)] + "\\t" + time\[random.nextInt(8)]), new Values(hosts\[0] + "\\t" + session\_id\[random.nextInt(5)] + "\\t" + time\[random.nextInt(8)]), new Values(hosts\[0] + "\\t" + session\_id\[random.nextInt(5)] + "\\t" + time\[random.nextInt(8)])); spout.setCycle(false); /\** * topo处理数据，存入中间存储 \*/ TridentTopology topology = new TridentTopology(); TridentState wordCounts = topology.newStream("spout1", spout)// 获取数据源 .each(new Fields("eachLog"), new Mysplit("\\t"), new Fields("date", "session\_id"))// 第一参数是输入数据类型，第二参数是实现“分割”功能，第三个参数是输出数据类型 .groupBy(new Fields("date"))// 按日期分组 // 持久化到内存，传入session\_id分组,输出pv数据类型 .persistentAggregate(new MemoryMapState.Factory(), new Fields("session\_id"), new Count(), new Fields("pv")); // .parallelismHint(16); /\** * 读取中间存储数据 \*/ topology.newDRPCStream("GetPV", drpc)// 输入函数名称 .each(new Fields("args"), new Split(" "), new Fields("date"))// 对传入参数进行“分割”处理， .groupBy(new Fields("date"))// 用日期进行查询 .stateQuery(wordCounts, new Fields("date"), new MapGet(), new Fields("PV"))// 第一个参数是中间存储。第二个参数是查询参数，可以不输入默认是传入流的值。 .each(new Fields("PV"), new FilterNull());// 查询结果过滤 return topology.build(); } public static void main(String\[] args) throws Exception { // 客户端调用topo Config conf = new Config(); conf.setMaxSpoutPending(20); if (args.length == 0) { LocalDRPC drpc = new LocalDRPC(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("wordCounter", conf, buildTopology(drpc)); for (int i = 0; i &lt; 100; i++) { System.err.println("DRPC RESULT: " + drpc.execute("GetPV", "2014-01-07 2014-01-08")); Thread.sleep(1000); } } else { conf.setNumWorkers(3); StormSubmitter.submitTopologyWithProgressBar(args\[0], conf, buildTopology(null)); } } } \[/code] \[size=large]2、自定义分割数据\[/size] \[code="java"] public class Mysplit extends BaseFunction { /\** * 自定义分割数据 \*/ private static final long serialVersionUID = 1L; String patton = null; public Mysplit(String patton) { this.patton = patton; } public void execute(TridentTuple tuple, TridentCollector collector) { String log = tuple.getString(0); String logArr\[] = log.split(patton); if (logArr.length == 3) { collector.emit(new Values(DateFmt.getCountDate(logArr\[2], DateFmt.date\_short), logArr\[1])); } } } \[/code] \[code="java"] public class Split extends BaseFunction { /\** * 分割数据 \*/ private static final long serialVersionUID = 1L; String patton = null; public Split(String patton) { this.patton = patton; } public void execute(TridentTuple tuple, TridentCollector collector) { String sentence = tuple.getString(0); for (String word : sentence.split(patton)) { collector.emit(new Values(word)); } } } \[/code] \[size=large]3、日期处理类\[/size] \[code="java"] public class DateFmt { /* * 日期处理类 \*/ public static final String date\_long = "yyyy-MM-dd HH:mm:ss" ; public static final String date\_short = "yyyy-MM-dd" ; public static SimpleDateFormat sdf = new SimpleDateFormat(date\_short); public static String getCountDate(String date,String patton) { SimpleDateFormat sdf = new SimpleDateFormat(patton); Calendar cal = Calendar.getInstance(); if (date != null) { try { cal.setTime(sdf.parse(date)) ; } catch (ParseException e) { e.printStackTrace(); } } return sdf.format(cal.getTime()); } public static Date parseDate(String dateStr) throws Exception { return sdf.parse(dateStr); } public static void main(String\[] args) throws Exception{ // System.out.println(DateFmt.getCountDate("2014-03-01 12:13:14", DateFmt.date\_short)); System.out.println(parseDate("2014-05-02").after(parseDate("2014-05-01"))); } } \[/code] \[size=large]4、pom文件\[/size] \[quote] 4.0.0 com.test StormMavenProject jar 0.0.1-SNAPSHOT StormMavenProject http://maven.apache.org org.ow2.asm asm 5.0.3 org.clojure clojure 1.7.0 com.lmax disruptor 3.3.2 com.esotericsoftware kryo 3.0.3 org.apache.logging.log4j log4j-api 2.8 org.apache.logging.log4j log4j-core 2.8 org.slf4j log4j-over-slf4j 1.6.6 org.apache.logging.log4j log4j-slf4j-impl 2.8 com.esotericsoftware minlog 1.3.0 org.objenesis objenesis 2.1 com.esotericsoftware reflectasm 1.10.1 javax.servlet servlet-api 2.5 org.slf4j slf4j-api 1.7.21 org.apache.storm storm-core 1.1.0 org.apache.storm storm-rename-hack 1.1.0 junit junit 3.8.1 test ring-cors ring-cors 0.1.5 org.apache.zookeeper zookeeper 3.4.10 StormMavenProject \[/quote] \[size=large]5、测试结果\[/size] \[quote] DRPC RESULT: \[\["2014-01-07 2014-01-08","2014-01-07",3]] DRPC RESULT: \[\["2014-01-07 2014-01-08","2014-01-07",3]] DRPC RESULT: \[\["2014-01-07 2014-01-08","2014-01-07",3]] \[/quote]