27 lines
1.1 KiB
Python
Executable File
27 lines
1.1 KiB
Python
Executable File
"""Pembanding bergaya MapReduce (Map -> Shuffle -> Reduce) dengan RDD Spark untuk frekuensi kata.
|
|
Hasilnya dipakai sebagai pembanding konsep terhadap versi DataFrame/SQL di analysis_job (Bab 3.4 proposal)."""
|
|
import json
|
|
|
|
import config
|
|
from spark_jobs.session import Timer, get_spark, log_metric
|
|
|
|
|
|
def main():
|
|
spark = get_spark("karhutla-wordcount-mr")
|
|
with Timer() as tm:
|
|
rdd = spark.read.parquet(config.uri("clean")).select("tokens").rdd
|
|
counts = (rdd.flatMap(lambda row: row["tokens"] or []) # Map: emit kata
|
|
.map(lambda w: (w, 1)) # Map: (kata, 1)
|
|
.reduceByKey(lambda a, b: a + b)) # Shuffle + Reduce
|
|
top = counts.takeOrdered(50, key=lambda kv: -kv[1])
|
|
config.REPORT_DIR.mkdir(parents=True, exist_ok=True)
|
|
(config.REPORT_DIR / "wordcount_mapreduce_top50.json").write_text(
|
|
json.dumps(top, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
log_metric("wordcount_mapreduce", seconds=tm.seconds)
|
|
print(top[:15])
|
|
spark.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|