5.Spark的wordCount(java/scala)

在代碼實現(xiàn)之前啸箫,先查看一下數(shù)據(jù)源是怎樣的

Preface
“The Forsyte Saga” was the title originally destined for that part of it which is called “The Man of Property”; and to adopt it for the collected chronicles of the Forsyte family has indulged the Forsytean tenacity that is in all of us. The word Saga might be objected to on the ground that it connotes the heroic and that there is little heroism in these pages. But it is used with a suitable irony; and, after all, this long tale, though it may deal with folk in frock coats, furbelows, and a gilt-edged period, is not devoid of the essential heat of conflict. Discounting for the gigantic stature and blood-thirstiness of old days, as they have come down to us in fairy-tale and legend, the folk of the old Sagas were Forsytes, assuredly, in their possessive instincts, and as little proof against the inroads of beauty and passion as Swithin, Soames, or even Young Jolyon. And if heroic figures, in days that never were, seem to startle out from their surroundings in fashion unbecoming to a Forsyte of the Victorian era, we may be sure that tribal instinct was even then the prime force, and that “family” and the sense of home and property counted as they do to this day, for all the recent efforts to “talk them out.”
So many people have written and claimed that their families were the originals of the Forsytes that one has been almost encouraged to believe in the typicality of an imagined species. Manners change and modes evolve, and “Timothy’s on the Bayswater Road” becomes a nest of the unbelievable in all except essentials; we shall not look upon its like again, nor perhaps on such a one as James or Old Jolyon. And yet the figures of Insurance Societies and the utterances of Judges reassure us daily that our earthly paradise is still a rich preserve, where the wild raiders, Beauty and Passion, come stealing in, filching security from beneath our noses. As surely as a dog will bark at a brass band, so will the essential Soames in human nature ever rise up uneasily against the dissolution which hovers round the folds of ownership.
“Let the dead Past bury its dead” would be a better saying if the Past ever died. The persistence of the Past is one of those tragi-comic blessings which each new age denies, coming cocksure on to the stage to mouth its claim to a perfect novelty.
But no Age is so new as that! Human Nature, under its changing pretensions and clothes, is and ever will be very much of a Forsyte, and might, after all, be a much worse animal.
Looking back on the Victorian era, whose ripeness, decline, and ‘fall-of’ is in some sort pictured in “The Forsyte Saga,” we see now that we have but jumped out of a frying-pan into a fire. It would be difficult to substantiate a claim that the case of England was better in 1913 than it was in 1886, when the Forsytes assembled at Old Jolyon’s to celebrate the engagement of June to Philip Bosinney. And in 1920, when again the clan gathered to bless the marriage of Fleur with Michael Mont, the state of England is as surely too molten and bankrupt as in the eighties it was too congealed and low-percented. If these chronicles had been a really scientific study of transition one would have dwelt probably on such factors as the invention of bicycle, motor-car, and flying-machine; the arrival of a cheap Press; the decline of country life and increase of the towns; the birth of the Cinema. Men are, in fact, quite unable to control their own inventions; they at best develop adaptability to the new conditions those inventions create.
But this long tale is no scientific study of a period; it is rather an intimate incarnation of the disturbance that Beauty effects in the lives of men.
The figure of Irene, never, as the reader may possibly have observed, present, except through the senses of other characters, is a concretion of disturbing Beauty impinging on a possessive world.
One has noticed that readers, as they wade on through the salt waters of the Saga, are inclined more and more to pity Soames, and to think that in doing so they are in revolt against the mood of his creator. Far from it! He, too, pities Soames, the tragedy of whose life is the very simple, uncontrollable tragedy of being unlovable, without quite a thick enough skin to be thoroughly unconscious of the fact. Not even Fleur loves Soames as he feels he ought to be loved. But in pitying Soames, readers incline, perhaps, to animus against Irene: After all, they think, he wasn’t a bad fellow, it wasn’t his fault; she ought to have forgiven him, and so on!
And, taking sides, they lose perception of the simple truth, which underlies the whole story, that where sex attraction is utterly and definitely lacking in one partner to a union, no amount of pity, or reason, or duty, or what not, can overcome a repulsion implicit in Nature. Whether it ought to, or no, is beside the point; because in fact it never does. And where Irene seems hard and cruel, as in the Bois de Boulogne, or the Goupenor Gallery, she is but wisely realistic — knowing that the least concession is the inch which precedes the impossible, the repulsive ell.
A criticism one might pass on the last phase of the Saga is the complaint that Irene and Jolyon those rebels against property — claim spiritual property in their son Jon. But it would be hypercriticism, as the tale is told. No father and mother could have let the boy marry Fleur without knowledge of the facts; and the facts determine Jon, not the persuasion of his parents. Moreover, Jolyon’s persuasion is not on his own account, but on Irene’s, and Irene’s persuasion becomes a reiterated: “Don’t think of me, think of yourself!” That Jon, knowing the facts, can realise his mother’s feelings, will hardly with justice be held proof that she is, after all, a Forsyte.
But though the impingement of Beauty and the claims of Freedom on a possessive world are the main prepossessions of the Forsyte Saga, it cannot be absolved from the charge of embalming the upper-middle class. As the old Egyptians placed around their mummies the necessaries of a future existence, so I have endeavoured to lay beside the, figures of Aunts Ann and Juley and Hester, of Timothy and Swithin, of Old Jolyon and James, and of their sons, that which shall guarantee them a little life here-after, a little balm in the hurried Gilead of a dissolving “Progress.”
If the upper-middle class, with other classes, is destined to “move on” into amorphism, here, pickled in these pages, it lies under glass for strollers in the wide and ill-arranged museum of Letters. Here it rests, preserved in its own juice: The Sense of Property.
1922.

截取了文本的第一段耸彪,已經(jīng)對文本進行預(yù)處理,以空格將單詞分割忘苛。

Java實現(xiàn)

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;

public class WordCountByJava {

    public static void main(String[] args) {
        //配置項
        SparkConf conf = new SparkConf().setAppName("WordCount").setMaster("local");
        //入口
        JavaSparkContext sc = new JavaSparkContext(conf);
        //文件路徑
        String input_path = "C:\\Users\\Desktop\\text\\a.txt";
        //讀取文件
        JavaRDD<String> rdd = sc.textFile(input_path);
        //對單詞按照空格分詞
        JavaRDD<String> splitRDD = rdd.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterator<String> call(String s) throws Exception {
                return Arrays.asList(s.split(" ")).iterator();
            }
        });
        //對每個單詞組成tuple蝉娜,標(biāo)記出現(xiàn)次數(shù)為1
        JavaPairRDD<String, Integer> pairRDD = splitRDD.mapToPair(new PairFunction<String, String, Integer>() {
            @Override
            public Tuple2<String, Integer> call(String s) throws Exception {
                return new Tuple2<>(s,1);
            }
        });
        //根據(jù)單詞進行次數(shù)累加
        JavaPairRDD<String, Integer> reduceByKey = pairRDD.reduceByKey(new Function2<Integer, Integer, Integer>() {
            @Override
            public Integer call(Integer count1, Integer count2) throws Exception {
                return count1 + count2;
            }
        });
        //為排序做準備,將(word,count)轉(zhuǎn)換成(count,word)
        JavaPairRDD<Integer, String> javaPairRDD = reduceByKey.mapToPair(new PairFunction<Tuple2<String, Integer>, Integer, String>() {
            @Override
            public Tuple2<Integer, String> call(Tuple2<String, Integer> stringIntegerTuple2) throws Exception {
                return new Tuple2<>(stringIntegerTuple2._2, stringIntegerTuple2._1);
            }
        });
        //根據(jù)出現(xiàn)次數(shù)進行排序扎唾,默認升序
        JavaPairRDD<Integer, String> sortByKey = javaPairRDD.sortByKey();
        //將排好序的數(shù)據(jù)進行打印
        sortByKey.foreach(new VoidFunction<Tuple2<Integer, String>>() {
            @Override
            public void call(Tuple2<Integer, String> stringIntegerTuple2) throws Exception {
                System.out.println("單詞:" + stringIntegerTuple2._2 +",出現(xiàn)的次數(shù)是"+stringIntegerTuple2._1);
            }
        });
        //資源關(guān)閉
        sc.stop();
    }
}

輸出結(jié)果

//截取部分結(jié)果集
單詞:was,出現(xiàn)的次數(shù)是1702
單詞:his,出現(xiàn)的次數(shù)是1912
單詞:he,出現(xiàn)的次數(shù)是2139
單詞:a,出現(xiàn)的次數(shù)是2543
單詞:and,出現(xiàn)的次數(shù)是2573
單詞:to,出現(xiàn)的次數(shù)是2782
單詞:of,出現(xiàn)的次數(shù)是3407
單詞:the,出現(xiàn)的次數(shù)是5144

Scala實現(xiàn)

import org.apache.spark.{SparkConf, SparkContext}

object WordCount {
  def main(args: Array[String]): Unit = {
    //配置基本信息
    val conf = new SparkConf().setAppName("wordCount").setMaster("local")
    //程序入口
    val sc = new SparkContext(conf)
    //文件路徑
    val input_path = "C:\\Users\\Desktop\\text\\a.txt";

    val result = sc.textFile(input_path)//讀取文件
                  .flatMap(x => x.split(" "))//對單詞按照空格分詞
                  .map(x => (x,1))//對單詞進行標(biāo)記次數(shù)
                  .reduceByKey(_ + _)//根據(jù)單詞為key進行次數(shù)累加
                  .sortBy(tuple => tuple._2,ascending = true)//根據(jù)出現(xiàn)次數(shù)進行排序召川,ascending=false為降序,true為升序
                  .map(x => x._1 + "," + x._2)//對數(shù)據(jù)重新封裝返回
    result.foreach(println)//數(shù)據(jù)打印
  }
}

輸出結(jié)果

//截取部分結(jié)果集
that,1273
had,1526
in,1694
was,1702
his,1912
he,2139
a,2543
and,2573
to,2782
of,3407
the,5144
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末胸遇,一起剝皮案震驚了整個濱河市扮宠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌狐榔,老刑警劉巖坛增,帶你破解...
    沈念sama閱讀 211,948評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異薄腻,居然都是意外死亡收捣,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,371評論 3 385
  • 文/潘曉璐 我一進店門庵楷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來罢艾,“玉大人,你說我怎么就攤上這事尽纽「莉牵” “怎么了?”我有些...
    開封第一講書人閱讀 157,490評論 0 348
  • 文/不壞的土叔 我叫張陵弄贿,是天一觀的道長春锋。 經(jīng)常有香客問我,道長差凹,這世上最難降的妖魔是什么期奔? 我笑而不...
    開封第一講書人閱讀 56,521評論 1 284
  • 正文 為了忘掉前任侧馅,我火速辦了婚禮,結(jié)果婚禮上呐萌,老公的妹妹穿的比我還像新娘馁痴。我一直安慰自己,他們只是感情好肺孤,可當(dāng)我...
    茶點故事閱讀 65,627評論 6 386
  • 文/花漫 我一把揭開白布罗晕。 她就那樣靜靜地躺著,像睡著了一般赠堵。 火紅的嫁衣襯著肌膚如雪攀例。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,842評論 1 290
  • 那天顾腊,我揣著相機與錄音粤铭,去河邊找鬼。 笑死杂靶,一個胖子當(dāng)著我的面吹牛梆惯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播吗垮,決...
    沈念sama閱讀 38,997評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼垛吗,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了烁登?” 一聲冷哼從身側(cè)響起怯屉,我...
    開封第一講書人閱讀 37,741評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎饵沧,沒想到半個月后锨络,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,203評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡狼牺,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,534評論 2 327
  • 正文 我和宋清朗相戀三年羡儿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片是钥。...
    茶點故事閱讀 38,673評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡智听,死狀恐怖哥艇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情崩溪,我是刑警寧澤忍抽,帶...
    沈念sama閱讀 34,339評論 4 330
  • 正文 年R本政府宣布因俐,位于F島的核電站抬驴,受9級特大地震影響秒啦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,955評論 3 313
  • 文/蒙蒙 一领铐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宋舷,春花似錦绪撵、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,770評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绎狭,卻和暖如春细溅,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背儡嘶。 一陣腳步聲響...
    開封第一講書人閱讀 32,000評論 1 266
  • 我被黑心中介騙來泰國打工喇聊, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蹦狂。 一個月前我還...
    沈念sama閱讀 46,394評論 2 360
  • 正文 我出身青樓誓篱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凯楔。 傳聞我的和親對象是個殘疾皇子窜骄,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,562評論 2 349

推薦閱讀更多精彩內(nèi)容