ul-wukong 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +60 -0
  3. data/.gitmodules +6 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +19 -0
  6. data/.yardopts +6 -0
  7. data/CHANGELOG.md +7 -0
  8. data/Gemfile +17 -0
  9. data/Guardfile +12 -0
  10. data/LICENSE.md +95 -0
  11. data/NOTES-travis.md +31 -0
  12. data/README-old.md +422 -0
  13. data/README.md +1308 -0
  14. data/Rakefile +28 -0
  15. data/TODO.md +99 -0
  16. data/bin/cutc +30 -0
  17. data/bin/cuttab +5 -0
  18. data/bin/greptrue +6 -0
  19. data/bin/md5sort +20 -0
  20. data/bin/setcat +11 -0
  21. data/bin/tabchar +5 -0
  22. data/bin/uniq-ord +59 -0
  23. data/bin/uniqc +3 -0
  24. data/bin/wu +34 -0
  25. data/bin/wu-clean-encoding +31 -0
  26. data/bin/wu-date +13 -0
  27. data/bin/wu-datetime +13 -0
  28. data/bin/wu-hist +3 -0
  29. data/bin/wu-lign +186 -0
  30. data/bin/wu-local +4 -0
  31. data/bin/wu-plus +9 -0
  32. data/bin/wu-source +5 -0
  33. data/bin/wu-sum +31 -0
  34. data/diagrams/wu_local.dot +39 -0
  35. data/diagrams/wu_local.dot.png +0 -0
  36. data/examples/Gemfile +38 -0
  37. data/examples/README.md +9 -0
  38. data/examples/basic/string_reverser.rb +23 -0
  39. data/examples/basic/tiny_count.rb +8 -0
  40. data/examples/basic/word_count/accumulator.rb +26 -0
  41. data/examples/basic/word_count/tokenizer.rb +13 -0
  42. data/examples/basic/word_count/word_count.rb +6 -0
  43. data/examples/dataflow/scraper_macro_flow.rb +28 -0
  44. data/examples/deploy_pack/Gemfile +6 -0
  45. data/examples/deploy_pack/README.md +6 -0
  46. data/examples/deploy_pack/a/b/c/.gitkeep +0 -0
  47. data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
  48. data/examples/deploy_pack/config/environment.rb +1 -0
  49. data/examples/dsl/dataflow/fibonacci_series.rb +101 -0
  50. data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
  51. data/examples/dsl/dataflow/simple.rb +12 -0
  52. data/examples/dsl/dataflow/telegram.rb +45 -0
  53. data/examples/dsl/workflow/cherry_pie.dot +97 -0
  54. data/examples/dsl/workflow/cherry_pie.md +104 -0
  55. data/examples/dsl/workflow/cherry_pie.png +0 -0
  56. data/examples/dsl/workflow/cherry_pie.rb +101 -0
  57. data/examples/empty/.gitkeep +0 -0
  58. data/examples/examples_helper.rb +9 -0
  59. data/examples/geo.rb +4 -0
  60. data/examples/geo/geo_grids.numbers +0 -0
  61. data/examples/geo/geolocated.rb +331 -0
  62. data/examples/geo/quadtile.rb +69 -0
  63. data/examples/geo/spec/geolocated_spec.rb +247 -0
  64. data/examples/geo/tile_fetcher.rb +77 -0
  65. data/examples/graph/implied_geolocation/README.md +63 -0
  66. data/examples/graph/minimum_spanning_tree/airfares_graphviz.rb +73 -0
  67. data/examples/improver/tweet_summary.rb +73 -0
  68. data/examples/loadable.rb +2 -0
  69. data/examples/munging/airline_flights/airline_flights.rake +83 -0
  70. data/examples/munging/airline_flights/airplane.rb +0 -0
  71. data/examples/munging/airline_flights/airport_id_unification.rb +129 -0
  72. data/examples/munging/airline_flights/airport_ok_chars.rb +4 -0
  73. data/examples/munging/airline_flights/indexable.rb +75 -0
  74. data/examples/munging/airline_flights/indexable_spec.rb +90 -0
  75. data/examples/munging/airline_flights/reconcile_airports.rb +142 -0
  76. data/examples/munging/airline_flights/tasks.rake +83 -0
  77. data/examples/munging/airline_flights/topcities.rb +167 -0
  78. data/examples/munging/geo/geo_json.rb +54 -0
  79. data/examples/munging/geo/geo_models.rb +69 -0
  80. data/examples/munging/geo/geonames_models.rb +107 -0
  81. data/examples/munging/geo/iso_codes.rb +172 -0
  82. data/examples/munging/geo/reconcile_countries.rb +124 -0
  83. data/examples/munging/geo/tasks.rake +71 -0
  84. data/examples/munging/wikipedia/articles/extract_articles-parsed.rb +79 -0
  85. data/examples/munging/wikipedia/articles/extract_articles-templated.rb +136 -0
  86. data/examples/munging/wikipedia/articles/textualize_articles.rb +54 -0
  87. data/examples/munging/wikipedia/articles/verify_structure.rb +43 -0
  88. data/examples/munging/wikipedia/articles/wp2txt-LICENSE.txt +22 -0
  89. data/examples/munging/wikipedia/articles/wp2txt_article.rb +259 -0
  90. data/examples/munging/wikipedia/articles/wp2txt_utils.rb +452 -0
  91. data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +5 -0
  92. data/examples/munging/wikipedia/dbpedia/dbpedia_extract_geocoordinates.rb +78 -0
  93. data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
  94. data/examples/munging/wikipedia/dbpedia/extract_links.rb +260 -0
  95. data/examples/munging/wikipedia/dbpedia/sameas_extractor.rb +20 -0
  96. data/examples/rake_helper.rb +97 -0
  97. data/examples/ruby_project/Gemfile +6 -0
  98. data/examples/ruby_project/README.md +6 -0
  99. data/examples/ruby_project/a/b/c/.gitkeep +0 -0
  100. data/examples/server_logs/geo_ip_mapping/munge_geolite.rb +82 -0
  101. data/examples/server_logs/logline.rb +95 -0
  102. data/examples/server_logs/models.rb +66 -0
  103. data/examples/server_logs/page_counts.pig +48 -0
  104. data/examples/server_logs/server_logs-01-parse-script.rb +13 -0
  105. data/examples/server_logs/server_logs-02-histograms-full.rb +33 -0
  106. data/examples/server_logs/server_logs-02-histograms-mapper.rb +14 -0
  107. data/examples/server_logs/server_logs-03-breadcrumbs-full.rb +71 -0
  108. data/examples/server_logs/server_logs-04-page_page_edges-full.rb +40 -0
  109. data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
  110. data/examples/serverlogs/models/logline.rb +102 -0
  111. data/examples/serverlogs/parser/apache_parser_widget.rb +46 -0
  112. data/examples/serverlogs/visit_paths/common.rb +4 -0
  113. data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
  114. data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
  115. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
  116. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
  117. data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
  118. data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
  119. data/examples/splitter.rb +94 -0
  120. data/examples/string_reverser.rb +7 -0
  121. data/examples/text/pig_latin/pig_latinizer.rb +35 -0
  122. data/examples/text/pig_latin/pig_latinizer_widget.rb +16 -0
  123. data/examples/text/regional_flavor/README.md +14 -0
  124. data/examples/text/regional_flavor/article_wordbags.pig +39 -0
  125. data/examples/text/regional_flavor/j01-article_wordbags.rb +4 -0
  126. data/examples/text/regional_flavor/simple_pig_script.pig +27 -0
  127. data/examples/twitter.rb +5 -0
  128. data/lib/hanuman.rb +36 -0
  129. data/lib/hanuman/graph.rb +97 -0
  130. data/lib/hanuman/graphvizzer.rb +206 -0
  131. data/lib/hanuman/graphvizzer/gv_models.rb +161 -0
  132. data/lib/hanuman/graphvizzer/gv_presenter.rb +97 -0
  133. data/lib/hanuman/link.rb +35 -0
  134. data/lib/hanuman/registry.rb +46 -0
  135. data/lib/hanuman/stage.rb +128 -0
  136. data/lib/hanuman/tree.rb +67 -0
  137. data/lib/wu/geo.rb +4 -0
  138. data/lib/wu/geo/geo_grids.numbers +0 -0
  139. data/lib/wu/geo/geolocated.rb +331 -0
  140. data/lib/wu/geo/quadtile.rb +69 -0
  141. data/lib/wu/graph/union_find.rb +62 -0
  142. data/lib/wu/model/reconcilable.rb +63 -0
  143. data/lib/wu/munging.rb +71 -0
  144. data/lib/wu/social/models/twitter.rb +31 -0
  145. data/lib/wu/wikipedia/models.rb +20 -0
  146. data/lib/wukong.rb +54 -0
  147. data/lib/wukong/dataflow.rb +43 -0
  148. data/lib/wukong/doc_helpers.rb +14 -0
  149. data/lib/wukong/doc_helpers/dataflow_handler.rb +29 -0
  150. data/lib/wukong/doc_helpers/field_handler.rb +91 -0
  151. data/lib/wukong/doc_helpers/processor_handler.rb +29 -0
  152. data/lib/wukong/driver.rb +214 -0
  153. data/lib/wukong/driver/event_machine_driver.rb +15 -0
  154. data/lib/wukong/driver/wiring.rb +68 -0
  155. data/lib/wukong/local.rb +42 -0
  156. data/lib/wukong/local/runner.rb +96 -0
  157. data/lib/wukong/local/stdio_driver.rb +104 -0
  158. data/lib/wukong/logger.rb +102 -0
  159. data/lib/wukong/model/faker.rb +136 -0
  160. data/lib/wukong/model/flatpack_parser/flat.rb +60 -0
  161. data/lib/wukong/model/flatpack_parser/flatpack.rb +4 -0
  162. data/lib/wukong/model/flatpack_parser/lang.rb +46 -0
  163. data/lib/wukong/model/flatpack_parser/parser.rb +55 -0
  164. data/lib/wukong/model/flatpack_parser/tokens.rb +130 -0
  165. data/lib/wukong/plugin.rb +48 -0
  166. data/lib/wukong/processor.rb +110 -0
  167. data/lib/wukong/rake_helper.rb +6 -0
  168. data/lib/wukong/runner.rb +169 -0
  169. data/lib/wukong/runner/boot_sequence.rb +123 -0
  170. data/lib/wukong/runner/code_loader.rb +52 -0
  171. data/lib/wukong/runner/command_runner.rb +44 -0
  172. data/lib/wukong/runner/deploy_pack_loader.rb +75 -0
  173. data/lib/wukong/runner/help_message.rb +42 -0
  174. data/lib/wukong/source.rb +33 -0
  175. data/lib/wukong/source/source_driver.rb +74 -0
  176. data/lib/wukong/source/source_runner.rb +38 -0
  177. data/lib/wukong/spec_helpers.rb +74 -0
  178. data/lib/wukong/spec_helpers/integration_tests.rb +150 -0
  179. data/lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb +207 -0
  180. data/lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb +97 -0
  181. data/lib/wukong/spec_helpers/shared_examples.rb +22 -0
  182. data/lib/wukong/spec_helpers/unit_tests.rb +135 -0
  183. data/lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb +132 -0
  184. data/lib/wukong/spec_helpers/unit_tests/unit_test_matchers.rb +169 -0
  185. data/lib/wukong/spec_helpers/unit_tests/unit_test_runner.rb +60 -0
  186. data/lib/wukong/version.rb +3 -0
  187. data/lib/wukong/widget/echo.rb +55 -0
  188. data/lib/wukong/widget/extract.rb +122 -0
  189. data/lib/wukong/widget/filters.rb +452 -0
  190. data/lib/wukong/widget/logger.rb +56 -0
  191. data/lib/wukong/widget/operators.rb +82 -0
  192. data/lib/wukong/widget/reducers.rb +10 -0
  193. data/lib/wukong/widget/reducers/accumulator.rb +73 -0
  194. data/lib/wukong/widget/reducers/bin.rb +368 -0
  195. data/lib/wukong/widget/reducers/count.rb +73 -0
  196. data/lib/wukong/widget/reducers/group.rb +128 -0
  197. data/lib/wukong/widget/reducers/group_concat.rb +98 -0
  198. data/lib/wukong/widget/reducers/improver.rb +71 -0
  199. data/lib/wukong/widget/reducers/join_xml.rb +37 -0
  200. data/lib/wukong/widget/reducers/moments.rb +72 -0
  201. data/lib/wukong/widget/reducers/sort.rb +180 -0
  202. data/lib/wukong/widget/reducers/uniq.rb +91 -0
  203. data/lib/wukong/widget/serializers.rb +317 -0
  204. data/lib/wukong/widget/utils.rb +46 -0
  205. data/lib/wukong/widgets.rb +7 -0
  206. data/spec/examples/dataflow/fibonacci_series_spec.rb +18 -0
  207. data/spec/examples/dataflow/parse_apache_logs_spec.rb +8 -0
  208. data/spec/examples/dataflow/parsing_spec.rb +14 -0
  209. data/spec/examples/dataflow/simple_spec.rb +34 -0
  210. data/spec/examples/dataflow/telegram_spec.rb +43 -0
  211. data/spec/examples/graph/minimum_spanning_tree_spec.rb +34 -0
  212. data/spec/examples/munging/airline_flights/identifiers_spec.rb +16 -0
  213. data/spec/examples/munging/airline_flights_spec.rb +202 -0
  214. data/spec/examples/text/pig_latin_spec.rb +18 -0
  215. data/spec/examples/workflow/cherry_pie_spec.rb +36 -0
  216. data/spec/hanuman/graph_spec.rb +119 -0
  217. data/spec/hanuman/hanuman_spec.rb +10 -0
  218. data/spec/hanuman/registry_spec.rb +123 -0
  219. data/spec/hanuman/stage_spec.rb +81 -0
  220. data/spec/hanuman/tree_spec.rb +119 -0
  221. data/spec/spec.opts +1 -0
  222. data/spec/spec_helper.rb +43 -0
  223. data/spec/support/example_test_helpers.rb +95 -0
  224. data/spec/support/hanuman_test_helpers.rb +92 -0
  225. data/spec/support/integration_helper.rb +38 -0
  226. data/spec/support/model_test_helpers.rb +115 -0
  227. data/spec/support/shared_context_for_graphs.rb +57 -0
  228. data/spec/support/shared_context_for_reducers.rb +37 -0
  229. data/spec/support/shared_examples_for_builders.rb +94 -0
  230. data/spec/support/shared_examples_for_shortcuts.rb +57 -0
  231. data/spec/wu/model/reconcilable_spec.rb +152 -0
  232. data/spec/wukong/dataflow_spec.rb +87 -0
  233. data/spec/wukong/driver_spec.rb +154 -0
  234. data/spec/wukong/local/runner_spec.rb +29 -0
  235. data/spec/wukong/local/stdio_driver_spec.rb +73 -0
  236. data/spec/wukong/local_spec.rb +6 -0
  237. data/spec/wukong/logger_spec.rb +49 -0
  238. data/spec/wukong/model/faker_spec.rb +132 -0
  239. data/spec/wukong/processor_spec.rb +21 -0
  240. data/spec/wukong/runner_spec.rb +132 -0
  241. data/spec/wukong/source_spec.rb +6 -0
  242. data/spec/wukong/widget/extract_spec.rb +101 -0
  243. data/spec/wukong/widget/filters_spec.rb +79 -0
  244. data/spec/wukong/widget/logger_spec.rb +23 -0
  245. data/spec/wukong/widget/operators_spec.rb +25 -0
  246. data/spec/wukong/widget/reducers/bin_spec.rb +92 -0
  247. data/spec/wukong/widget/reducers/count_spec.rb +11 -0
  248. data/spec/wukong/widget/reducers/group_spec.rb +21 -0
  249. data/spec/wukong/widget/reducers/join_xml_spec.rb +25 -0
  250. data/spec/wukong/widget/reducers/moments_spec.rb +36 -0
  251. data/spec/wukong/widget/reducers/sort_spec.rb +26 -0
  252. data/spec/wukong/widget/reducers/uniq_spec.rb +14 -0
  253. data/spec/wukong/widget/serializers_spec.rb +114 -0
  254. data/spec/wukong/widget/sink_spec.rb +19 -0
  255. data/spec/wukong/widget/source_spec.rb +65 -0
  256. data/spec/wukong/wu-local_spec.rb +109 -0
  257. data/spec/wukong/wu-source_spec.rb +32 -0
  258. data/spec/wukong/wu_spec.rb +14 -0
  259. data/spec/wukong/wukong_spec.rb +10 -0
  260. data/wukong.gemspec +35 -0
  261. metadata +465 -0
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path('../../lib', File.dirname(__FILE__))
3
+ require 'wukong/script'
4
+ require_relative './logline'
5
+
6
+ class BreadcrumbsMapper < Wukong::Streamer::ModelStreamer
7
+ self.model_klass = Logline
8
+ def process visit, *args
9
+ # return unless Settings.page_types.include?(visit.page_type)
10
+ yield [visit.ip, visit.visit_time.to_i, visit.path]
11
+ end
12
+ end
13
+
14
+ class BreadcrumbEdgesReducer < Wukong::Streamer::Reducer
15
+ def get_key ip, itime, path
16
+ [ip]
17
+ end
18
+ def start!(*args)
19
+ @paths = Set.new
20
+ super
21
+ end
22
+ def accumulate ip, itime, path
23
+ @paths << path
24
+ end
25
+
26
+ # for each pair of paths, emit the edge in both directions
27
+ def finalize
28
+ @paths = @paths.to_a
29
+ while @paths.present?
30
+ from = @paths.shift
31
+ @paths.each do |into|
32
+ yield [key, from, into]
33
+ yield [key, into, from]
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+
40
+ Wukong.run( BreadcrumbsMapper, BreadcrumbEdgesReducer, :sort_fields => 2 )
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env ruby
2
+ require 'wukong'
3
+ require 'gorillib/type/ip_address'
4
+ require 'gorillib/pathname'
5
+ require 'gorillib/model/serialization'
6
+ require 'gorillib/model/positional_fields'
7
+
8
+ Pathname.register_paths(
9
+ maxmind: '../../../data/server_logs/maxmind',
10
+ geolite_ip_blocks: [:maxmind, 'GeoLiteCity_20121002/GeoLiteCity-Blocks.csv'],
11
+ geolite_locations: [:maxmind, 'GeoLiteCity_20121002/GeoLiteCity-Location.csv'],
12
+ )
13
+
14
+ class IpGeo
15
+ include Gorillib::Model
16
+ include Gorillib::Model::PositionalFields
17
+ #
18
+ field :location_id, Integer
19
+ field :longitude, Float # note: longitude, latitude
20
+ field :latitude, Float
21
+ #
22
+ field :country_id, String, blankish: ["", nil, '""']
23
+ field :admin1_id, String, blankish: ["", nil, '""']
24
+ field :city, String, blankish: ["", nil, '""']
25
+ #
26
+ field :postal_code, String, blankish: ["", nil, '""']
27
+ field :metro_code, String, blankish: ["", nil, '""']
28
+ field :area_code, String, blankish: ["", nil, '""']
29
+ end
30
+
31
+ lines = 0
32
+ LOCATIONS = {}
33
+ locations_file = Pathname.of(:geolite_locations).open(encoding: "ISO-8859-1")
34
+ locations_file.readline; locations_file.readline
35
+ locations_file.
36
+ # readlines[0..1000].
37
+ each do |line|
38
+ location_id, country_id, admin1_id, city, postal_code, latitude, longitude, metro_code, area_code = line.chomp.gsub(/"/, '').split(',', 9)
39
+ LOCATIONS[location_id.to_i] = [ location_id.to_i, longitude.to_f, latitude.to_f, country_id, admin1_id, city, postal_code, metro_code, area_code ]
40
+ end
41
+
42
+ module IpCensus
43
+ class IpBlocksMapper < Wukong::Streamer::RecordStreamer
44
+
45
+ def initialize(*)
46
+ super
47
+ @last = IpNumeric.from_dotted('0.116.0.0')
48
+ end
49
+
50
+ def recordize line
51
+ beg_ip, end_ip, location_id = line.gsub(/\"/, '').split(",", 3)
52
+ [IpNumeric.new(Integer(beg_ip)), IpNumeric.new(Integer(end_ip)), Integer(location_id)]
53
+ end
54
+
55
+ # Use the regex to break line into fields
56
+ # Emit each record as flat line
57
+ def process(beg_ip, end_ip, location_id, &block)
58
+ emit_range(IpRange.new(@last, beg_ip), 0, &block) if @last != beg_ip.to_int
59
+ emit_range IpRange.new(beg_ip, end_ip), location_id, &block
60
+ @last = end_ip.to_i + 1
61
+ end
62
+
63
+ def emit_range(rng, location_id)
64
+ location = LOCATIONS[location_id]
65
+ warn "No location #{location_id.inspect}" unless location || (location_id == 0)
66
+ rng.bitness_blocks(16).each do |blk_min, blk_max|
67
+ # raise [blk_min, blk_max, blk_min.to_hex[0..-5], blk_max.to_hex[0..-5]].inspect if blk_min.to_hex[0..-3] != blk_max.to_hex[0..-3]
68
+ yield [
69
+ blk_min.to_hex[0..3],
70
+ blk_min.to_hex[4..-1],
71
+ blk_max.to_hex[4..-1],
72
+ blk_min.to_i,
73
+ blk_max.to_i,
74
+ location,
75
+ ].flatten
76
+ end
77
+ end
78
+
79
+ end
80
+ end
81
+
82
+ Wukong::Script.new(IpCensus::IpBlocksMapper, nil).run
@@ -0,0 +1,102 @@
1
+ # # Parse logs to TSV
2
+ #
3
+ # bzcat data/star_wars_kid.log.bz2 | head -n 100200 | tail -n 100 > data/swk-100.log
4
+ # cat data/swk-100.tsv
5
+ # cat data/swk-100.log | ./apache_log_parser.rb --map | wu-lign | cutc 150
6
+ # cat data/swk-100.log | ./apache_log_parser.rb --map > data/swk-100.tsv
7
+ # ./histograms.rb --run data/star_wars_kid.log data/star_wars_kid.tsv
8
+
9
+ # # Histograms
10
+ #
11
+ # cat data/swk-100.tsv | ./histograms.rb --map | wu-lign
12
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce
13
+ # ./histograms.rb --run data/star_wars_kid.tsv data/star_wars_kid-pages_by_hour.tsv
14
+
15
+ # # Sessionize
16
+ #
17
+ # cat data/swk-100.tsv | ./histograms.rb --map | wu-lign
18
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce
19
+ # ./histograms.rb --run data/star_wars_kid.tsv data/star_wars_kid-pages_by_hour.tsv
20
+
21
+ class Logline
22
+ include Gorillib::Model
23
+ include Gorillib::Model::PositionalFields
24
+
25
+ field :ip_address, String
26
+ field :rfc_1413, String
27
+ field :userid, String
28
+ #
29
+ field :requested_at, Time
30
+ field :http_method, String
31
+ field :path, String
32
+ field :protocol, String
33
+ field :response_code, Integer
34
+ field :bytesize, Integer, blankish: ['', nil, '-']
35
+ field :referer, String
36
+ field :user_agent, String
37
+
38
+ def day_hr
39
+ [visit_time.year, visit_time.month, visit_time.day, visit_time.hour].join
40
+ end
41
+
42
+ def receive_requested_at(val)
43
+ if %r{(\d+)/(\w+)/(\d+):(\d+):(\d+):(\d+)\s([\+\-]\d\d)(\d\d)} === val
44
+ day, mo, yr, hour, min, sec, tz1, tz2 = [$1, $2, $3, $4, $5, $6, $7, $8]
45
+ val = Time.new(yr.to_i, MONTHS[mo], day.to_i,
46
+ hour.to_i, min.to_i, sec.to_i, "#{tz1}:#{tz2}")
47
+ end
48
+ super(val)
49
+ end
50
+
51
+ # Use the regex to break line into fields
52
+ # Emit each record as flat line
53
+ def self.parse(line)
54
+ mm = LOG_RE.match(line.chomp) or return BadRecord.new('no match', line)
55
+ new(* mm.captures)
56
+ end
57
+
58
+ def page_type
59
+ file_ext = path[FILE_EXT_RE]
60
+ case file_ext
61
+ when nil then 'page'
62
+ when '.wmv' then 'video'
63
+ when '.html','.shtml' then 'page'
64
+ when '.css', '.js' then 'asset'
65
+ when '.png', '.gif', '.ico' then 'image'
66
+ when '.wmv' then 'image'
67
+ when '.pl','.asp','.jsp','.cgi' then 'page'
68
+ else 'other'
69
+ end
70
+ end
71
+
72
+ # Matches a file extension
73
+ FILE_EXT_RE = %r{\.[^/]+\z}
74
+
75
+ # Map of abbreviated months to date number.
76
+ MONTHS = { 'Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12, }
77
+
78
+ #
79
+ # Regular expression to parse an apache log line.
80
+ #
81
+ # 83.240.154.3 - - [07/Jun/2008:20:37:11 +0000] "GET /faq HTTP/1.1" 200 569 "http://infochimps.org/search?query=your_mom" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
82
+ #
83
+ LOG_RE = Regexp.compile(%r{\A
84
+ (\S+) # ip_address 83.240.154.3
85
+ \s(\S+) # rfc_1413 -
86
+ \s(\S+) # userid -
87
+
88
+ \s\[(\d+/\w+/\d+ # date part [07/Jun/2008
89
+ :\d+:\d+:\d+ # time part :20:37:11
90
+ \s[\+\-]\S*)\] # timezone +0000]
91
+
92
+ \s\"(?:(\S+) # http_method "GET
93
+ \s(\S+) # path /faq
94
+ \s(HTTP/[\d\.]+)|-)\" # protocol HTTP/1.1"
95
+
96
+ \s(\d+) # response_code 200
97
+ \s(\d+|-) # bytesize 569
98
+ \s\"([^\"]*)\" # referer "http://infochimps.org/search?query=CAC"
99
+ \s\"([^\"]*)\" # user_agent "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.16) Gecko/2009120208 Firefox/3.0.16"
100
+ \z}x)
101
+
102
+ end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift(File.expand_path("../lib", File.realdirpath(File.dirname(__FILE__))))
3
+ require 'wukong'
4
+
5
+ Settings.use(:commandline)
6
+ Settings.define :profiler, :default => nil
7
+ Settings.resolve!
8
+
9
+ require File.expand_path('../examples_helper', File.dirname(__FILE__))
10
+ require Pathname.path_to(:examples, 'dataflow/apache_log_line')
11
+
12
+ Wukong.dataflow(:parse_apache_logs) do
13
+
14
+ doc 'Parses an apache log line into a structured model, emits it as JSON'
15
+
16
+ source = ($0 == __FILE__) ? stdin : file_source(Pathname.path_to(:data, 'log/sample_apache_log.log'))
17
+ set_input :default, source
18
+ set_output :dump, stdout
19
+
20
+ input >
21
+ map{|line| ApacheLogLine.make(line) or bad_record(line) } >
22
+ to_tsv >
23
+ output
24
+ end
25
+
26
+ # if ($0 == __FILE__)
27
+ # flow_name = :parse_apache_logs
28
+ # if Settings.profiler
29
+ # require 'perftools'
30
+ # Pathname(Settings.profiler).dirname.mkpath
31
+ # PerfTools::CpuProfiler.start(Settings.profiler) do
32
+ # Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
33
+ # end
34
+ # else
35
+ # Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
36
+ # end
37
+ #
38
+ # # require 'jruby/profiler'
39
+ # # profile_data = JRuby::Profiler.profile do
40
+ # # Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
41
+ # # end
42
+ # # profile_printer = JRuby::Profiler::GraphProfilePrinter.new(profile_data)
43
+ # # profile_printer.printProfile($stderr)
44
+ #
45
+ # # Wukong::LocalRunner.run(Wukong.dataflow(flow_name), :default)
46
+ # end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../../lib', File.dirname(__FILE__))
2
+ require 'configliere'
3
+ require 'wukong/script'
4
+ require_relative '../models/logline'
@@ -0,0 +1,48 @@
1
+
2
+
3
+ LOAD common_pages FROM 'data/common_pages' AS (ip:chararray, from_path:chararray, into_path:chararray);
4
+
5
+ --
6
+ -- Build adjacency list <A pr B,C,D> from edges (<A B>, <A C>, <A D>)
7
+ --
8
+
9
+ adj_list_j = GROUP common_pages BY from_path;
10
+ adj_list = FOREACH adj_list_j GENERATE
11
+ group AS from_path,
12
+ 1.0F AS pagerank:float,
13
+ common_pages.(dest) AS into_paths
14
+ ;
15
+ STORE adj_list INTO 'data/pagerank/pr_iter_00';
16
+
17
+
18
+ --
19
+ -- Iterate pagerank <A pr_00 B,C,D> to become <A pr_01 B,C,D>
20
+ --
21
+
22
+ -- find partial share: A.rank / A.into_paths.length
23
+ -- dispatch <into_path partial_share> to each page
24
+ sent_shares = FOREACH adj_list GENERATE
25
+ FLATTEN(into_paths) AS path,
26
+ (float)(pagerank / (float)SIZE(into_paths)) AS share:float;
27
+
28
+ -- dispatch <from_path into_paths> to yourself, so you have the links still around
29
+ sent_edges = FOREACH adj_list GENERATE
30
+ from_path AS path, into_paths;
31
+
32
+ -- assemble all the received shared, and the self-sent edge list;
33
+ rcvd_shares = COGROUP sent_edges BY path INNER, sent_shares BY path PARALLEL $PARALLEL;
34
+
35
+ -- calculate the new rank, and emit a record that looked just like the input.
36
+ next_iter = FOREACH rcvd_shares {
37
+ raw_rank = (float)SUM(sent_shares.share);
38
+ -- treat the case that a node has no in links
39
+ damped_rank = ((raw_rank IS NOT NULL AND raw_rank > 1.0e-12f) ? raw_rank*0.85f + 0.15f : 0.0f);
40
+ GENERATE
41
+ group AS from_path,
42
+ damped_rank AS rank,
43
+ FLATTEN(sent_edges.into_paths)
44
+ ; };
45
+
46
+ STORE next_iter INTO 'data/pagerank/pr_iter_01';
47
+
48
+
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ class ApacheLogParser < Wukong::Streamer::LineStreamer
5
+ # create a Logline object from each record and serialize it flat to disk
6
+ def process line
7
+ yield Logline.parse(line)
8
+ end
9
+ end
10
+
11
+ Wukong.run( ApacheLogParser )
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ # cat data/swk-100.tsv | ./histograms.rb --map | sort > data/swk-hist-map.tsv
5
+ # cat data/swk-hist-map.tsv | ./histograms.rb --reduce > data/swk-hist.tsv
6
+
7
+ class HistogramsMapper < Wukong::Streamer::ModelStreamer
8
+ self.model_klass = Logline
9
+ def process visit
10
+ yield [visit.path, visit.day_hr]
11
+ end
12
+ end
13
+
14
+ class HistogramsReducer < Wukong::Streamer::Reducer
15
+ def get_key(path, day_hr)
16
+ [path, day_hr]
17
+ end
18
+ def start!(*args)
19
+ @count = 0
20
+ super
21
+ end
22
+ def accumulate(path, day_hr)
23
+ @count += 1
24
+ end
25
+ def finalize
26
+ yield [key, @count]
27
+ end
28
+ end
29
+
30
+ # Wukong.run( HistogramsMapper )
31
+ Wukong.run( HistogramsMapper, HistogramsReducer, :sort_fields => 3 )
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative './common'
3
+
4
+ class HistogramsMapper < Wukong::Streamer::ModelStreamer
5
+ self.model_klass = Logline
6
+ def process visit
7
+ yield [visit.path, visit.day_hr]
8
+ end
9
+ end
10
+
11
+ # Wukong.run( HistogramsMapper )
12
+ Wukong.run( HistogramsMapper )
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env ruby
2
+ require 'configliere'
3
+ Settings.define :page_types, type: Array, default: ['page', 'video'], description: "Acceptable page types"
4
+ require_relative './common'
5
+
6
+ #
7
+ # Group all visitors, and then troll through all the pages they've visited
8
+ # breaking each into distinct visits (where more than an [hour|day|whatever]
9
+ # separate subsequent pageviews
10
+ #
11
+
12
+ #
13
+ # Mapper parses log files and created a visitor_id from the visitor's user_id,
14
+ # cookie or ip. It emits
15
+ #
16
+ # <visitor_id> <datetime> <url_path>
17
+ #
18
+ # where the partition key is visitor_id, and we sort by visitor_id and datetime.
19
+ #
20
+ class BreadcrumbsMapper < Wukong::Streamer::ModelStreamer
21
+ self.model_klass = Logline
22
+ def process visit, *args
23
+ # return unless Settings.page_types.include?(visit.page_type)
24
+ yield [visit.ip, visit.day_hr, visit.requested_at.to_i, visit.path]
25
+ end
26
+ end
27
+
28
+ #
29
+ # Reducer:
30
+ #
31
+ # The reducer is given all page requests for the given visitor id, sorted by
32
+ # timestamp.
33
+ #
34
+ # It group by visits (pageviews separated by more than DISTINCT_VISIT_TIMEGAP)
35
+ # and emits
36
+ #
37
+ # trail <visitor_id> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
38
+ #
39
+ # where the last is a comma-separated string of URL encoded paths (any internal comma is converted to %2C).
40
+ #
41
+ # You can instead emit
42
+ #
43
+ # page_trails <page1> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
44
+ # page_trails <page2> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
45
+ # ....
46
+ # page_trails <pagen> <n_pages_in_visit> <duration> <timestamp> < page1,page2,... >
47
+ #
48
+ # to discover all trails passing through a given page.
49
+ class BreadcrumbsReducer < Wukong::Streamer::Reducer
50
+ def get_key ip, day_hr, itime, path, *args
51
+ [ip]
52
+ end
53
+ def start!(*args)
54
+ @path_times = []
55
+ super
56
+ end
57
+ def accumulate ip, day_hr, itime, path, *args
58
+ @path_times << "(#{itime},#{path})"
59
+ end
60
+ def finalize
61
+ path_times_str = ("{" << @path_times.join(",") << "}")
62
+ yield [key, path_times_str]
63
+ end
64
+ end
65
+
66
+
67
+ Wukong.run( BreadcrumbsMapper, BreadcrumbsReducer, :sort_fields => 2 )