{"id":6708,"date":"2024-07-30T22:01:01","date_gmt":"2024-07-30T14:01:01","guid":{"rendered":""},"modified":"2024-07-30T22:01:01","modified_gmt":"2024-07-30T14:01:01","slug":"spark\u9700\u8981scala\u4ec0\u4e48\u6c34\u5e73_\u8003\u524d\u4e00\u5468\u5982\u4f55\u51b2\u523a","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6708.html","title":{"rendered":"spark\u9700\u8981scala\u4ec0\u4e48\u6c34\u5e73_\u8003\u524d\u4e00\u5468\u5982\u4f55\u51b2\u523a"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u4e00\u3001Spark\u7b80\u4ecb<\/h2>\n

1\u3001\u4ec0\u4e48\u662fSpark\uff1f\uff08\u95ee\u7b54\u5fc5\u8003\uff09<\/h3>\n

Spark<\/strong>\u662f\u4e00\u4e2a\u5feb\u901f\u3001\u5206\u5e03\u5f0f\u3001\u53ef\u6269\u5c55\uff08\u968f\u65f6\u53ef\u4ee5\u8fdb\u884c\u8282\u70b9\u7684\u6269\u5145\uff09\u3001\u5bb9\u9519\uff08\u8282\u70b9\u5b95\u673a\u4e86\u3002\u90a3\u4e48\u5b83\u53ef\u4ee5\u91cd\u65b0\u6784\u5efa\u6062\u590d\u8fd9\u4e2a\u6570\u636e\uff09\u7684\u96c6\u7fa4\u8ba1\u7b97\u6846\u67b6\u3002\u4f4e\u5ef6\u8fdf\u7684\u590d\u6742\u5206\u6790\uff0c\u56e0\u4e3aSpark\u7684\u4f4e\u5ef6\u8fdf\uff0c\u5ef6\u8fdf\u4f4e\u662f\u56e0\u4e3aSpark\u662f\u5728\u5185\u5b58\u91cc\u9762\u8ba1\u7b97\u7684\u3002\uff08Spark\u5df2\u7ecf\u6210\u4e3aApache\u8f6f\u4ef6\u57fa\u91d1\u4f1a\u65d7\u4e0b\u7684\u9876\u7ea7\u5f00\u6e90\u9879\u76ee\uff09<\/p>\n

2\u3001\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528 Spark \u66ff\u6362 MapReduce\uff1f\uff08\u9009\u62e9\u53ef\u80fd\u4f1a\u8003\uff09<\/h3>\n

MapReudce\u4e0d\u9002\u5408\u8fed\u4ee3\u548c\u4ea4\u4e92\u5f0f\u4efb\u52a1\uff0cSpark\u4e3b\u8981\u4e3a\u4ea4\u4e92\u5f0f\u67e5\u8be2\u548c\u8fed\u4ee3\u7b97\u6cd5\u8bbe\u8ba1\uff0c\u652f\u6301\u5185\u5b58\u5b58\u50a8\u548c\u9ad8\u6548\u7684\u5bb9\u9519\u6062\u590d\u3002Spark\u62e5\u6709MapReduce\u5177\u6709\u7684\u4f18\u70b9\uff0c\u4f46\u4e0d\u540c\u4e8eMapReduce\uff0cSpark\u4e2d\u95f4\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u51cf\u5c11\u8bfb\u5199HDFS\u7684\u6b21\u6570\u3002<\/p>\n

3\u3001Spark\u7279\u70b9\uff1a\uff08\u80cc\u719f\uff0c\u95ee\u7b54\u5fc5\u8003\uff09<\/h3>\n
    \n
  • \u5feb\u901f<\/strong>\uff1a\u56e0\u4e3a Spark \u662f\u5728\u5185\u5b58\u91cc\u9762\u8ba1\u7b97\u7684<\/li>\n
  • \u6613\u7528\u6027<\/strong>\uff1aSpark \u652f\u6301\u4f7f\u7528Scala\u3001Python\u3001Java\u53caR\u8bed\u8a00\u5feb\u901f\u7f16\u5199\u5e94\u7528\u3002<\/li>\n
  • \u901a\u7528\u6027<\/strong>\uff1aSpark\u53ef\u4ee5\u4e0eSQL\u3001Streaming\u53ca\u590d\u6742\u7684\u5206\u6790\u826f\u597d\u7ed3\u5408\u3002<\/li>\n
  • \u968f\u5904\u8fd0\u884c<\/strong>\uff1a\u7528\u6237\u53ef\u4ee5\u4f7f\u7528Spark\u7684\u72ec\u7acb\u96c6\u7fa4\u6a21\u5f0f\u8fd0\u884cSpark\uff0c\u4e5f\u53ef\u4ee5\u5728EC2\uff08\u4e9a\u9a6c\u900a\u5f39\u6027\u8ba1\u7b97\u4e91\uff09\u3001Hadoop YARN\u6216\u8005Apache Mesos\u4e0a\u8fd0\u884cSpark\u3002\u5e76\u4e14\u53ef\u4ee5\u4eceHDFS\u3001Cassandra\u3001HBase\u3001Tachyon\u548c\u4efb\u4f55\u5206\u5e03\u5f0f\u6587\u4ef6\u7cfb\u7edf\u8bfb\u53d6\u6570\u636e\u3002<\/li>\n<\/ul>\n

    4\u3001Spark\u7684\u751f\u6001\u5708\uff1a\uff08\u9009\u62e9\u53ef\u80fd\u4f1a\u8003\uff09<\/h3>\n

    \"spark\u9700\u8981scala\u4ec0\u4e48\u6c34\u5e73_\u8003\u524d\u4e00\u5468\u5982\u4f55\u51b2\u523a<\/p>\n

      \n
    • Spark Core<\/strong>\uff1aSpark\u6838\u5fc3\uff0c\u63d0\u4f9b\u5e95\u5c42\u6846\u67b6\u53ca\u6838\u5fc3\u652f\u6301\u3002\u5305\u542bSpark\u7684\u57fa\u672c\u529f\u80fd\uff0c\u5305\u62ec\u4efb\u52a1\u8c03\u5ea6\u3001\u5185\u5b58\u7ba1\u7406\u3001\u5bb9\u9519\u673a\u5236\u7b49\u3002Spark Core\u5185\u90e8\u5b9a\u4e49\u4e86RDDS\uff0c\u5e76\u63d0\u4f9b\u4e86\u5f88\u591aAPI\u6765\u521b\u5efa\u548c\u64cd\u4f5cRDD<\/li>\n
    • Tachyon<\/strong>\uff1aTachyon\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u5185\u5b58\u6587\u4ef6\u7cfb\u7edf\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5185\u5b58\u4e2d\u7684HDFS\u3002<\/li>\n
    • BlinkDB<\/strong>\uff1a\u4e00\u4e2a\u7528\u4e8e\u5728\u6d77\u91cf\u6570\u636e\u4e0a\u8fd0\u884c\u4ea4\u4e92\u5f0fSQL\u67e5\u8be2\u7684\u5927\u89c4\u6a21\u5e76\u884c\u67e5\u8be2\u5f15\u64ce\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6743\u8861\u6570\u636e\u7cbe\u5ea6\u6765\u63d0\u5347\u67e5\u8be2\u54cd\u5e94\u65f6\u95f4\uff0c\u5176\u6570\u636e\u7684\u7cbe\u5ea6\u88ab\u63a7\u5236\u5728\u5141\u8bb8\u7684\u8bef\u5dee\u8303\u56f4\u5185\u3002<\/li>\n
    • Spark SQL<\/strong>\uff1a\u53ef\u4ee5\u6267\u884cSQL\u67e5\u8be2\uff0c\u5305\u62ec\u57fa\u672c\u7684SQL\u8bed\u6cd5\u548cHiveQL\u8bed\u6cd5\u3002\u8bfb\u53d6\u7684\u6570\u636e\u6e90\u5305\u62ecHive\u8868\u3001Parquent\u6587\u4ef6\u3001JSON\u6570\u636e\u3001\u5173\u7cfb\u6570\u636e\u5e93\uff08\u5982MySQL\uff09\u7b49\u3002<\/li>\n
    • Spark Streaming<\/strong>\uff1a\u6d41\u5f0f\u8ba1\u7b97\u3002\u6bd4\u5982\uff0c\u4e00\u4e2a\u7f51\u7ad9\u7684\u6d41\u91cf\u662f\u6bcf\u65f6\u6bcf\u523b\u90fd\u5728\u53d1\u751f\u7684\uff0c\u5982\u679c\u9700\u8981\u77e5\u9053\u8fc7\u53bb15\u5206\u949f\u6216\u4e00\u4e2a\u5c0f\u65f6\u7684\u6d41\u91cf\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528Spark Streaming\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/li>\n
    • MLBase<\/strong>\uff1aMLBase\u662fSpark\u751f\u6001\u5708\u7684\u4e00\u90e8\u5206\uff0c\u4e13\u6ce8\u4e8e\u673a\u5668\u5b66\u4e60\uff0c\u8ba9\u673a\u5668\u5b66\u4e60\u7684\u95e8\u69db\u66f4\u4f4e\uff0c\u8ba9\u4e00\u4e9b\u53ef\u80fd\u5e76\u4e0d\u4e86\u89e3\u673a\u5668\u5b66\u4e60\u7684\u7528\u6237\u4e5f\u80fd\u65b9\u4fbf\u5730\u4f7f\u7528MLBase\u3002MLBase\u5206\u4e3a\u56db\u90e8\u5206\uff1aMLlib\u3001MLI\u3001ML Optimizer\u548cMLRuntime\u3002<\/li>\n
    • MLlib<\/strong>\uff1aMLBase\u7684\u4e00\u90e8\u5206\uff0cMLlib\u662fSpark\u7684\u6570\u636e\u6316\u6398\u7b97\u6cd5\u5e93\uff0c\u5b9e\u73b0\u4e86\u4e00\u4e9b\u5e38\u89c1\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5305\u62ec\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u3001\u534f\u540c\u8fc7\u6ee4\u3001\u964d\u7ef4\u4ee5\u53ca\u5e95\u5c42\u4f18\u5316\u3002<\/li>\n
    • GraphX<\/strong>\uff1a\u56fe\u8ba1\u7b97\u7684\u5e94\u7528\u5728\u5f88\u591a\u60c5\u51b5\u4e0b\u5904\u7406\u7684\u6570\u636e\u90fd\u662f\u5f88\u5e9e\u5927\u7684\uff0c\u6bd4\u5982\u5728\u79fb\u52a8\u793e\u4ea4\u4e0a\u9762\u7684\u5173\u7cfb\u7b49\u90fd\u53ef\u4ee5\u7528\u56fe\u76f8\u5173\u7b97\u6cd5\u6765\u8fdb\u884c\u5904\u7406\u548c\u6316\u6398\uff0c\u4f46\u662f\u5982\u679c\u7528\u6237\u8981\u81ea\u884c\u7f16\u5199\u76f8\u5173\u7684\u56fe\u8ba1\u7b97\u7b97\u6cd5\uff0c\u5e76\u4e14\u8981\u5728\u96c6\u7fa4\u4e2d\u5e94\u7528\uff0c\u90a3\u4e48\u96be\u5ea6\u662f\u975e\u5e38\u5927\u7684\u3002\u800c\u4f7f\u7528Spark GraphX\u5c31\u53ef\u4ee5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5b83\u91cc\u9762\u5185\u7f6e\u4e86\u5f88\u591a\u7684\u56fe\u76f8\u5173\u7b97\u6cd5\u3002<\/li>\n
    • SparkR<\/strong>\uff1aSparkR\u662fAMPLab\u53d1\u5e03\u7684\u4e00\u4e2aR\u5f00\u53d1\u5305\uff0c\u4f7f\u5f97R\u6446\u8131\u5355\u673a\u8fd0\u884c\u7684\u547d\u8fd0\uff0c\u53ef\u4ee5\u4f5c\u4e3aSpark\u7684Job\u8fd0\u884c\u5728\u96c6\u7fa4\u4e0a\uff0c\u6781\u5927\u5730\u6269\u5c55\u4e86R\u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ul>\n

      \u4e8c\u3001Scala\u7f16\u7a0b\u57fa\u7840<\/h2>\n

      1\u3001\u4ec0\u4e48\u662fScala?\uff08\u7406\u89e3\u7740\u80cc\uff0c\u95ee\u7b54\u53ef\u80fd\u4f1a\u8003\uff09<\/h3>\n

      Scala\u662f\u4e00\u95e8\u591a\u8303\u5f0f\u7684\u7f16\u7a0b\u8bed\u8a00<\/strong>\uff0c\u8bbe\u8ba1\u521d\u8877\u662f\u8981\u96c6\u6210\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong>\u7684\u5404\u79cd\u7279\u6027\u3002\u56e0\u6b64Scala\u662f\u4e00\u79cd\u7eaf\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\uff0c\u6bcf\u4e2a\u503c\u90fd\u662f\u5bf9\u8c61\u3002\u540c\u65f6Scala\u4e5f\u662f\u4e00\u79cd\u51fd\u6570\u5f0f\u7f16\u7a0b\u8bed\u8a00\uff0c\u5176\u51fd\u6570\u4e5f\u80fd\u5f53\u6210\u503c\u6765\u4f7f\u7528\u3002\u7531\u4e8eScala\u6574\u5408\u4e86\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u548c\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u7279\u6027\uff0cScala\u76f8\u5bf9\u4e8eJava\u3001C#\u3001C++\u7b49\u5176\u4ed6\u8bed\u8a00\u66f4\u52a0\u7b80\u6d01\u3002Scala\u6e90\u4ee3\u7801\u88ab\u7f16\u8bd1\u6210Java\u5b57\u8282\u7801\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u8fd0\u884c\u4e8eJVM\u4e4b\u4e0a\uff0c\u5e76\u53ef\u4ee5\u8c03\u7528\u73b0\u6709\u7684Java\u7c7b\u5e93\u3002Scala\u4e00\u5f00\u59cb\u5c31\u6253\u7b97\u57fa\u4e8eJava\u7684\u751f\u6001\u7cfb\u7edf\u53d1\u5c55\u81ea\u8eab\uff0c\u800c\u8fd9\u4ee4Scala\u53d7\u76ca\u532a\u6d45\u3002<\/p>\n

      2\u3001Scala\u7279\u70b9\uff1a\uff08\u80cc\u719f\uff0c\u95ee\u7b54\u5fc5\u8003\uff09<\/h3>\n
        \n
      • \u9762\u5411\u5bf9\u8c61<\/strong>\uff1a\u628a\u76f8\u5173\u7684\u6570\u636e\u548c\u65b9\u6cd5\u7ec4\u7ec7\u4e3a\u4e00\u4e2a\u6574\u4f53\u6765\u770b\u5f85\uff0c\u4ece\u66f4\u9ad8\u7684\u5c42\u6b21\u6765\u8fdb\u884c\u7cfb\u7edf\u5efa\u6a21\uff0c\u66f4\u8d34\u8fd1\u4e8b\u7269\u7684\u81ea\u7136\u8fd0\u884c\u6a21\u5f0f\u3002<\/li>\n
      • \u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong>\uff1a\u51fd\u6570\u662f\u4e00\u7b49\u516c\u6c11,\u5141\u8bb8\u81ea\u5b9a\u4e49\u63a7\u5236\u8bed\u53e5\u3001\u6539\u9020\u8bed\u8a00\u3002<\/li>\n
      • \u9759\u6001\u7c7b\u578b<\/strong>\uff1a\u7f16\u8bd1\u65f6\u8fdb\u884c\u68c0\u67e5\u3002<\/li>\n
      • scala\u662f\u53ef\u6269\u5c55\u7684<\/strong>\uff1a
        \u9690\u5f0f\u7c7b\uff1a \u5141\u8bb8\u7ed9\u5df2\u6709\u7684\u7c7b\u578b\u6dfb\u52a0\u6269\u5c55\u65b9\u6cd5
        \u5b57\u7b26\u4e32\u63d2\u503c\uff1a \u53ef\u4ee5\u8ba9\u7528\u6237\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u63d2\u503c\u5668\u8fdb\u884c\u6269\u5c55<\/li>\n<\/ul>\n

        3\u3001Scala\u7684\u7f16\u7a0b\u6a21\u5f0f\uff1a\uff08\u7406\u89e3\u5c31\u884c\uff0c\u4e0d\u7528\u80cc\uff09<\/h3>\n
          \n
        • \u4ea4\u4e92\u5f0f\u7f16\u7a0b<\/strong>\uff1aREPL(Read Eval Print Loop:\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668)
          \u8bfb\u53d6\uff1a\u8bfb\u53d6\u7528\u6237\u8f93\u5165\uff0c\u89e3\u6790\u8f93\u5165\u7684\u5185\u5bb9\u3002
          \u6267\u884c\uff1a\u6267\u884c\u8f93\u5165\u7684scala\u4ee3\u7801
          \u6253\u5370\uff1a\u8f93\u51fa\u7ed3\u679c
          \u5faa\u73af \uff1a\u5faa\u73af\u64cd\u4f5c\u4ee5\u4e0a\u6b65\u9aa4\u76f4\u5230\u7528\u6237\u786e\u8ba4\u9000\u51fa\u3002<\/li>\n
        • \u811a\u672c\u7f16\u7a0b<\/strong>\uff1a\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u521b\u5efa\u4e00\u4e2a .scala \u7684\u6587\u4ef6\u6765\u6267\u884c\u4ee3\u7801
          \u4f7f\u7528 scalac \u547d\u4ee4\u7f16\u8bd1
          \u4f7f\u7528scala\u547d\u4ee4\u6267\u884c\u7a0b\u5e8f<\/li>\n<\/ul>\n

          4\u3001Scala\u6570\u636e\u7c7b\u578b<\/h3>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
          \u6570\u636e\u7c7b\u578b<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n
          Byte<\/td>\n8\u4f4d\u6709\u7b26\u53f7\u8865\u7801\u6574\u6570\u3002\u6570\u503c\u533a\u95f4\u4e3a -128 \u5230 127<\/td>\n<\/tr>\n
          Short<\/td>\n16\u4f4d\u6709\u7b26\u53f7\u8865\u7801\u6574\u6570\u3002\u6570\u503c\u533a\u95f4\u4e3a -32768 \u5230 32767<\/td>\n<\/tr>\n
          Int<\/td>\n32\u4f4d\u6709\u7b26\u53f7\u8865\u7801\u6574\u6570\u3002\u6570\u503c\u533a\u95f4\u4e3a - \u5230 \u3002<\/td>\n<\/tr>\n
          Long<\/td>\n64\u4f4d\u6709\u7b26\u53f7\u8865\u7801\u6574\u6570\u3002\u6570\u503c\u533a\u95f4\u4e3a - \u5230 <\/td>\n<\/tr>\n
          Float<\/td>\n32 \u4f4d, IEEE 754 \u6807\u51c6\u7684\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\u6d6e\u70b9\u6570\u540e\u9762\u6709f\u6216\u8005F\u540e\u7f00\u65f6\uff0c\u8868\u793a\u8fd9\u662f\u4e00\u4e2aFloat\u7c7b\u578b<\/td>\n<\/tr>\n
          Double<\/td>\n64 \u4f4d IEEE 754 \u6807\u51c6\u7684\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u3002\u6d6e\u70b9\u6570\u540e\u6ca1\u6709F\u6216f\u7684\uff0c\u662fDouble\u7c7b\u578b<\/td>\n<\/tr>\n
          Char<\/td>\n16\u4f4d\u65e0\u7b26\u53f7Unicode\u5b57\u7b26, \u533a\u95f4\u503c\u4e3a U+0000 \u5230 U+FFFF<\/td>\n<\/tr>\n
          String<\/td>\n\u5b57\u7b26\u5e8f\u5217\uff0c\u5373\u5b57\u7b26\u4e32\uff0c\u5176\u7528\u6cd5\u662f\u7528\u53cc\u5f15\u53f7\u5305\u542b\u4e00\u7cfb\u5217\u5b57\u7b26\u3002<\/td>\n<\/tr>\n
          Boolean<\/td>\n\u53ea\u80fd\u4fdd\u5b58\u4e24\u4e2a\u7279\u6b8a\u7684\u503c\uff0cture\u548cflase\u3002<\/td>\n<\/tr>\n
          Unit<\/td>\n\u5728Java\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b9\u6cd5\u65f6\u7ecf\u5e38\u7528void\u8868\u793a\u8be5\u65b9\u6cd5\u65e0\u8fd4\u56de\u503c\uff0c\u800cScala\u4e2d\u6ca1\u6709void\u5173\u952e\u5b57\uff0cScala\u4e2d\u7528Unit\u8868\u793a\u65e0\u503c\uff0c\u7b49\u540c\u4e8eJava\u4e2d\u7684void\u3002<\/td>\n<\/tr>\n
          Null<\/td>\nnull \u6216\u7a7a\u5f15\u7528<\/td>\n<\/tr>\n
          Nothing<\/td>\nNothing\u7c7b\u578b\u5728Scala\u7684\u7c7b\u5c42\u7ea7\u7684\u6700\u5e95\u7aef\uff1b\u5b83\u662f\u4efb\u4f55\u5176\u4ed6\u7c7b\u578b\u7684\u5b50\u7c7b\u578b\u3002<\/td>\n<\/tr>\n
          Any<\/td>\nAny\u662f\u6240\u6709\u5176\u4ed6\u7c7b\u7684\u8d85\u7c7b<\/td>\n<\/tr>\n
          AnyRef<\/td>\nAnyRef\u7c7b\u662fScala\u91cc\u6240\u6709\u5f15\u7528\u7c7b(reference class)\u7684\u57fa\u7c7b<\/td>\n<\/tr>\n
          Range<\/td>\n\u662f\u4e00\u79cd\u6570\u636e\u8303\u56f4\u6216\u5e8f\u5217\uff0c \u652f\u6301Range\u7684\u7c7b\u578b\u5305\u62ecInt\u3001Long\u3001Float\u3001Double\u3001Char\u3001BigInt\u548cBigDecimal\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

          5\u3001\u53d8\u91cf(var)\u548c\u5e38\u91cf(val)<\/h3>\n

          var\u53d8\u91cf<\/strong>\uff1a\u53ef\u4ee5\u5728\u5b83\u7684\u58f0\u660e\u5468\u671f\u4e2d\u88ab\u591a\u6b21\u8d4b\u503c
          val\u5e38\u91cf<\/strong>\uff1a\u4e00\u65e6\u521d\u59cb\u5316\u4e86\uff0cval\u5c31\u4e0d\u80fd\u518d\u88ab\u8d4b\u503c
          \u6ce8\u610f\uff1aval \u5bf9\u5e76\u53d1\u6216\u5206\u5e03\u5f0f\u7f16\u7a0b\u5f88\u6709\u597d\u5904<\/p>\n

          6\u3001Scala\u7f16\u7a0b\u8bed\u6cd5\uff08\u5fc5\u80cc\uff0c\u5e76\u4e14~\u8981\u4f1a\u7075\u6d3b\u4f7f\u7528\uff09<\/h3>\n

          6.1 \u5224\u65ad<\/h4>\n
          if<\/span> (<\/span>\u8868\u8fbe\u5f0f)<\/span> { \n   <\/span> \/\/\u4ee3\u7801\u5757 <\/span> }<\/span> else<\/span> if<\/span>(<\/span>\u8868\u8fbe\u5f0f)<\/span>{ \n   <\/span> \/\/\u4ee3\u7801\u5757<\/span> }<\/span>else<\/span>{ \n   <\/span> \/\/\u4ee3\u7801\u5757<\/span> }<\/span> <\/code><\/pre>\n

          6.2 \u6570\u7ec4<\/h4>\n

          \uff081\uff09\u5b9a\u957f\u6570\u7ec4<\/strong><\/p>\n

          \u5b9a\u4e49<\/strong>\u5b9a\u957f\u6570\u7ec4\u7684\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n

            \n
          • val arr = new ArrayInt<\/li>\n
          • val arr = Array(1,2,3,4,5)<\/li>\n<\/ul>\n

            \u64cd\u4f5c<\/strong>\u5b9a\u957f\u6570\u7ec4\uff1a<\/p>\n

              \n
            • arr.length<\/strong>\uff1a\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6<\/li>\n
            • arr.head<\/strong>\uff1a\u8fd4\u56de\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20<\/li>\n
            • arr.tail<\/strong>\uff1a\u8fd4\u56de\u9664\u4e86\u7b2c\u4e00\u4e2a\u5143\u7d20<\/li>\n
            • arr.isEmpty<\/strong>\uff1a\u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a<\/li>\n
            • arr.contains(\u6570\u636e)<\/strong>\uff1a\u5224\u65ad\u6570\u7ec4\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20<\/li>\n<\/ul>\n

              \uff082\uff09\u53d8\u957f\u6570\u7ec4<\/strong>\uff08\u5373\u957f\u5ea6\u53ef\u53d8\u7684\u6570\u7ec4\uff09
              \u5b9a\u4e49\u53d8\u957f\u6570\u7ec4\u4e4b\u524d\u9700\u8981\u5148\u5bfc\u5165\u5305<\/strong>\uff1a<\/p>\n

              import<\/span> scala.<\/span>collection.<\/span>mutable.<\/span>ArrayBuffer <\/code><\/pre>\n

              \u5b9a\u4e49<\/strong>\u53d8\u957f\u6570\u7ec4\uff1a<\/p>\n

              val<\/span> bufArr =<\/span> new<\/span> ArrayBuffer[<\/span>Int<\/span>]<\/span>(<\/span>)<\/span> <\/code><\/pre>\n

              \u64cd\u4f5c<\/strong>\u53d8\u957f\u6570\u7ec4\uff1a\u5b9a\u957f\u6570\u7ec4\u7684\u64cd\u4f5c\u5747\u53ef\u4ee5\u4f7f\u7528<\/p>\n

                \n
              • bufArr += 1<\/strong>\uff1a\u5728\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20<\/li>\n
              • bufArr.trimEnd(n)<\/strong>\uff1a\u79fb\u9664\u6700\u540e\u7684n\u4e2a\u5143\u7d20<\/li>\n
              • bufArr.insert(n-1,x)<\/strong>\uff1a\u4e2d\u95f4\u63d2\u5165\u6570\u7ec4\u5143\u7d20(\u7b2cn\u4e2a\u5143\u7d20\u524d\u63d2\u5165x\u5143\u7d20\uff09\u3002<\/li>\n
              • bufArr.insert(n-1,x,y,z)<\/strong>\uff1a\u63d2\u5165\u591a\u4e2a\u5143\u7d20<\/li>\n
              • bufArr.remove(n-1)<\/strong>\uff1a\u5220\u9664\u6570\u7ec4\u7b2cn\u4e2a\u5143\u7d20<\/li>\n
              • bufArr.toArray<\/strong>\uff1a\u53d8\u6210\u5b9a\u957f\u6570\u7ec4<\/li>\n<\/ul>\n

                \uff083\uff09\u6570\u7ec4\u5176\u4ed6\u5e38\u7528\u7684\u65b9\u6cd5<\/strong>\uff1a
                \u9700\u8981\u5bfc\u5165\u5305\uff1aimport Array._<\/code><\/p>\n

                  \n
                • concat(arr1,arr2)\uff1a\u5408\u5e76\u6570\u7ec4<\/li>\n<\/ul>\n
                  var<\/span> arr1 =<\/span> Array(<\/span>1<\/span>,<\/span>2<\/span>)<\/span> var<\/span> arr2 =<\/span> Array(<\/span>3<\/span>,<\/span>4<\/span>)<\/span> var<\/span> arr3 =<\/span> concat(<\/span>arr1,<\/span>arr2)<\/span> \/\/ \u7ed3\u679c(1,2,3,4)<\/span> <\/code><\/pre>\n
                    \n
                  • fill(len, data)\uff1a\u8fd4\u56de\u6570\u7ec4\uff0c\u957f\u5ea6\u4e3a\u7b2c\u4e00\u4e2a\u53c2\u6570\u6307\u5b9a\uff0c\u540c\u65f6\u6bcf\u4e2a\u5143\u7d20\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8fdb\u884c\u586b\u5145\u3002<\/li>\n<\/ul>\n
                    val<\/span> arr:<\/span>Array[<\/span>Int<\/span>]<\/span>=<\/span>fill(<\/span>3<\/span>)<\/span>(<\/span>2<\/span>)<\/span> <\/code><\/pre>\n
                      \n
                    • ofDim(len)\uff1a\u521b\u5efa\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u7ec4\uff0c\u6216\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/li>\n<\/ul>\n
                      val<\/span> arr1:<\/span>Array[<\/span>Int<\/span>]<\/span>=<\/span>ofDim(<\/span>3<\/span>)<\/span> val<\/span> arr2:<\/span>Array[<\/span>Array[<\/span>Int<\/span>]<\/span>]<\/span>=<\/span>ofDim(<\/span>3<\/span>,<\/span>3<\/span>)<\/span> \/\/\u4e8c\u7ef4\u6570\u7ec4<\/span> <\/code><\/pre>\n
                        \n
                      • range(start,end,step)\uff1a\u521b\u5efa\u6307\u5b9a\u533a\u95f4\u5185\u7684\u6570\u7ec4\uff0cstep \u4e3a\u6bcf\u4e2a\u5143\u7d20\u95f4\u7684\u6b65\u957f<\/li>\n<\/ul>\n
                        val<\/span> arr3:<\/span>Array[<\/span>Int<\/span>]<\/span>=<\/span>range(<\/span>1<\/span>,<\/span>10<\/span>,<\/span>2<\/span>)<\/span> val<\/span> arr4:<\/span>Array[<\/span>Int<\/span>]<\/span>=<\/span>range(<\/span>1<\/span>,<\/span>10<\/span>)<\/span> <\/code><\/pre>\n

                        \u8fde\u63a5\u6570\u7ec4<\/strong>\uff1a\u8fde\u63a5\u4e24\u4e2a\u6570\u7ec4\u65e2\u53ef\u4ee5\u4f7f\u7528\u64cd\u4f5c\u7b26<\/strong>++<\/code>\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528concat\u65b9\u6cd5\u3002\u4f46\u662f\u4f7f\u7528concat\u65b9\u6cd5\u9700\u8981\u5148\u4f7f\u7528import Array._<\/code>\u5f15\u5165\u5305\u3002<\/p>\n

                        \u521b\u5efa\u591a\u7ef4\u6570\u7ec4\uff1a<\/strong><\/p>\n

                        var<\/span> arrInt =<\/span> Array(<\/span>Array(<\/span>2<\/span>,<\/span>3<\/span>)<\/span>,<\/span>Array(<\/span>5<\/span>,<\/span>6<\/span>)<\/span>)<\/span> <\/code><\/pre>\n

                        6.3 \u65b9\u6cd5\u548c\u51fd\u6570<\/h4>\n

                        \u65b9\u6cd5\u548c\u51fd\u6570<\/strong>\uff1a\u4e8c\u8005\u5728\u8bed\u4e49\u4e0a\u7684\u533a\u522b\u5f88\u5c0f\u3002Scala \u65b9\u6cd5\u662f\u7c7b\u7684\u4e00\u90e8\u5206\uff0c\u800c\u51fd\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u3002\u6362\u53e5\u8bdd\u6765\u8bf4\u5728\u7c7b\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u5373\u662f\u65b9\u6cd5\u3002
                        \u4f7f\u7528 val \u8bed\u53e5<\/strong>\u53ef\u4ee5\u5b9a\u4e49\u51fd\u6570\uff0cdef \u8bed\u53e5<\/strong>\u5b9a\u4e49\u65b9\u6cd5\u3002
                        \uff081\uff09\u65b9\u6cd5<\/strong>
                        \u65b9\u6cd5\u7684\u5b9a\u4e49\u65b9\u5f0f\uff1a<\/strong><\/p>\n

                         object<\/span> Test { \n   <\/span> \/\/\u65b9\u6cd5<\/span> def<\/span> addInt(<\/span> a:<\/span>Int<\/span>,<\/span> b:<\/span>Int<\/span>)<\/span> :<\/span>Int<\/span> =<\/span> { \n   <\/span> var<\/span> sum:<\/span>Int<\/span> =<\/span>0<\/span> sum =<\/span> a +<\/span> b return<\/span> sum }<\/span> }<\/span> \/\/\u8c03\u7528\u65b9\u6cd5\u5f62\u5f0f1\uff1a\u5e26\u7c7b\u540d\u8c03\u7528<\/span> Test.<\/span>addInt(<\/span>1<\/span>,<\/span>2<\/span>)<\/span> <\/code><\/pre>\n

                        \"spark\u9700\u8981scala\u4ec0\u4e48\u6c34\u5e73_\u8003\u524d\u4e00\u5468\u5982\u4f55\u51b2\u523a<\/p>\n

                        \/\/\u65e2\u4e0d\u5e26\u53c2\u6570\u4e5f\u4e0d\u8fd4\u56de\u7ed3\u679c<\/span> def<\/span> printMe(<\/span>)<\/span> :<\/span> Unit<\/span> =<\/span> { \n   <\/span> println(<\/span>\"Hello, Scala!\"<\/span>)<\/span> }<\/span> \/\/\u8c03\u7528\u65b9\u6cd5\u5f62\u5f0f2\uff1a\u76f4\u63a5\u8c03\u7528<\/span> printMe(<\/span>)<\/span> <\/code><\/pre>\n

                        \u65b9\u6cd5\u7684\u53c2\u6570\uff1a<\/strong><\/p>\n

                          \n
                        • \u53ef\u53d8\u53c2\u6570<\/strong>\uff1aScala\u5141\u8bb8\u65b9\u6cd5\u7684\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u91cd\u590d\u7684\uff0c\u5373\u4e0d\u9700\u8981\u6307\u5b9a\u53c2\u6570\u7684\u4e2a\u6570\uff0c\u53ef\u4ee5\u5411\u65b9\u6cd5\u4f20\u5165\u53ef\u53d8\u957f\u5ea6\u53c2\u6570\u5217\u8868\u3002<\/li>\n<\/ul>\n
                          def<\/span> printStrings(<\/span> args:<\/span>String<\/span>*<\/span>)<\/span> =<\/span> { \n   <\/span> var<\/span> i :<\/span>Int<\/span> =<\/span> 0<\/span>;<\/span> for<\/span> (<\/span>arg <-<\/span> args)<\/span>{ \n   <\/span> println(<\/span>\"arg value[\"<\/span> +<\/span> i +<\/span> \"] = \"<\/span> +<\/span> arg)<\/span>;<\/span> i +=<\/span> 1<\/span>;<\/span> }<\/span> }<\/span> printStrings(<\/span>\"abc\"<\/span>,<\/span> \"cd\"<\/span>)<\/span> <\/code><\/pre>\n
                            \n
                          • \u9ed8\u8ba4\u503c\u53c2\u6570<\/strong>\uff1a\u5728\u5b9a\u4e49\u65b9\u6cd5\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u7ed9\u53c2\u6570\u8d4b\u9ed8\u8ba4\u503c\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u8c03\u7528\u65b9\u6cd5\u65f6\u4e0d\u4f20\u5165\u53c2\u6570\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u4f20\u5165\u53c2\u6570\u5219\u6839\u636e\u5b9e\u9645\u53c2\u6570\u503c\u8fdb\u884c\u8ba1\u7b97\u3002<\/li>\n<\/ul>\n
                            def<\/span> addInt (<\/span> a:<\/span>Int<\/span>=<\/span>5<\/span>,<\/span> b:<\/span>Int<\/span>=<\/span>7<\/span>)<\/span> :<\/span>Int<\/span> =<\/span> { \n   <\/span> var<\/span> sum:<\/span>Int<\/span> =<\/span> 0<\/span> sum =<\/span> a+<\/span>b return<\/span> sum }<\/span> <\/code><\/pre>\n

                            \uff082\uff09\u51fd\u6570<\/strong>
                            \u51fd\u6570<\/strong>\uff1a\u51fd\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\u53ef\u4ee5\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f7f\u7528val\u8fdb\u884c\u5b9a\u4e49\u3002\u51fd\u6570\u53ef\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\u4f20\u5165\u5230\u65b9\u6cd5\u4e2d\uff0c\u800c\u65b9\u6cd5\u4e0d\u884c\u3002
                            \u51fd\u6570\u7684\u5b9a\u4e49<\/strong>\uff1a<\/p>\n

                            val<\/span> f =<\/span> (<\/span>x:<\/span>Int<\/span>,<\/span>y:<\/span>Int<\/span>)<\/span> =><\/span> { \n   <\/span> x +<\/span> y x +<\/span> y +<\/span> 10<\/span> }<\/span> <\/code><\/pre>\n

                            6.4 \u5faa\u73af<\/h4>\n

                            \uff081\uff09while\u5faa\u73af<\/strong><\/p>\n

                            var<\/span> a =<\/span> 10<\/span>;<\/span> while<\/span>(<\/span> a <<\/span> 20<\/span> )<\/span>{ \n   <\/span> println(<\/span> \"Value of a: \"<\/span> +<\/span> a )<\/span>;<\/span> a =<\/span> a +<\/span> 1<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            \u6ce8\u610f<\/strong>\uff1a++i\u548ci++\u5728Scala\u91cc\u4e0d\u8d77\u4f5c\u7528\uff0c\u8981\u5728\u5f97\u5230\u540c\u6837\u6548\u679c\uff0c\u5fc5\u987b\u8981\u4e48\u5199\u6210i=i+1\uff0c\u8981\u4e48\u5199\u6210i+=1<\/p>\n

                            \uff082\uff09do while\u5faa\u73af<\/strong><\/p>\n

                            var<\/span> a =<\/span> 10<\/span>;<\/span> do<\/span>{ \n   <\/span> println(<\/span> \"Value of a: \"<\/span> +<\/span> a )<\/span>;<\/span> a =<\/span> a +<\/span> 1<\/span>;<\/span> }<\/span>while<\/span>(<\/span> a <<\/span> 20<\/span> )<\/span> <\/code><\/pre>\n

                            \uff083\uff09for\u5faa\u73af<\/strong><\/p>\n

                            var<\/span> a =<\/span> 0<\/span>;<\/span> for<\/span>(<\/span> a <-<\/span> 1<\/span> to 10<\/span>)<\/span>{ \n   <\/span> println(<\/span> \"Value of a: \"<\/span> +<\/span> a )<\/span>;<\/span> }<\/span> \/\/----\u7b49\u540c\u4e8e<\/span> for<\/span>(<\/span> a <-<\/span> 1<\/span> until 10<\/span>)<\/span>{ \n   <\/span> println(<\/span> \"Value of a: \"<\/span> +<\/span> a )<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            \u6ce8\u610f<\/strong>\uff1a\u5de6\u7bad\u5934<-<\/code> \u7528\u4e8e\u4e3a\u53d8\u91cf a \u8d4b\u503c\u3002<\/p>\n

                            for\u5faa\u73af\u548c\u6570\u7ec4<\/strong>\uff1a<\/p>\n

                            var<\/span> a =<\/span> 0<\/span>;<\/span> val<\/span> numList =<\/span> Array(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>,<\/span>6<\/span>)<\/span>;<\/span> for<\/span>(<\/span> a <-<\/span> numList )<\/span>{ \n   <\/span> println(<\/span> \"Value of a: \"<\/span> +<\/span> a )<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            for\u5faa\u73af\u548c\u8fc7\u6ee4<\/strong>
                            \u8bed\u6cd5\uff1a<\/strong><\/p>\n

                            for<\/span>(<\/span> var<\/span> x <-<\/span> List if<\/span> condition1;<\/span> if<\/span> condition2.<\/span>.<\/span>.<\/span> )<\/span>{ \n   <\/span> statement(<\/span>s)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            \u793a\u4f8b\uff1a<\/strong><\/p>\n

                            for<\/span>(<\/span> var<\/span> x <-<\/span> List if<\/span> condition1;<\/span> if<\/span> condition2.<\/span>.<\/span>.<\/span> )<\/span>{ \n   <\/span> statement(<\/span>s)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            for\u5faa\u73af\u548cyield<\/strong>
                            \u8bed\u6cd5<\/strong>\uff1a\u5c06 for \u5faa\u73af\u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u4e00\u4e2a\u53d8\u91cf\u5b58\u50a8<\/p>\n

                            for<\/span>(<\/span> var<\/span> x <-<\/span> List if<\/span> condition1;<\/span> if<\/span> condition2.<\/span>.<\/span>.<\/span> )<\/span>{ \n   <\/span> statement(<\/span>s)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            \u793a\u4f8b\uff1a<\/strong><\/p>\n

                            for<\/span>(<\/span> var<\/span> x <-<\/span> List if<\/span> condition1;<\/span> if<\/span> condition2.<\/span>.<\/span>.<\/span> )<\/span>{ \n   <\/span> statement(<\/span>s)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            \uff084\uff09foreach\u65b9\u6cd5<\/strong><\/p>\n

                            for<\/span>(<\/span> var<\/span> x <-<\/span> List if<\/span> condition1;<\/span> if<\/span> condition2.<\/span>.<\/span>.<\/span> )<\/span>{ \n   <\/span> statement(<\/span>s)<\/span>;<\/span> }<\/span> <\/code><\/pre>\n

                            6.5 List\u5217\u8868<\/h4>\n

                            \u5b9a\u4e49\u5217\u8868\uff1a<\/strong><\/p>\n

                            \u65b9\u5f0f1\uff1a<\/strong><\/p>\n

                            val<\/span> list:<\/span>List[<\/span>String<\/span>]<\/span> =<\/span> List(<\/span>\"baidu\"<\/span>,<\/span> \"google\"<\/span>)<\/span> <\/code><\/pre>\n

                            \u65b9\u5f0f2<\/strong>\uff1aNil \u4ee3\u8868\u4e00\u4e2a\u7a7a\uff0c\u6700\u540e\u4e00\u4e2a\u5fc5\u987b\u4e3aNil \uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/p>\n

                            val<\/span> list:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> 1<\/span>:<\/span>:<\/span>2<\/span>:<\/span>:<\/span>3<\/span>:<\/span>:<\/span>Nil <\/code><\/pre>\n

                            \u64cd\u4f5c\u5217\u8868\uff1a<\/strong><\/p>\n

                              \n
                            • ::<\/code>\uff1a\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5217\u8868<\/li>\n
                            • :::<\/code>\uff1a\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5217\u8868<\/li>\n
                            • concat<\/code>\uff1a\u7528\u4e8e\u8fde\u63a5\u4e24\u4e2a\u5217\u8868
                              \u533a\u522b\u8be6\u89c1\u8fd9\u7bc7\u6587\u7ae0<\/li>\n
                            • head\uff1a\u83b7\u53d6\u5217\u8868\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20<\/li>\n
                            • init\uff1a\u8fd4\u56de\u6240\u6709\u5143\u7d20\uff0c\u9664\u4e86\u6700\u540e\u4e00\u4e2a<\/li>\n
                            • drop(n)\uff1a\u4e22\u5f03\u524dn\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u65b0\u5217\u8868<\/li>\n
                            • filter\uff1a\u8f93\u51fa\u7b26\u53f7\u6307\u5b9a\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u3002<\/li>\n<\/ul>\n
                              val<\/span> num:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span> val<\/span> list =<\/span> num.<\/span>filter(<\/span>x =><\/span> x%<\/span>2<\/span>==<\/span>0<\/span>)<\/span> \/\/List(2, 4)<\/span> <\/code><\/pre>\n
                                \n
                              • map\uff1a\u901a\u8fc7\u7ed9\u5b9a\u7684\u65b9\u6cd5\u5c06\u6240\u6709\u5143\u7d20\u91cd\u65b0\u8ba1\u7b97<\/li>\n<\/ul>\n
                                val<\/span> num:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span> val<\/span> list =<\/span> num.<\/span>map(<\/span>x =><\/span> x*<\/span>2<\/span>)<\/span> \/\/List(2, 4, 6, 8, 10)<\/span> <\/code><\/pre>\n
                                  \n
                                • mkString\uff1a\u4f7f\u7528\u5206\u9694\u7b26\u5c06\u5217\u8868\u6240\u6709\u5143\u7d20\u4f5c\u4e3a\u5b57\u7b26\u4e32\u663e\u793a<\/li>\n<\/ul>\n
                                  val<\/span> num:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span> val<\/span> list =<\/span> num.<\/span>mkString(<\/span>\",\"<\/span>)<\/span> \/\/1,2,3,4,5<\/span> <\/code><\/pre>\n
                                    \n
                                  • foreach\uff1a\u5c06\u51fd\u6570\u5e94\u7528\u5230\u5217\u8868\u7684\u6240\u6709\u5143\u7d20<\/li>\n<\/ul>\n
                                    val<\/span> num:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span> val<\/span> list =<\/span> num.<\/span>foreach(<\/span>x=><\/span>println(<\/span>x)<\/span>)<\/span> \/\/\u51fd\u6570<\/span> \/\/---\u4ee3\u7801\u5757---<\/span> val<\/span> list =<\/span> num.<\/span>foreach(<\/span>x=><\/span>{ \n   <\/span> println(<\/span>x)<\/span> }<\/span>)<\/span> <\/code><\/pre>\n

                                    6.6 ListBuffer\u53ef\u53d8\u5217\u8868<\/h4>\n

                                    \u4f7f\u7528\u53ef\u53d8\u5217\u8868\u9996\u5148\u8981\u5bfc\u5165<\/strong>\uff1a<\/p>\n

                                    import<\/span> scala.<\/span>collection.<\/span>mutable.<\/span>ListBuffer <\/code><\/pre>\n

                                    \u5b9a\u4e49<\/strong>\u53ef\u53d8\u5217\u8868\uff1a<\/p>\n

                                    val<\/span> list =<\/span> ListBuffer[<\/span>Int<\/span>]<\/span>(<\/span>1<\/span>,<\/span> 2<\/span>,<\/span> 3<\/span>)<\/span> <\/code><\/pre>\n

                                    \u64cd\u4f5c\u53ef\u53d8\u5217\u8868<\/strong>\uff1a<\/p>\n

                                      \n
                                    • \u6dfb\u52a0\u5143\u7d20\n
                                        \n
                                      • +=<\/code>\uff1alist += 4<\/code><\/li>\n
                                      • append(data)<\/code>\uff1alist.append(4)<\/code><\/li>\n<\/ul>\n<\/li>\n
                                      • \u5220\u9664\u5143\u7d20\n
                                          \n
                                        • remove(index)<\/code>\uff1alist.remove(0)<\/code>\u5220\u9664\u4e0b\u6807\u4e3a0\u7684\u5143\u7d20<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n

                                          6.7 Set\u96c6\u5408<\/h4>\n

                                          \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cScala \u4f7f\u7528\u7684\u662f\u4e0d\u53ef\u53d8\u96c6\u5408\uff0c\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u53ef\u53d8\u96c6\u5408<\/strong>\uff0c\u9700\u8981\u5f15\u7528 scala.collection.mutable.Set<\/code> \u5305\u3002Set\u662f\u6ca1\u6709\u91cd\u590d\u7684\u5bf9\u8c61\u96c6\u5408\uff0c\u6240\u6709\u7684\u5143\u7d20\u90fd\u662f\u552f\u4e00\u7684\u3002<\/strong>
                                          \u5b9a\u4e49<\/strong>\u96c6\u5408\uff1a<\/p>\n

                                          val<\/span> set =<\/span> Set(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>)<\/span> \/\/ \u5143\u7d20\u552f\u4e00\u6027<\/span> val<\/span> set =<\/span> Set(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>)<\/span> \/\/\u4e8c\u8005\u7ed3\u679c\u76f8\u540c<\/span> <\/code><\/pre>\n

                                          \u64cd\u4f5c\u96c6\u5408<\/strong>\uff1a<\/p>\n

                                            \n
                                          • head\uff1a\u8fd4\u56de\u96c6\u5408\u7b2c\u4e00\u4e2a\u5143\u7d20<\/li>\n
                                          • tail\uff1a\u8fd4\u56de\u4e00\u4e2a\u96c6\u5408\uff0c\u5305\u542b\u9664\u4e86\u7b2c\u4e00\u5143\u7d20\u4e4b\u5916\u7684\u5176\u4ed6\u5143\u7d20<\/li>\n
                                          • isEmpty\uff1a\u5224\u65ad\u662f\u5426\u662f\u4e00\u4e2a\u7a7a\u6570\u7ec4<\/li>\n
                                          • foreach\uff1a\u904d\u5386\u96c6\u5408<\/li>\n<\/ul>\n
                                            val<\/span> set =<\/span> Set(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>)<\/span> set.<\/span>foreach(<\/span>x =><\/span> { \n   <\/span> println(<\/span>x)<\/span>}<\/span> )<\/span> <\/code><\/pre>\n
                                              \n
                                            • \u6dfb\u52a0\u5143\u7d20\uff08\u524d\u63d0\u662f\u5f15\u5165\u4e86\u4e0a\u9762\u7684\u5305\u53d8\u6210\u53ef\u53d8\u96c6\u5408<\/strong>\uff09\n
                                                \n
                                              • add(data)<\/code><\/li>\n
                                              • +=<\/code><\/li>\n<\/ul>\n<\/li>\n
                                              • \u5220\u9664\u5143\u7d20\uff08\u524d\u63d0\u662f\u5f15\u5165\u4e86\u4e0a\u9762\u7684\u5305\u53d8\u6210\u53ef\u53d8\u96c6\u5408<\/strong>\uff09\n
                                                  \n
                                                • remove(data)<\/code><\/li>\n
                                                • -=<\/code><\/li>\n<\/ul>\n<\/li>\n
                                                • \u94fe\u63a5\u4e24\u4e2a\u96c6\u5408\n
                                                    \n
                                                  • ++<\/code><\/li>\n
                                                  • set1.++(set2)<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
                                                    val<\/span> set1 =<\/span> Set(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>)<\/span> val<\/span> set2 =<\/span> Set(<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>,<\/span>6<\/span>)<\/span> val<\/span> set3 =<\/span> set1 ++<\/span> set2 val<\/span> set4 =<\/span> set1.<\/span>++<\/span>(<\/span>set2)<\/span> <\/code><\/pre>\n
                                                      \n
                                                    • \u67e5\u770b\u4e24\u4e2a\u96c6\u5408\u7684\u4ea4\u96c6\u5143\u7d20\n
                                                        \n
                                                      • set1.&(set2)<\/code><\/li>\n
                                                      • set.intersect(set2)<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
                                                        val<\/span> set1 =<\/span> Set(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>)<\/span> val<\/span> set2 =<\/span> Set(<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>,<\/span>6<\/span>)<\/span> val<\/span> set5 =<\/span> set1.<\/span>&(<\/span>set2)<\/span> val<\/span> set6 =<\/span> set1.<\/span>intersect(<\/span>set2)<\/span> <\/code><\/pre>\n
                                                          \n
                                                        • \u67e5\u627e\u96c6\u5408\u4e2d\u6700\u5927\u4e0e\u6700\u5c0f\u5143\u7d20\n
                                                            \n
                                                          • set.max<\/code><\/li>\n
                                                          • set.min<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n

                                                            6.8 Map\u6620\u5c04<\/h4>\n

                                                            Map(\u6620\u5c04)\u662f\u4e00\u79cd\u53ef\u8fed\u4ee3\u7684\u952e\u503c\u5bf9\uff08key\/value\uff09\u7ed3\u6784\u3002\u6240\u6709\u7684\u503c\u90fd\u53ef\u4ee5\u901a\u8fc7\u952e\u6765\u83b7\u53d6\u3002<\/p>\n

                                                            \u540c\u6837\u7684 Map \u6709\u4e24\u79cd\u7c7b\u578b\uff0c\u53ef\u53d8\u4e0e\u4e0d\u53ef\u53d8\uff0c\u533a\u522b\u5728\u4e8e\u53ef\u53d8\u5bf9\u8c61\u53ef\u4ee5\u4fee\u6539\u5b83\uff0c\u800c\u4e0d\u53ef\u53d8\u5bf9\u8c61\u4e0d\u53ef\u4ee5\u3002<\/p>\n

                                                            \u9ed8\u8ba4\u60c5\u51b5\u4e0b Scala \u4f7f\u7528\u4e0d\u53ef\u53d8 Map<\/strong>\u3002\u5982\u679c\u4f60\u9700\u8981\u4f7f\u7528\u53ef\u53d8\u96c6\u5408\uff0c\u4f60\u9700\u8981\u663e\u5f0f\u7684\u5f15\u5165\u3002<\/p>\n

                                                            import<\/span> scala.<\/span>collection.<\/span>mutable.<\/span>Map <\/code><\/pre>\n

                                                            \u5b9a\u4e49<\/strong> Map \u6620\u5c04\uff1a<\/p>\n

                                                            val<\/span> map1 =<\/span> Map(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span> \"b\"<\/span> -<\/span>><\/span>2<\/span>,<\/span> \"c\"<\/span> -<\/span>><\/span>3<\/span>)<\/span> <\/code><\/pre>\n

                                                            \u64cd\u4f5c<\/strong> Map \u6620\u5c04\uff1a<\/p>\n

                                                              \n
                                                            • keys<\/li>\n
                                                            • values<\/li>\n
                                                            • isEmpty<\/li>\n
                                                            • foreach<\/li>\n<\/ul>\n
                                                              val<\/span> map =<\/span> Map(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span> \"b\"<\/span> -<\/span>><\/span>2<\/span>,<\/span> \"c\"<\/span> -<\/span>><\/span>3<\/span>)<\/span> println(<\/span>map.<\/span>keys)<\/span> \/\/Set(a, b, c)<\/span> println(<\/span>map.<\/span>values)<\/span> \/\/MapLike.DefaultValuesIterable(1, 2, 3)<\/span> println(<\/span>map.<\/span>isEmpty)<\/span> \/\/false<\/span> map.<\/span>foreach(<\/span>x=><\/span>println(<\/span>x)<\/span>)<\/span> <\/code><\/pre>\n
                                                                \n
                                                              • \u6dfb\u52a0\u6620\u5c04\n
                                                                  \n
                                                                • +=<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
                                                                  val<\/span> map1 =<\/span> Map(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span> \"b\"<\/span> -<\/span>><\/span>2<\/span>,<\/span> \"c\"<\/span> -<\/span>><\/span>3<\/span>)<\/span> map1 +=<\/span> (<\/span>\"d\"<\/span> -<\/span>><\/span> 4<\/span>)<\/span> var<\/span> map =<\/span> Map[<\/span>String<\/span>,<\/span>Int<\/span>]<\/span>(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span>\"b\"<\/span> -<\/span>><\/span> 2<\/span>)<\/span> \/\/\u5f15\u7528\u53ef\u53d8,\u652f\u6301\u8bfb\u5199\u64cd\u4f5c;<\/span> map +=<\/span> (<\/span>\"c\"<\/span> -<\/span>><\/span> 3<\/span>)<\/span> \/\/\u65b0\u589e <\/span> println(<\/span>map)<\/span> val<\/span> map2 =<\/span> Map[<\/span>String<\/span>,<\/span>Int<\/span>]<\/span>(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span>\"b\"<\/span> -<\/span>><\/span> 2<\/span>)<\/span> \/\/\u5f15\u7528\u4e0d\u53ef\u53d8,\u53ea\u80fd\u7b2c\u4e00\u6b21\u5199\u5165\u503c,\u4e4b\u540e\u53ea\u80fd\u8bfb\u53d6;<\/span> map2 +=<\/span> (<\/span>\"c\"<\/span> -<\/span>><\/span> 3<\/span>)<\/span> \/\/\u6b64\u65f6\u4e0d\u80fd\u52a0,\u76f4\u63a5\u62a5\u9519; <\/span> val<\/span> map3 =<\/span> scala.<\/span>collection.<\/span>mutable.<\/span>Map[<\/span>String<\/span>,<\/span>String<\/span>]<\/span>(<\/span>)<\/span> \/\/\u5f15\u7528\u4e0d\u53ef\u53d8,\u652f\u6301\u8bfb\u5199\u64cd\u4f5c;<\/span> Map3 +=<\/span> (<\/span>\u201cc\u201d -<\/span>><\/span> 3<\/span>)<\/span> <\/code><\/pre>\n
                                                                    \n
                                                                  • \u94fe\u63a5\u4e24\u4e2a\u6620\u5c04\uff1a\u5982\u679c Map \u4e2d\u5b58\u5728\u76f8\u540c\u7684 key\uff0c\u5408\u5e76\u540e\u7684 Map \u4e2d\u7684 value \u4f1a\u88ab\u6700\u53f3\u8fb9\u7684 Map \u7684\u503c\u6240\u4ee3\u66ff\u3002\n
                                                                      \n
                                                                    • ++<\/code><\/li>\n
                                                                    • map.++()<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n
                                                                      val<\/span> map1 =<\/span> Map(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span> \"b\"<\/span> -<\/span>><\/span>2<\/span>,<\/span> \"c\"<\/span> -<\/span>><\/span>3<\/span>)<\/span> val<\/span> map2 =<\/span> Map(<\/span>\"c\"<\/span> -<\/span>><\/span> 4<\/span>,<\/span> \"d\"<\/span> -<\/span>><\/span>5<\/span>)<\/span> val<\/span> map3 =<\/span> map1 ++<\/span> map2 val<\/span> map4 =<\/span> map1.<\/span>++<\/span>(<\/span>map2)<\/span> <\/code><\/pre>\n

                                                                      6.9 Tuple\u5143\u7ec4<\/h4>\n

                                                                      \u4e0e\u5217\u8868\u4e00\u6837\uff0c\u5143\u7ec4\u4e5f\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4f46\u4e0e\u5217\u8868\u4e0d\u540c\u7684\u662f\u5143\u7ec4\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7d20\u3002<\/p>\n

                                                                      Scala \u652f\u6301\u7684\u5143\u7ec4\u6700\u5927\u957f\u5ea6\u4e3a 22\u3002<\/p>\n

                                                                      \u5b9a\u4e49 Tuple \u5143\u7ec4\uff1a\u5143\u7ec4\u7684\u503c\u662f\u901a\u8fc7\u5c06\u5355\u4e2a\u7684\u503c\u5305\u542b\u5728\u5706\u62ec\u53f7\u4e2d\u6784\u6210\u7684\u3002<\/p>\n

                                                                      val<\/span> t =<\/span> (<\/span>1<\/span>,<\/span> 3.14<\/span>,<\/span> \"Fred\"<\/span>)<\/span> val<\/span> t =<\/span> new<\/span> Tuple3(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>\"string\"<\/span>)<\/span> <\/code><\/pre>\n

                                                                      \u4f7f\u7528t._1<\/code> \u8bbf\u95ee\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c t._2<\/code> \u8bbf\u95ee\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff1a<\/p>\n

                                                                      val<\/span> t =<\/span> (<\/span>1<\/span>,<\/span> 3.14<\/span>,<\/span> \"Fred\"<\/span>)<\/span> println(<\/span>t.<\/span>_1)<\/span> \/\/1<\/span> println(<\/span>t.<\/span>_2)<\/span> \/\/3.14<\/span> println(<\/span>t.<\/span>_3)<\/span> \/\/\"Fred\"<\/span> <\/code><\/pre>\n

                                                                      \u8865\u5145\uff1aScala Option(\u9009\u9879)\u7c7b\u578b\u7528\u6765\u8868\u793a\u4e00\u4e2a\u503c\u662f\u53ef\u9009\u7684\uff08\u6709\u503c\u6216\u65e0\u503c)\u3002<\/p>\n

                                                                      6.10 \u8fed\u4ee3\u5668<\/h4>\n

                                                                      Scala Iterator\uff08\u8fed\u4ee3\u5668\uff09\u4e0d\u662f\u4e00\u4e2a\u96c6\u5408\uff0c\u5b83\u662f\u4e00\u79cd\u7528\u4e8e\u8bbf\u95ee\u96c6\u5408\u7684\u65b9\u6cd5\u3002\u8fed\u4ee3\u5668\u7684\u4e24\u4e2a\u57fa\u672c\u64cd\u4f5c\u662f next<\/code> \u548c hasNext<\/code>\u3002<\/p>\n

                                                                      val<\/span> map =<\/span> Map(<\/span>\"a\"<\/span> -<\/span>><\/span> 1<\/span>,<\/span> \"b\"<\/span> -<\/span>><\/span>2<\/span>,<\/span> \"c\"<\/span> -<\/span>><\/span>3<\/span>)<\/span> val<\/span> iterator =<\/span> map.<\/span>iterator while<\/span> (<\/span>iterator.<\/span>hasNext)<\/span>{ \n   <\/span> val<\/span> ele =<\/span> iterator.<\/span>next(<\/span>)<\/span> println(<\/span>\"key:\"<\/span> +<\/span> ele.<\/span>_1 +<\/span> \";value:\"<\/span> +<\/span> ele.<\/span>_2)<\/span> }<\/span> <\/code><\/pre>\n

                                                                      6.11 \u51fd\u6570\u7ec4\u5408\u5668<\/h4>\n
                                                                        \n
                                                                      • \n

                                                                        map\uff1a\u901a\u8fc7\u4e00\u4e2a\u51fd\u6570\u91cd\u65b0\u8ba1\u7b97\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u76f8\u540c\u6570\u76ee\u5143\u7d20\u7684\u65b0\u5217\u8868\u3002<\/p>\n<\/li>\n

                                                                      • \n

                                                                        foreach\uff1aforeach\u6ca1\u6709\u8fd4\u56de\u503c\uff0cforeach\u53ea\u662f\u4e3a\u4e86\u5bf9\u53c2\u6570\u8fdb\u884c\u4f5c\u7528\u3002<\/p>\n<\/li>\n

                                                                      • \n

                                                                        filter\uff1a\u8fc7\u6ee4\u79fb\u9664\u4f7f\u5f97\u4f20\u5165\u7684\u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3afalse\u7684\u5143\u7d20\u3002<\/p>\n<\/li>\n<\/ul>\n

                                                                        val<\/span> num:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span> num.<\/span>map(<\/span>x =><\/span> x*<\/span>2<\/span>)<\/span> num.<\/span>foreach(<\/span>x =><\/span> println(<\/span>x*<\/span>x +<\/span> \"\\t\"<\/span>)<\/span>)<\/span> num.<\/span>filter(<\/span>x =><\/span> x%<\/span>2<\/span> ==<\/span>0<\/span>)<\/span> <\/code><\/pre>\n
                                                                          \n
                                                                        • flatten\uff1a\u628a\u5d4c\u5957\u7684\u7ed3\u6784\u5c55\u5f00\uff0c\u6216\u8005\u8bf4flatten\u53ef\u4ee5\u628a\u4e00\u4e2a\u4e8c\u7ef4\u7684\u5217\u8868\u5c55\u5f00\u6210\u4e00\u4e2a\u4e00\u7ef4\u7684\u5217\u8868<\/li>\n<\/ul>\n
                                                                          val<\/span> list =<\/span> List(<\/span>List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>)<\/span>,<\/span> List(<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span>)<\/span> var<\/span> newlist =<\/span> list.<\/span>flatten \/\/List(1, 2, 3, 3, 4, 5)<\/span> <\/code><\/pre>\n
                                                                            \n
                                                                          • flatMap\uff1a\u7ed3\u5408\u4e86map\u548cflatten\u7684\u529f\u80fd\uff0c\u63a5\u6536\u4e00\u4e2a\u53ef\u4ee5\u5904\u7406\u5d4c\u5957\u5217\u8868\u7684\u51fd\u6570\uff0c\u7136\u540e\u628a\u8fd4\u56de\u7ed3\u679c\u8fde\u63a5\u8d77\u6765\u3002<\/li>\n<\/ul>\n
                                                                            val<\/span> list:<\/span>List[<\/span>List[<\/span>Int<\/span>]<\/span>]<\/span> =<\/span> List(<\/span>List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>)<\/span>,<\/span> List(<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>)<\/span>)<\/span> var<\/span> newlist =<\/span> list.<\/span>flatMap(<\/span>x =><\/span> x.<\/span>map(<\/span>_*<\/span>2<\/span>)<\/span>)<\/span> \/\/List(2, 4, 6, 6, 8, 10)<\/span> <\/code><\/pre>\n
                                                                              \n
                                                                            • groupBy\uff1a\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u5206\u7ec4\u64cd\u4f5c\uff0c\u7ed3\u679c\u5f97\u5230\u7684\u662f\u4e00\u4e2aMap\u3002<\/li>\n<\/ul>\n
                                                                              val<\/span> intList:<\/span>List[<\/span>Int<\/span>]<\/span> =<\/span> List(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>,<\/span>5<\/span>,<\/span>6<\/span>)<\/span> var<\/span> newlist =<\/span> intList.<\/span>groupBy(<\/span>x=><\/span>x%<\/span>2<\/span>==<\/span>0<\/span>)<\/span> \/\/\u7ed3\u679c: Map(false -> List(1, 3, 5), true -> List(2, 4, 6))<\/span> <\/code><\/pre>\n

                                                                              6.12 \u8bbf\u95ee\u4fee\u9970\u7b26<\/h4>\n

                                                                              \u6ca1\u6709\u6307\u5b9a\u8bbf\u95ee\u4fee\u9970\u7b26\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8bbf\u95ee\u7ea7\u522b\u90fd\u662f public<\/strong><\/p>\n

                                                                                \n
                                                                              • \u79c1\u6709\u6210\u5458(Private)\uff1a\u5728\u5d4c\u5957\u7c7b\u60c5\u51b5\u4e0b\uff0c\u5916\u5c42\u7c7b\u751a\u81f3\u4e0d\u80fd\u8bbf\u95ee\u88ab\u5d4c\u5957\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002<\/li>\n<\/ul>\n
                                                                                class<\/span> Outer{ \n   <\/span> class<\/span> Inner{ \n   <\/span> private<\/span> def<\/span> f(<\/span>)<\/span>{ \n   <\/span>println(<\/span>\"f\"<\/span>)<\/span>}<\/span> class<\/span> InnerMost{ \n   <\/span> f(<\/span>)<\/span> }<\/span> }<\/span> (<\/span>new<\/span> Inner)<\/span>.<\/span>f(<\/span>)<\/span> \/\/error<\/span> }<\/span> <\/code><\/pre>\n
                                                                                  \n
                                                                                • \u4fdd\u62a4\u6210\u5458(Protected)\uff1aProtected\u53ea\u5141\u8bb8\u4fdd\u62a4\u6210\u5458\u5728\u5b9a\u4e49\u4e86\u8be5\u6210\u5458\u7684\u7684\u7c7b\u7684\u5b50\u7c7b\u4e2d\u88ab\u8bbf\u95ee\u3002<\/li>\n
                                                                                • \u516c\u5171\u6210\u5458(Public)\uff1a\u5728\u4efb\u4f55\u5730\u65b9\u90fd\u53ef\u4ee5\u88ab\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n
                                                                                  class<\/span> Outer { \n   <\/span> class<\/span> Inner { \n   <\/span> def<\/span> f(<\/span>)<\/span> { \n   <\/span> println(<\/span>\"f\"<\/span>)<\/span> }<\/span> class<\/span> InnerMost { \n   <\/span> f(<\/span>)<\/span> \/\/ correct<\/span> }<\/span> }<\/span> (<\/span>new<\/span> Inner)<\/span>.<\/span>f(<\/span>)<\/span> \/\/ correct <\/span> }<\/span> <\/code><\/pre>\n

                                                                                  6.13 \u7c7b<\/h4>\n

                                                                                  Scala\u662f\u4e00\u79cd\u7eaf\u7cb9\u7684\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\uff0c\u4e24\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\uff1a\u7c7b\u548c\u5bf9\u8c61\u3002\u7c7b\u662f\u5bf9\u8c61\u7684\u62bd\u8c61\uff0c\u4e5f\u53ef\u4ee5\u628a\u7c7b\u7406\u89e3\u4e3a\u6a21\u677f\uff0c\u5bf9\u8c61\u624d\u662f\u771f\u6b63\u7684\u5b9e\u4f53\u3002Scala\u4e2d\u7684\u7c7b\u4e0d\u58f0\u660e\u4e3apublic\u3002Scala \u7684\u7c7b\u5b9a\u4e49\u53ef\u4ee5\u6709\u53c2\u6570\uff0c\u79f0\u4e3a\u7c7b\u53c2\u6570\uff1b\u7c7b\u53c2\u6570\u5728\u6574\u4e2a\u7c7b\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002<\/p>\n

                                                                                  \u7c7b\u7684\u4f7f\u7528\uff1a<\/p>\n

                                                                                  class<\/span> Point(<\/span>xc:<\/span> Int<\/span>,<\/span> yc:<\/span> Int<\/span>)<\/span> { \n   <\/span> var<\/span> x:<\/span> Int<\/span> =<\/span> xc var<\/span> y:<\/span> Int<\/span> =<\/span> yc def<\/span> move(<\/span>dx:<\/span> Int<\/span>,<\/span> dy:<\/span> Int<\/span>)<\/span> { \n   <\/span> x =<\/span> x +<\/span> dx y =<\/span> y +<\/span> dy println (<\/span>\"x : \"<\/span> +<\/span> x)<\/span>;<\/span> println (<\/span>\"y : \"<\/span> +<\/span> y)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

                                                                                  \u4e00\u4e2aScala\u6e90\u6587\u4ef6\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2a\u7c7b\u3002\u53ef\u4ee5\u4f7f\u7528 new \u6765\u5b9e\u4f8b\u5316\u5bf9\u8c61<\/strong>\uff0c\u5e76\u8bbf\u95ee\u7c7b\u4e2d\u7684\u65b9\u6cd5\u548c\u53d8\u91cf<\/p>\n

                                                                                  \/\/scala\u6587\u4ef6<\/span> import<\/span> java.<\/span>io.<\/span>_ class<\/span> Point(<\/span>xc:<\/span> Int<\/span>,<\/span> yc:<\/span> Int<\/span>)<\/span> { \n   <\/span> var<\/span> x:<\/span> Int<\/span> =<\/span> xc var<\/span> y:<\/span> Int<\/span> =<\/span> yc def<\/span> move(<\/span>dx:<\/span> Int<\/span>,<\/span> dy:<\/span> Int<\/span>)<\/span> { \n   <\/span> x =<\/span> x +<\/span> dx y =<\/span> y +<\/span> dy println (<\/span>\"x \u7684\u5750\u6807\u70b9: \"<\/span> +<\/span> x)<\/span>;<\/span> println (<\/span>\"y \u7684\u5750\u6807\u70b9: \"<\/span> +<\/span> y)<\/span>;<\/span> }<\/span> }<\/span> object<\/span> Test { \n   <\/span> def<\/span> main(<\/span>args:<\/span> Array[<\/span>String<\/span>]<\/span>)<\/span> { \n   <\/span> val<\/span> pt =<\/span> new<\/span> Point(<\/span>10<\/span>,<\/span> 20<\/span>)<\/span>;<\/span> \/\/ \u79fb\u5230\u4e00\u4e2a\u65b0\u7684\u4f4d\u7f6e<\/span> pt.<\/span>move(<\/span>10<\/span>,<\/span> 10<\/span>)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

                                                                                  \u7c7b\u7684\u7ee7\u627f<\/strong>\uff1aScala \u4f7f\u7528 extends \u5173\u952e\u5b57\u6765\u7ee7\u627f\u4e00\u4e2a\u7c7b\uff0c\u5b50\u7c7b\u4f1a\u7ee7\u627f\u7236\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0cScala \u53ea\u5141\u8bb8\u7ee7\u627f\u4e00\u4e2a\u7236\u7c7b\u3002<\/p>\n

                                                                                  Scala\u7ee7\u627f\u4e00\u4e2a\u57fa\u7c7b\u8ddfJava\u5f88\u76f8\u4f3c, \u4f46\u6211\u4eec\u9700\u8981\u6ce8\u610f<\/strong>\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n

                                                                                  1\u3001\u91cd\u5199\u4e00\u4e2a\u975e\u62bd\u8c61\u65b9\u6cd5\u5fc5\u987b\u4f7f\u7528override\u4fee\u9970\u7b26\u3002<\/p>\n

                                                                                  2\u3001\u53ea\u6709\u4e3b\u6784\u9020\u51fd\u6570\u624d\u53ef\u4ee5\u5f80\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u91cc\u5199\u53c2\u6570\u3002<\/p>\n

                                                                                  3\u3001\u5728\u5b50\u7c7b\u4e2d\u91cd\u5199\u8d85\u7c7b\u7684\u62bd\u8c61\u65b9\u6cd5\u65f6\uff0c\u4f60\u4e0d\u9700\u8981\u4f7f\u7528override\u5173\u952e\u5b57<\/p>\n

                                                                                  class<\/span> Location(<\/span>val<\/span> xc:<\/span> Int<\/span>,<\/span> val<\/span> yc:<\/span> Int<\/span>,<\/span> val<\/span> zc :<\/span>Int<\/span>)<\/span> extends<\/span> Point(<\/span>xc,<\/span> yc)<\/span>{ \n   <\/span> var<\/span> z:<\/span> Int<\/span> =<\/span> zc override<\/span> def<\/span> move(<\/span>dx:<\/span> Int<\/span>,<\/span> dy:<\/span> Int<\/span>)<\/span> { \n   <\/span> x =<\/span> x +<\/span> dx +<\/span> 100<\/span> y =<\/span> y +<\/span> dy +<\/span> 100<\/span> println (<\/span>\"x location : \"<\/span> +<\/span> x)<\/span>;<\/span> println (<\/span>\"y location : \"<\/span> +<\/span> y)<\/span>;<\/span> }<\/span> def<\/span> move(<\/span>dx:<\/span> Int<\/span>,<\/span> dy:<\/span> Int<\/span>,<\/span> dz:<\/span> Int<\/span>)<\/span> { \n   <\/span> x =<\/span> x +<\/span> dx y =<\/span> y +<\/span> dy z =<\/span> z +<\/span> dz println (<\/span>\"x : \"<\/span> +<\/span> x)<\/span>;<\/span> println (<\/span>\"y : \"<\/span> +<\/span> y)<\/span>;<\/span> println (<\/span>\"z : \"<\/span> +<\/span> z)<\/span>;<\/span> }<\/span> }<\/span> object<\/span> Test { \n   <\/span> def<\/span> main(<\/span>args:<\/span> Array[<\/span>String<\/span>]<\/span>)<\/span> { \n   <\/span> val<\/span> loc =<\/span> new<\/span> Location(<\/span>10<\/span>,<\/span> 20<\/span>,<\/span> 15<\/span>)<\/span>;<\/span> loc.<\/span>move(<\/span>10<\/span>,<\/span> 10<\/span>)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

                                                                                  \u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u4e2d\u5df2\u7ecf\u5b9e\u73b0<\/strong>\u7684\u65b9\u6cd5\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57override<\/code>\uff0c\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u4e2d\u672a\u5b9e\u73b0<\/strong>\u7684\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u7528override<\/code>\u5173\u952e\u5b57\u3002<\/p>\n

                                                                                  \/\/Cat Animal \u7236\u5b50\u7c7b\u65b9\u6cd5\u7684\u8986\u76d6<\/span> \/\/\u4f7f\u7528eat\u65b9\u6cd5\uff0c\u4f7f\u7528{}\u5305\u5bb9\u4ee3\u7801\uff0c\u5728\u65b9\u6cd5\u91cc\u4f7f\u7528println<\/span> abstract<\/span> class<\/span> Animal { \n   <\/span> def<\/span> showName(<\/span>str:<\/span>String<\/span>)<\/span>=<\/span>{ \n   <\/span>println(<\/span>\"animal\"<\/span>)<\/span>}<\/span> def<\/span> eat(<\/span>food:<\/span>String<\/span>)<\/span> }<\/span> class<\/span> Cat extends<\/span> Animal { \n   <\/span> override<\/span> def<\/span> showName(<\/span>str:<\/span>String<\/span>)<\/span>=<\/span>{ \n   <\/span>println(<\/span>\"cat\"<\/span>)<\/span>}<\/span> \/\/\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u4e2d\u5df2\u7ecf\u5b9e\u73b0\u7684\u65b9\u6cd5\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57`override`<\/span> def<\/span> eat(<\/span>food:<\/span>String<\/span>)<\/span> =<\/span> { \n   <\/span>println(<\/span>\"fish\"<\/span>)<\/span>}<\/span>\/\/\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u4e2d\u672a\u5b9e\u73b0\u7684\u65b9\u6cd5\u53ef\u4ee5\u4e0d\u7528`override`\u5173\u952e\u5b57\u3002<\/span> }<\/span> <\/code><\/pre>\n

                                                                                  \u5355\u4f8b\u5bf9\u8c61<\/strong>\uff1a\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u53ea\u6709\u8fd9\u4e48\u4e00\u4e2a\u5b9e\u4f8b\u3002Scala\u4e2d\u6ca1\u6709static\u5173\u952e\u5b57\uff0c\u56e0\u6b64Scala\u7684\u7c7b\u4e2d\u4e0d\u5b58\u5728\u9759\u6001\u6210\u5458\u3002 Scala\u4e2d\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u65f6\u9700\u8981\u4f7f\u7528object\u5b9a\u4e49\u4e00\u4e2a\u5355\u4f8b\u5bf9\u8c61\u3002object \u5bf9\u8c61\u4e0e\u7c7b\u7684\u533a\u522b\u5728\u4e8eobject \u5bf9\u8c61\u4e0d\u80fd\u5e26\u53c2\u6570\u3002\u5305\u542b main \u65b9\u6cd5\u7684 object \u5bf9\u8c61\u53ef\u4ee5\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002<\/p>\n

                                                                                  \u4f34\u751f\u5bf9\u8c61<\/strong>\uff1a\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5355\u4f8b\u5bf9\u8c61\uff0c\u662f\u4e00\u79cd\u76f8\u5bf9\u6982\u5ff5\u3002\u9700\u8981\u4e24\u4e2a\u6761\u4ef6\uff1a<\/p>\n

                                                                                    \n
                                                                                  • \u5728\u540c\u4e00\u4e2a\u6e90\u6587\u4ef6\u4e2d<\/li>\n
                                                                                  • \u5bf9\u8c61\u540d\u548c\u7c7b\u540d\u76f8\u540c<\/li>\n<\/ul>\n

                                                                                    \u7c7b\u548c\u4f34\u751f\u5bf9\u8c61\u4e4b\u95f4\u53ef\u4ee5\u76f8\u4e92\u8bbf\u95ee\u79c1\u6709\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n

                                                                                    \u6784\u9020\u5668:<\/strong>
                                                                                    \u6784\u9020\u5668\u5206\u4e3a\u4e24\u7c7b\uff1a\u4e3b\u6784\u9020\u5668\uff08\u53ea\u6709\u4e00\u4e2a\uff09\uff0c\u8f85\u52a9\u6784\u9020\u5668\uff08\u6709\u591a\u4e2a\uff09<\/p>\n

                                                                                    \u4e3b\u6784\u9020\u5668<\/strong>\u76f4\u63a5\u5728\u7c7b\u540d\u540e\u9762\u5b9a\u4e49\uff0c\u6bcf\u4e2a\u7c7b\u90fd\u6709\u4e3b\u6784\u9020\u5668\uff0c\u4e3b\u6784\u9020\u5668\u7684\u53c2\u6570\u76f4\u63a5\u653e\u5728\u7c7b\u540d\u540e\uff0c\u4e0e\u7c7b\u4ea4\u7ec7\u5728\u4e00\u8d77
                                                                                    \u5982\u679c\u6ca1\u6709\u5b9a\u4e49\u6784\u9020\u5668\uff0c\u7c7b\u4f1a\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u7a7a\u53c2\u6784\u9020\u5668<\/p>\n

                                                                                    class<\/span> Point(<\/span>xc:<\/span> Int<\/span>,<\/span> yc:<\/span> Int<\/span>)<\/span> { \n   <\/span>\/\/\u4e3b\u6784\u9020\u5668<\/span> var<\/span> x:<\/span> Int<\/span> =<\/span> xc var<\/span> y:<\/span> Int<\/span> =<\/span> yc def<\/span> move(<\/span>dx:<\/span> Int<\/span>,<\/span> dy:<\/span> Int<\/span>)<\/span> { \n   <\/span> x =<\/span> x +<\/span> dx y =<\/span> y +<\/span> dy println (<\/span>\"x \u7684\u5750\u6807\u70b9: \"<\/span> +<\/span> x)<\/span>;<\/span> println (<\/span>\"y \u7684\u5750\u6807\u70b9: \"<\/span> +<\/span> y)<\/span>;<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

                                                                                    \u8f85\u52a9\u6784\u9020\u5668<\/strong>\u5b9a\u4e49\uff0c\u4f7f\u7528def this\uff0c\u5fc5\u987b\u8c03\u7528\u4e3b\u6784\u9020\u5668\uff0c\u6216\u8005\u5176\u4ed6\u6784\u9020\u5668\u3002<\/p>\n

                                                                                    class<\/span> Person { \n   <\/span> \/\/\u7a7a\u53c2\u6784\u9020\u5668<\/span> var<\/span> name:<\/span>String<\/span> =<\/span> \"Tom\"<\/span> var<\/span> sex:<\/span>String<\/span> =<\/span> \"man\"<\/span> val<\/span> age:<\/span>Int<\/span> =<\/span>18<\/span> println(<\/span>\"main constructor\"<\/span>)<\/span> def<\/span> this<\/span>(<\/span>name:<\/span>String<\/span>)<\/span>{ \n   <\/span> \/\/\u8f85\u52a9\u6784\u9020\u5668<\/span> this<\/span>(<\/span>)<\/span> println(<\/span>\"name constructor\"<\/span>)<\/span> }<\/span> def<\/span> this<\/span>(<\/span>name:<\/span>String<\/span>,<\/span> sex:<\/span>String<\/span>)<\/span> { \n   <\/span> this<\/span>(<\/span>name)<\/span> println(<\/span>\"name sex construtor\"<\/span>)<\/span> }<\/span> }<\/span> <\/code><\/pre>\n

                                                                                    \u6ce8\u610f\uff1a<\/strong><\/p>\n

                                                                                      \n
                                                                                    • \u6709\u4e24\u7c7b\u6784\u9020\u5668\uff0c\u4e3b\u6784\u9020\u5668\u548c\u8f85\u6784\u9020\u5668<\/li>\n
                                                                                    • \u8f85\u6784\u9020\u5668\u7684\u53c2\u6570\u4e0d\u80fd\u548c\u4e3b\u6784\u9020\u5668\u7684\u53c2\u6570\u5b8c\u5168\u4e00\u81f4\uff08\u53c2\u6570\u4e2a\u6570\u3001\u7c7b\u578b\u3001\u987a\u5e8f\uff09<\/li>\n
                                                                                    • \u53ef\u4ee5\u5b9a\u4e49\u7a7a\u53c2\u7684\u8f85\u52a9\u6784\u9020\u5668\uff0c\u4f46\u662f\u4e3b\u6784\u9020\u5668\u53c2\u6570\u5fc5\u987b\u8fdb\u884c\u521d\u59cb\u5316\u8d4b\u503c<\/li>\n
                                                                                    • \u8f85\u6784\u9020\u5668\u7684\u4f5c\u7528\u57df\u53ea\u5728\u65b9\u6cd5\u4e2d\uff0c\u4e3b\u6784\u9020\u5668\u7684\u4f5c\u7528\u57df\u662f\u7c7b\u4e2d\u9664\u53bb\u6210\u5458\u5c5e\u6027\u548c\u6210\u5458\u65b9\u6cd5\u5916\u7684\u6240\u6709\u8303\u56f4<\/li>\n<\/ul>\n

                                                                                      \u4e09\u3001IDE\u4e2d\u7684Spark\u7a0b\u5e8f<\/h2>\n

                                                                                      1\u3001SparkContext\uff08\u80cc\u719f\uff0c\u5fc5\u8003\uff09<\/h3>\n

                                                                                      1.1 SparkContext \u4ecb\u7ecd<\/h4>\n

                                                                                      \u200b \u4efb\u4f55Spark\u7a0b\u5e8f\u90fd\u662f\u4ee5SparkContext\u5bf9\u8c61\u5f00\u59cb\u7684\uff0c\u56e0\u4e3aSparkContext\u662fSpark\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u548c\u5165\u53e3<\/strong>\uff0c\u90fd\u662f\u901a\u8fc7SparkContext\u5bf9\u8c61\u7684\u5b9e\u4f8b\u6765\u521b\u5efaRDD\u3002\u56e0\u6b64\u5728\u5b9e\u9645Spark\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u4e2d\uff0c\u5728main\u65b9\u6cd5\u4e2d\u9700\u8981\u521b\u5efaSparkContext\u5bf9\u8c61\uff0c\u4f5c\u4e3aSpark\u5e94\u7528\u7a0b\u5e8f\u7684\u5165\u53e3\uff0c\u5e76\u5728Spark\u7a0b\u5e8f\u7ed3\u675f\u65f6\u5173\u95edSparkContext\u5bf9\u8c61\u3002<\/p>\n

                                                                                      1.2 SparkContext\u521d\u59cb\u5316<\/h4>\n

                                                                                      \u901a\u8fc7SparkConf\u5bf9\u8c61\u8bbe\u7f6e\u96c6\u7fa4\u914d\u7f6e\u7684\u5404\u79cd\u53c2\u6570\u3002<\/p>\n

                                                                                      val<\/span> conf =<\/span> new<\/span> SparkConf(<\/span>)<\/span>.<\/span>setMaster(<\/span>\"local\"<\/span>)<\/span>.<\/span>setAppName(<\/span>\"appName\"<\/span>)<\/span> var<\/span> sc =<\/span> new<\/span> SparkContext(<\/span>conf)<\/span> <\/code><\/pre>\n

                                                                                      \u56db\u3001spark\u5206\u5e03\u5f0f\u5b58\u50a8<\/h2>\n

                                                                                      1\u3001\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5206\u5e03\u5f0f\u5b58\u50a8\uff1f\u5206\u5e03\u5f0f\u5b58\u50a8\u7684\u7279\u70b9\uff1f<\/h3>\n
                                                                                        \n
                                                                                      • \u6570\u636e\u5206\u5757\u5b58\u50a8\u5728\u591a\u53f0\u673a\u5668\u4e0a<\/li>\n
                                                                                      • \u6bcf\u4e00\u6570\u636e\u5757\u90fd\u53ef\u4ee5\u5197\u4f59\u5b58\u50a8\u5728\u591a\u53f0\u673a\u5668\u4e0a\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u5757\u7684\u9ad8\u53ef\u7528\u6027<\/li>\n<\/ul>\n

                                                                                        2\u3001\u5982\u4f55\u7ba1\u7406\u5206\u5e03\u5f0f\u5b58\u50a8\uff1f<\/h3>\n

                                                                                        \u5728\u53e6\u5916\u4e00\u53f0\u673a\u5668\u4e0a\u542f\u52a8\u4e00\u4e2a\u7ba1\u7406\u6240\u6709\u8282\u70b9\u4ee5\u53ca\u5b58\u50a8\u5728\u4e0a\u9762\u6570\u636e\u5757\u7684\u670d\u52a1\u3002<\/p>\n

                                                                                        3\u3001\u6587\u4ef6\u7684\u6982\u5ff5<\/h3>\n
                                                                                          \n
                                                                                        • \u5b58\u5728\u4e8eslave\u4e0a\u7684\u6587\u4ef6\uff1a\u8868\u793a\u771f\u5b9e\u5b58\u653e\u6570\u636e\u7684\u6587\u4ef6\uff0c \u5373\u672c\u5730\u78c1\u76d8\u6587\u4ef6<\/li>\n
                                                                                        • \u5b58\u5728\u4e8emaster\u4e0a\u7684\u6587\u4ef6\uff1a\u8868\u793a\u903b\u8f91\u6587\u4ef6<\/li>\n<\/ul>\n

                                                                                          4\u3001\u5206\u5e03\u5f0f\u5b58\u50a8\u7cfb\u7edfHDFS\u5e38\u89c1linux\u547d\u4ee4\uff08\u91cd\u70b9\uff09<\/h3>\n
                                                                                            \n
                                                                                          • \u67e5\u770bhdfs\u6587\u4ef6\u7cfb\u7edf\u7684\u65b9\u5f0f\uff1a hadoop fs \u2013ls \/user\/hadoop-twq<\/code><\/li>\n
                                                                                          • \u4e0a\u4f20\u6587\u4ef6\uff1a hadoop fs -copyFromLocal word.txt \/users\/hadoop-twq<\/code><\/li>\n
                                                                                          • \u4e0b\u8f7d\u6587\u4ef6\uff1a hadoop fs -get \/users\/hadoop-twq\/word.txt<\/code><\/li>\n
                                                                                          • \u8bbe\u7f6e\u6570\u636e\u5757\u7684\u5907\u4efd\u6570\uff1ahadoop fs -setrep 2 \/users\/hadoop-twq\/word.txt<\/code><\/li>\n
                                                                                          • \u5220\u9664\u6587\u4ef6\u7684\u65b9\u5f0f\uff1ahadoop fs -rm \/users\/hadoop-twq\/word.txt<\/code><\/li>\n<\/ul>\n

                                                                                            5\u3001Spark\u5206\u5e03\u5f0f\u8ba1\u7b97\u6982\u8ff0\uff08\u91cd\u70b9\uff09<\/h3>\n

                                                                                            \u7b80\u4ecb<\/strong>\uff1a\u5728\u6bcf\u4e00\u4e2ablock\u6240\u5728\u7684\u673a\u5668\u9488\u5bf9block\u6570\u636e\u8fdb\u884c\u8ba1\u7b97\uff0c\u5c06\u7ed3\u679c\u6c47\u603b\u5230\u8ba1\u7b97master
                                                                                            \u539f\u5219<\/strong>\uff1a \u79fb\u52a8\u8ba1\u7b97\u800c\u5c3d\u53ef\u80fd\u5c11\u7684\u79fb\u52a8\u6570\u636e
                                                                                            \u542b\u4e49<\/strong>\uff1a \u5176\u5b9e\u5c31\u662f\u5c06\u5355\u53f0\u673a\u5668\u4e0a\u7684\u8ba1\u7b97\u6269\u5c55\u5230\u591a\u53f0\u673a\u5668\u4e0a\u8fdb\u884c\u5e76\u884c\u8ba1\u7b97
                                                                                            \u7279\u70b9<\/strong>\uff1a\u5c31\u8fd1\u8ba1\u7b97\uff0c\u8ba1\u7b97\u5206\u644a\u5230\u6bcf\u4e2a\u8282\u70b9\uff0c\u57fa\u4e8e\u6027\u8d28\u76f8\u540c\u7684\u6bcf\u4e2a\u6570\u636e\u5757\uff0c\u540c\u65f6\u4f7f\u7528\u76f8\u540c\u65b9\u6cd5\u6765\u8ba1\u7b97\u3002
                                                                                            \u57fa\u672c\u7ed3\u6784<\/strong>\uff1a\u8ba1\u7b97\u4e3b\u8282\u70b9\u548c\u8d44\u6e90\u4e3b\u8282\u70b9<\/p>\n

                                                                                            6\u3001\u6570\u636e\u7684\u5206\u533a\u65b9\u5f0f\u662f\u4ec0\u4e48\u6837\u7684\uff1f<\/h3>\n

                                                                                            \u6587\u4ef6\u7684\u6bcf\u4e00\u4e2ablock\u5c31\u662f\u4e00\u4e2a\u5206\u533a\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u62102\u4e2ablock\u4e00\u4e2a\u5206\u533a\uff0c\u5bf9\u4e8ekey-value\u7c7b\u578b\u7684\u6570\u636e\u7684\u5206\u533a\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636ekey\u6309\u7167\u67d0\u79cd\u89c4\u5219\u6765\u8fdb\u884c\u5206\u533a\uff0c\u6bd4\u5982\u6309\u7167key\u7684hash\u503c\u6765\u5206\u533a\u3002<\/p>\n

                                                                                            7\u3001\u5728\u8ba1\u7b97\u4f0a\u59cb\u8bfb\u53d6\u5206\u533a\u6570\u636e\u7684\u65f6\u5019\uff0c\u4f1a\u53d1\u751f\u4ece\u5176\u4ed6\u673a\u5668\u8282\u70b9\u901a\u8fc7\u7f51\u7edc\u4f20\u8f93\u8bfb\u53d6\u6570\u636e\u5417\uff1f<\/h3>\n

                                                                                            \u53ef\u80fd\u4f1a\u53d1\u751f\uff0c\u4f46\u662f\u9700\u8981\u5c3d\u91cf\u907f\u514d\uff0c\u6211\u4eec\u9700\u8981\u9075\u5faa\u79fb\u52a8\u8ba1\u7b97\u800c\u4e0d\u79fb\u52a8\u6570\u636e\u7684\u539f\u5219\u3002\u6bcf\u4e00\u4e2a\u6570\u636e\u5757\u90fd\u5305\u542b\u4e86\u5b83\u6240\u5728\u7684\u673a\u5668\u7684\u4fe1\u606f\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u8fd9\u4e2a\u6570\u636e\u5757\u6240\u5728\u7684\u673a\u5668\uff0c\u7136\u540e\u5c06\u8ba1\u7b97\u4efb\u52a1\u53d1\u9001\u5230\u5bf9\u5e94\u7684\u673a\u5668\u4e0a\u6765\u6267\u884c\uff0c\u8fd9\u4e2a\u5c31\u662f\u8ba1\u7b97\u4efb\u52a1\u7684\u672c\u5730\u6027\u3002<\/p>\n

                                                                                            8\u3001\u6bcf\u4e00\u6b65\u51fa\u73b0\u7684\u6570\u636e\u90fd\u662f\u4e00\u4efd\u5b58\u50a8\u5417\uff1f<\/h3>\n

                                                                                            \u4e0d\u662f\uff0c\u6570\u636e\u7684\u5b58\u50a8\u53ea\u6709\u4e00\u4efd\uff0c\u5c31\u662f\u4e00\u5f00\u59cb\u7684\u6570\u636e\u5b58\u50a8\uff0c\u5728 shuffle \u7684\u65f6\u5019\u4f1a\u6709\u4e2d\u95f4\u4e34\u65f6\u6570\u636e\u7684\u5b58\u50a8\u3002<\/p>\n

                                                                                            9\u3001MapReduce\u662f\u57fa\u4e8e\u78c1\u76d8\u7684\uff1f\u662f\u8fd9\u6837\u5417\uff1f<\/h3>\n

                                                                                            MapReduce\u662f\u57fa\u4e8e\u78c1\u76d8\u7684\u3002<\/p>\n

                                                                                            10\u3001Shuffle\u8fc7\u7a0bspark\u662f\u57fa\u4e8e\u5185\u5b58\u7684\uff1f<\/h3>\n

                                                                                            \u4e0d\u5b8c\u5168\u57fa\u4e8e\u5185\u5b58\u3002spark\u7684shuffle\u4e2d\u95f4\u7ed3\u679c\u4e5f\u662f\u9700\u8981\u5199\u6587\u4ef6\u7684\uff0c\u53ea\u662f\u5bf9\u5185\u5b58\u7684\u5229\u7528\u6bd4\u8f83\u5145\u5206\u800c\u5df2\u3002<\/p>\n

                                                                                            11\u3001\u5927\u6570\u636e\u4e2d\u95f4\u7ed3\u679c\u6570\u636e\u7684\u590d\u7528\u573a\u666f\uff0c\u5b58\u50a8\u65b9\u5f0f\uff1f<\/h3>\n

                                                                                            \u590d\u7528\u573a\u666f\uff1a\u4e00\u79cd\u662f\u8fed\u4ee3\u5f0f\u8ba1\u7b97\u5e94\u7528\uff1b\u4e00\u79cd\u662f\u4ea4\u4e92\u578b\u6570\u636e\u6316\u6398\u5e94\u7528
                                                                                            \u5b58\u50a8\u65b9\u5f0f\uff1ahadoop\uff08\u78c1\u76d8\uff09Spark\uff08\u5185\u5b58\uff09<\/p>\n

                                                                                            12\u3001Spark\u5206\u5e03\u5f0f\u5185\u5b58\u7684\u4f18\u70b9\uff1f<\/h3>\n

                                                                                            \u5c06\u4e2d\u95f4\u7ed3\u679c\u653e\u5230\u5185\u5b58\u4e2d\uff0c\u5145\u5206\u53d1\u6325\u5185\u5b58\u7684\u8bfb\u5199\u6548\u7387\uff0c\u907f\u514d\u91cd\u590d\u6027\u7684\u5de5\u4f5c\u4e00\u904d\u904d\u6d6a\u8d39CPU\u3002<\/p>\n

                                                                                            \u4e94\u3001Spark\u7f16\u7a0b\u57fa\u7840<\/h2>\n

                                                                                            1\u3001\u4ec0\u4e48\u662fRDD\uff1f\uff08\u80cc\u719f\uff0c\u5fc5\u8003\uff09<\/h3>\n

                                                                                            \uff081\uff09RDD\u5b9a\u4e49<\/strong>\uff1a RDD\u53eb\u505a\u5f39\u6027\u5206\u5e03\u5f0f\u6570\u636e\u96c6 ,\u662fSpark\u4e2d\u6700\u57fa\u672c\u7684\u6570\u636e\u62bd\u8c61\uff0c\u5b83\u4ee3\u8868\u4e00\u4e2a\u4e0d\u53ef\u53d8\u3001\u53ef\u5206\u533a\u3001\u91cc\u9762\u7684\u5143\u7d20\u53ef\u5e76\u884c\u8ba1\u7b97\u7684\u96c6\u5408\u3002<\/p>\n

                                                                                            \uff082\uff09\u4f7f\u7528RDD\u7684\u9a71\u52a8\u529b<\/strong>\uff1a<\/p>\n

                                                                                              \n
                                                                                            • \u5feb\u901f<\/strong>\uff1a\u53ef\u4f38\u7f29\u6027\u3001\u4f4d\u7f6e\u611f\u77e5\u8c03\u5ea6\u3001\u5e76\u884c\u8f6c\u6362\u3001\u6570\u636e\u5206\u533a<\/li>\n
                                                                                            • \u51c6\u786e<\/strong>\uff1a\u7ebf\u6027\u4f9d\u8d56\u3001\u53ef\u5e8f\u5217\u5316\u3001\u5931\u8d25\u81ea\u52a8\u91cd\u5efa\u3001\u81ea\u52a8\u5bb9\u9519<\/li>\n<\/ul>\n

                                                                                              \u6ce8\u610f\uff1aRDD\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u53ef\u66f4\u6539<\/strong>\u3002\u91cd\u5efa\u4e0d\u662f\u4ece\u6700\u5f00\u59cb\u7684\u70b9\u6765\u91cd\u5efa\u7684\uff0c\u53ef\u4ee5\u662f\u4e0a\u4e00\u6b65\u5f00\u59cb\u91cd\u5efa<\/p>\n

                                                                                              \uff083\uff09RDD\u7279\u70b9<\/strong>\uff1a<\/p>\n

                                                                                                \n
                                                                                              • \u4e0d\u53ef\u66f4\u6539\u6027<\/strong>\uff1a\u4e00\u65e6\u521b\u5efa\u5c31\u4e0d\u53ef\u66f4\u6539\uff0c\u4fdd\u8bc1\u6570\u636e\u7a33\u5b9a\u3001\u53ef\u8ffd\u6eaf\u6027\u3001\u5bb9\u9519\u6027<\/li>\n
                                                                                              • \u6570\u636e\u6d41\u6a21\u578b\u7279\u70b9<\/strong>\uff1a\u81ea\u52a8\u5bb9\u9519\u3001\u4f4d\u7f6e\u611f\u77e5\u6027\u8c03\u5ea6\u548c\u53ef\u4f38\u7f29\u6027\u3002<\/li>\n
                                                                                              • \u6570\u636e\u5206\u533a\u7279\u6027<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7684\u672c\u5730\u6027\u6765\u63d0\u9ad8\u6027\u80fd\uff0c\u8fd9\u4e0eHadoop MapReduce\u662f\u4e00\u6837\u7684\uff1b<\/li>\n
                                                                                              • \u5e76\u884c\u8f6c\u6362<\/strong>\uff1a\u5e76\u884c\u65b9\u5f0f\u6765\u521b\u5efa\u5982\uff08map\u3001filter\u3001join\u7b49\uff09\uff1b<\/li>\n
                                                                                              • \u53ef\u5e8f\u5217\u5316<\/strong>\uff1a\u5728\u5185\u5b58\u4e0d\u8db3\u65f6\u53ef\u81ea\u52a8\u964d\u7ea7\u4e3a\u78c1\u76d8\u5b58\u50a8\uff0c\u628aRDD\u5b58\u50a8\u4e8e\u78c1\u76d8\u4e0a<\/li>\n
                                                                                              • \u6570\u636e\u9ad8\u5ea6\u91cd\u7528<\/strong>\uff1aRDD\u5141\u8bb8\u7528\u6237\u5728\u6267\u884c\u591a\u4e2a\u67e5\u8be2\u65f6\u663e\u5f0f\u5730\u5c06\u5de5\u4f5c\u96c6\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u540e\u7eed\u67e5\u8be2\u80fd\u591f\u91cd\u7528\u5de5\u4f5c\u96c6\uff0c\u6781\u5927\u5730\u63d0\u5347\u67e5\u8be2\u901f\u5ea6\u3002<\/li>\n
                                                                                              • \u5bb9\u9519\u6027<\/strong>\uff1aRDD\u53ea\u80fd\u4ece\u6301\u4e45\u5b58\u50a8\u6216\u901a\u8fc7Transformations\u64cd\u4f5c\u4ea7\u751f\uff0c\u76f8\u6bd4\u4e8e\u5206\u5e03\u5f0f\u5171\u4eab\u5185\u5b58\uff08DSM\uff09\u53ef\u4ee5\u66f4\u9ad8\u6548\u5b9e\u73b0\u5bb9\u9519\uff0c\u5bf9\u4e8e\u4e22\u5931\u90e8\u5206\u6570\u636e\u5206\u533a\u53ea\u9700\u8981\u6839\u636e\u5b83\u7684lineage\u5c31\u53ef\u91cd\u65b0\u8ba1\u7b97\u51fa\u6765\uff0c\u800c\u4e0d\u9700\u8981\u505a\u7279\u5b9acheckpoint\uff1b<\/li>\n<\/ul>\n

                                                                                                \uff084\uff09RDD\u4e24\u79cd\u7b97\u5b50<\/strong>\uff1a<\/p>\n

                                                                                                  \n
                                                                                                • \u8f6c\u6362\u64cd\u4f5c<\/strong>\uff08Transformations\uff09\uff1aTransformations\u64cd\u4f5c\u662fLazy\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u4ece\u4e00\u4e2aRDD\u8f6c\u6362\u751f\u6210\u53e6\u4e00\u4e2aRDD\u7684\u64cd\u4f5c\u4e0d\u662f\u9a6c\u4e0a\u6267\u884c\uff0cspark\u5728\u9047\u5230Transformations\u64cd\u4f5c\u65f6\u53ea\u4f1a\u8bb0\u5f55\u9700\u8981\u8fd9\u6837\u7684\u64cd\u4f5c\uff0c\u5e76\u4e0d\u4f1a\u53bb\u6267\u884c\uff0c\u9700\u8981\u7b49\u5230\u6709Actions\u64cd\u4f5c\u7684\u65f6\u5019\u624d\u4f1a\u771f\u6b63\u542f\u52a8\u8ba1\u7b97\u8fc7\u7a0b\u8fdb\u884c\u8ba1\u7b97\u3002\u5982\uff1amap\u3001Filter\u3001groupby\u3001join\u7b49\u3002<\/li>\n
                                                                                                • \u884c\u52a8\u64cd\u4f5c<\/strong>\uff08Actions\uff09\uff1aActions\u64cd\u4f5c\u4f1a\u8fd4\u56de\u7ed3\u679c\u6216\u628aRDD\u6570\u636e\u5199\u5230\u5b58\u50a8\u7cfb\u7edf\u4e2d\u3002Actions\u662f\u89e6\u53d1Spark\u542f\u52a8\u8ba1\u5212\u7684\u52a8\u56e0\u3002\u5982\uff1acount\u3001collect\u3001save\u7b49\u3002<\/li>\n<\/ul>\n

                                                                                                  \uff085\uff09RDD\u7684\u5bbd\u4f9d\u8d56\u4e0e\u7a84\u4f9d\u8d56\u7684\u6982\u5ff5\u4ee5\u53ca\u5404\u81ea\u7684\u65b9\u6cd5\uff1f<\/strong><\/p>\n

                                                                                                    \n
                                                                                                  • \u5bbd\u4f9d\u8d56<\/strong>\uff1a\u591a\u4e2a\u5b50RDD\u4e2d\u7684\u5206\u533a\u6570\u636e\u6765\u6e90\u4e8e\u540c\u4e00\u4e2a\u7236RDD\u6570\u636e\u5206\u533a\u3002\u4f8b\u5982groupByKey\u3001reduceByKey\u3001sortByKey\u7b49\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u5bbd\u4f9d\u8d56\u3002<\/li>\n
                                                                                                  • \u7a84\u4f9d\u8d56<\/strong>\uff1a\u7236RDD\u7684\u6bcf\u4e2a\u5206\u533a\u6700\u591a\u53ea\u88ab\u4e00\u4e2a\u5b50RDD\u7684\u4e00\u4e2a\u5206\u533a\u4f7f\u7528\u3002\u4f8b\u5982map\u3001filter\u3001union\u7b49\u64cd\u4f5c\u90fd\u4f1a\u4ea7\u751f\u7a84\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n

                                                                                                    \uff085\uff09RDD\u7684\u521b\u5efa\u65b9\u5f0f\uff1f<\/strong><\/p>\n

                                                                                                      \n
                                                                                                    • \u901a\u8fc7\u5e76\u884c\u96c6\u5408\uff08\u6570\u7ec4\uff09\u521b\u5efaRDD\uff1a\u53ef\u4ee5\u8c03\u7528 SparkContext \u7684 parallelize\u65b9\u6cd5\uff0c\u5728Driver\u4e2d\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u96c6\u5408\uff08\u6570\u7ec4\uff09\u4e0a\u521b\u5efa\u3002<\/li>\n
                                                                                                    • \u4ece\u4e00\u4e2a\u7a33\u5b9a\u7684\u5b58\u50a8\u7cfb\u7edf\u4e2d\u521b\u5efa\uff1aSpark\u91c7\u7528textFile()\u65b9\u6cd5\u6765\u4ece\u6587\u4ef6\u7cfb\u7edf\u4e2d\u52a0\u8f7d\u6570\u636e\u521b\u5efaRDD\uff0c\u8be5\u65b9\u6cd5\u628a\u6587\u4ef6\u7684URI\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u4e2aURI\u53ef\u4ee5\u662f\uff1a\n
                                                                                                        \n
                                                                                                      • \u672c\u5730\u6587\u4ef6\u7cfb\u7edf\u7684\u5730\u5740<\/li>\n
                                                                                                      • \u6216\u8005\u662f\u5206\u5e03\u5f0f\u6587\u4ef6\u7cfb\u7edfHDFS\u7684\u5730\u5740<\/li>\n
                                                                                                      • \u6216\u8005\u662fAmazon S3\u7684\u5730\u5740\u7b49\u7b49<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n

                                                                                                        2\u3001RDD\u7684\u8f6c\u6362\u64cd\u4f5c<\/h3>\n

                                                                                                        RDD\u7684\u8f6c\u6362\u548c\u884c\u52a8\u64cd\u4f5c\u90fd\u9700\u8981\u521b\u5efa SparkContext \u6267\u884c\u4e0a\u4e0b\u6587\uff0c\u4e0b\u9762\u793a\u4f8b\u4e2d\u7684 sc \u6765\u81ea\u8fd9\u91cc<\/strong>\uff1a<\/p>\n

                                                                                                        val<\/span> conf =<\/span> new<\/span> SparkConf(<\/span>)<\/span>.<\/span>setMaster(<\/span>\"local\"<\/span>)<\/span>.<\/span>setAppName(<\/span>\"appName\"<\/span>)<\/span> var<\/span> sc =<\/span> new<\/span> SparkContext(<\/span>conf)<\/span> <\/code><\/pre>\n
                                                                                                          \n
                                                                                                        • map()\uff1a\u53c2\u6570\u662f\u51fd\u6570\uff0c\u51fd\u6570\u5e94\u7528\u4e8eRDD\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u8fd4\u56de\u503c\u662f\u65b0\u7684RDD<\/li>\n<\/ul>\n
                                                                                                          val<\/span> rdd_arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>\"b\"<\/span>,<\/span> \"a\"<\/span>,<\/span> \"c\"<\/span>)<\/span>)<\/span> val<\/span> rdd_map =<\/span> rdd_arr.<\/span>map(<\/span>x =><\/span> (<\/span>x,<\/span>1<\/span>)<\/span>)<\/span> \/\/rdd.collect(): \u5c06RDD\u7c7b\u578b\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u6570\u7ec4<\/span> println(<\/span>rdd_arr.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/ b, a, c<\/span> println(<\/span>rdd_map.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/(b,1), (a,1), (c,1)<\/span> <\/code><\/pre>\n
                                                                                                            \n
                                                                                                          • flatMap()\uff1a\u53c2\u6570\u662f\u51fd\u6570\uff0c\u51fd\u6570\u5e94\u7528\u4e8eRDD\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5c06\u5143\u7d20\u6570\u636e\u8fdb\u884c\u62c6\u5206\uff0c\u53d8\u6210\u8fed\u4ee3\u5668\uff0c\u8fd4\u56de\u503c\u662f\u65b0\u7684RDD<\/li>\n<\/ul>\n
                                                                                                            \/\/\u4f7f\u7528flatMap\u5bf9\u591a\u4e2a\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u518d\u6241\u5e73\u5316<\/span> val<\/span> data =<\/span> sc.<\/span>parallelize(<\/span>List(<\/span>\"I am learning Spark\"<\/span>,<\/span> \"I like Spark\"<\/span>)<\/span>)<\/span> val<\/span> res =<\/span> data.<\/span>flatMap(<\/span>x =><\/span> x.<\/span>split(<\/span>\" \"<\/span>)<\/span>)<\/span> println(<\/span>res.<\/span>collect.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/I, am, learning, Spark, I, like, Spark<\/span> \/\/\u5bf9\u4e00\u4e2a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6269\u5145<\/span> val<\/span> arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>)<\/span>)<\/span> val<\/span> newArr =<\/span> arr.<\/span>flatMap(<\/span>n =><\/span> Array(<\/span>n,<\/span> n*<\/span>100<\/span>,<\/span> 42<\/span>)<\/span>)<\/span> println(<\/span>arr.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/1, 2, 3<\/span> println(<\/span>newArr.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/1, 100, 42, 2, 200, 42, 3, 300, 42<\/span> <\/code><\/pre>\n
                                                                                                              \n
                                                                                                            • filter()\uff1a\u53c2\u6570\u662f\u51fd\u6570\uff0c\u51fd\u6570\u4f1a\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u503c\u662f\u65b0\u7684RDD<\/li>\n<\/ul>\n
                                                                                                              val<\/span> rdd_filter =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>)<\/span>)<\/span>.<\/span>filter(<\/span>n =><\/span> n><\/span>2<\/span>)<\/span> println(<\/span>rdd_filter.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/3<\/span> <\/code><\/pre>\n
                                                                                                                \n
                                                                                                              • distinct()\uff1a\u6ca1\u6709\u53c2\u6570\uff0c\u5c06RDD\u91cc\u7684\u5143\u7d20\u8fdb\u884c\u53bb\u91cd\u64cd\u4f5c<\/li>\n<\/ul>\n
                                                                                                                val<\/span> arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>3<\/span>,<\/span>4<\/span>)<\/span>)<\/span> val<\/span> newArr =<\/span> arr.<\/span>distinct(<\/span>)<\/span> println(<\/span>newArr.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/4, 1, 3, 2<\/span> <\/code><\/pre>\n
                                                                                                                  \n
                                                                                                                • union()\uff1a\u53c2\u6570\u662fRDD\uff0c\u751f\u6210\u5305\u542b\u4e24\u4e2aRDD\u6240\u6709\u5143\u7d20\u7684\u65b0RDD<\/li>\n
                                                                                                                • intersection()\uff1a\u53c2\u6570\u662fRDD\uff0c\u6c42\u51fa\u4e24\u4e2aRDD\u7684\u5171\u540c\u5143\u7d20<\/li>\n
                                                                                                                • subtract()\uff1a\u53c2\u6570\u662fRDD\uff0c\u5c06\u539fRDD\u91cc\u548c\u53c2\u6570RDD\u91cc\u76f8\u540c\u7684\u5143\u7d20\u53bb\u6389<\/li>\n
                                                                                                                • cartesian()\uff1a\u53c2\u6570\u662fRDD\uff0c\u6c42\u4e24\u4e2aRDD\u7684\u7b1b\u5361\u513f\u79ef<\/li>\n
                                                                                                                • groupBy()\uff1a\u505a\u4e00\u4e2a\u5206\u7ec4\u3002<\/li>\n<\/ul>\n
                                                                                                                  val<\/span> arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>\"John\"<\/span>,<\/span> \"Fred\"<\/span>,<\/span> \"Anna\"<\/span>,<\/span> \"James\"<\/span>)<\/span>)<\/span> val<\/span> newArr =<\/span> arr.<\/span>groupBy(<\/span>w =><\/span> w.<\/span>charAt(<\/span>0<\/span>)<\/span>)<\/span> println(<\/span>newArr.<\/span>collect(<\/span>)<\/span>.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/(A,CompactBuffer(Anna)), (J,CompactBuffer(John, James)), (F,CompactBuffer(Fred))<\/span> <\/code><\/pre>\n
                                                                                                                    \n
                                                                                                                  • coalesce\uff1a\u6536\u7f29\u5408\u5e76\u5206\u533a\uff0c\u51cf\u5c11\u5206\u533a\u7684\u4e2a\u6570<\/li>\n
                                                                                                                  • reduceByKey\u548cgroupByKey<\/li>\n<\/ul>\n
                                                                                                                    val<\/span> words =<\/span> Array(<\/span>\"one\"<\/span>,<\/span> \"two\"<\/span>,<\/span> \"two\"<\/span>,<\/span> \"three\"<\/span>,<\/span> \"three\"<\/span>,<\/span> \"three\"<\/span>)<\/span> val<\/span> wordRDD =<\/span> sc.<\/span>parallelize(<\/span>words)<\/span>.<\/span>map(<\/span>word =><\/span> (<\/span>word,<\/span> 1<\/span>)<\/span>)<\/span> val<\/span> wordCountsWithReduce =<\/span> wordRDD .<\/span>reduceByKey(<\/span>_ +<\/span> _)<\/span> .<\/span>collect(<\/span>)<\/span> val<\/span> wordCountsWithGroup =<\/span> wordRDD .<\/span>groupByKey(<\/span>)<\/span> .<\/span>map(<\/span>t =><\/span> (<\/span>t.<\/span>_1,<\/span> t.<\/span>_2.<\/span>sum)<\/span>)<\/span> .<\/span>collect(<\/span>)<\/span> println(<\/span>wordCountsWithReduce.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/(two,2), (one,1), (three,3)<\/span> println(<\/span>wordCountsWithGroup.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/(two,2), (one,1), (three,3)<\/span> <\/code><\/pre>\n

                                                                                                                    reduceByKey\u548cgroupByKey\u533a\u522b\uff1f<\/strong>
                                                                                                                    \u200b reduceByKey\u7528\u4e8e\u5bf9\u6bcf\u4e2akey\u5bf9\u5e94\u7684\u591a\u4e2avalue\u8fdb\u884cmerge\u64cd\u4f5c\uff0c\u6700\u91cd\u8981\u7684\u662f\u5b83\u80fd\u591f\u5728\u672c\u5730\u5148\u8fdb\u884cmerge\u64cd\u4f5c\uff0c\u5e76\u4e14merge\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u81ea\u5b9a\u4e49\uff0c\u5f53\u91c7\u7528reduceByKey\u65f6\uff0cSpark\u53ef\u4ee5\u5728\u6bcf\u4e2a\u5206\u533a\u79fb\u52a8\u6570\u636e\u4e4b\u524d\u5c06\u5f85\u8f93\u51fa\u6570\u636e\u4e0e\u4e00\u4e2a\u5171\u7528\u7684key\u7ed3\u5408<\/p>\n

                                                                                                                    \u200b groupByKey\u4e5f\u662f\u5bf9\u6bcf\u4e2akey\u8fdb\u884c\u64cd\u4f5c\uff0c\u4f46\u53ea\u751f\u6210\u4e00\u4e2asequence\uff0cgroupByKey\u672c\u8eab\u4e0d\u80fd\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u9700\u8981\u5148\u7528groupByKey\u751f\u6210RDD\uff0c\u7136\u540e\u624d\u80fd\u5bf9\u6b64RDD\u901a\u8fc7map\u8fdb\u884c\u81ea\u5b9a\u4e49\u51fd\u6570\u64cd\u4f5c\uff0c\u5f53\u91c7\u7528groupByKey\u65f6\uff0c\u7531\u4e8e\u5b83\u4e0d\u63a5\u6536\u51fd\u6570\uff0cSpark\u53ea\u80fd\u5148\u5c06\u6240\u6709\u7684\u952e\u503c\u5bf9(key-value pair)\u90fd\u79fb\u52a8\uff0c\u8fd9\u6837\u7684\u540e\u679c\u662f\u96c6\u7fa4\u8282\u70b9\u4e4b\u95f4\u7684\u5f00\u9500\u5f88\u5927\uff0c\u5bfc\u81f4\u4f20\u8f93\u5ef6\u65f6\u3002<\/p>\n

                                                                                                                    3\u3001RDD\u7684\u884c\u52a8\u64cd\u4f5c<\/h3>\n
                                                                                                                      \n
                                                                                                                    • collect()\uff1a \u8fd4\u56deRDD\u6240\u6709\u5143\u7d20 \uff0c\u5c06RDD\u7c7b\u578b\u8f6c\u6362\u4e3a\u6570\u7ec4\u7c7b\u578b<\/li>\n
                                                                                                                    • count()\uff1aRDD\u91cc\u5143\u7d20\u4e2a\u6570<\/li>\n
                                                                                                                    • countByValue()\uff1a\u5404\u5143\u7d20\u5728RDD\u4e2d\u51fa\u73b0\u6b21\u6570<\/li>\n
                                                                                                                    • countByKey()\uff1a\u5404\u952e\u5728RDD\u4e2d\u51fa\u73b0\u7684\u6b21\u6570<\/li>\n<\/ul>\n
                                                                                                                      val<\/span> arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>(<\/span>'J'<\/span>,<\/span>\"James\"<\/span>)<\/span>,<\/span>(<\/span>'F'<\/span>,<\/span>\"Fred\"<\/span>)<\/span>,<\/span>(<\/span>'A'<\/span>,<\/span>\"Anna\"<\/span>)<\/span>,<\/span>(<\/span>'J'<\/span>,<\/span>\"John\"<\/span>)<\/span>)<\/span>)<\/span> val<\/span> keyCount =<\/span> arr.<\/span>countByKey(<\/span>)<\/span> println(<\/span>keyCount)<\/span> \/\/Map(A -> 1, J -> 2, F -> 1)<\/span> <\/code><\/pre>\n
                                                                                                                        \n
                                                                                                                      • reduce()\uff1a\u5e76\u884c\u6574\u5408\u6240\u6709RDD\u6570\u636e\uff0c\u4f8b\u5982\u6c42\u548c\u64cd\u4f5c<\/li>\n<\/ul>\n
                                                                                                                        val<\/span> arr =<\/span> sc.<\/span>parallelize(<\/span>Array(<\/span>1<\/span>,<\/span>2<\/span>,<\/span>3<\/span>,<\/span>4<\/span>)<\/span>)<\/span> val<\/span> res =<\/span> arr.<\/span>reduce(<\/span>(<\/span>a,<\/span>b)<\/span> =><\/span> a+<\/span>b)<\/span> println(<\/span>arr.<\/span>collect.<\/span>mkString(<\/span>\", \"<\/span>)<\/span>)<\/span> \/\/1, 2, 3, 4<\/span> println(<\/span>res)<\/span> \/\/10<\/span> <\/code><\/pre>\n
                                                                                                                          \n
                                                                                                                        • fold(0)(func)\uff1a\u548creduce\u529f\u80fd\u4e00\u6837\uff0c\u4e0d\u8fc7fold\u5e26\u6709\u521d\u59cb\u503c<\/li>\n
                                                                                                                        • aggregate(0)(seqOp,combop)\uff1a\u548creduce\u529f\u80fd\u4e00\u6837\uff0c\u4f46\u662f\u8fd4\u56de\u7684RDD\u6570\u636e\u7c7b\u578b\u548c\u539fRDD\u4e0d\u4e00\u6837<\/li>\n
                                                                                                                        • foreach(func)\uff1a\u5bf9RDD\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4f7f\u7528\u7279\u5b9a\u51fd\u6570<\/li>\n
                                                                                                                        • saveAsTextFile(pathname)\uff1a\u4fdd\u5b58\u6587\u4ef6<\/li>\n<\/ul>\n
                                                                                                                          rdd.<\/span>saveAsTextFile(<\/span>\"file:\/\/\/f:\/output-test.txt\"<\/span>)<\/span> <\/code><\/pre>\n

                                                                                                                          4\u3001RDD\u5206\u533a<\/h3>\n

                                                                                                                          \uff081\uff09RDD\u5206\u533a\u5b9a\u4e49<\/strong>\uff1aRDD\u662f\u5f39\u6027\u5206\u5e03\u5f0f\u6570\u636e\u96c6\uff0c\u901a\u5e38RDD\u5f88\u5927\uff0c\u4f1a\u88ab\u5206\u6210\u5f88\u591a\u4e2a\u5206\u533a\uff0c\u5206\u522b\u4fdd\u5b58\u5728\u4e0d\u540c\u7684\u8282\u70b9\u4e0a\u3002<\/p>\n

                                                                                                                          \uff082\uff09\u4f7f\u7528RDD\u5206\u533a\u7684\u4f18\u70b9\uff1a<\/strong><\/p>\n

                                                                                                                            \n
                                                                                                                          • \u589e\u52a0\u5e76\u884c\u5ea6<\/li>\n
                                                                                                                          • \u51cf\u5c11\u901a\u4fe1\u5f00\u9500<\/li>\n<\/ul>\n

                                                                                                                            \uff083\uff09\u80fd\u4ecespark\u5206\u533a\u4e2d\u83b7\u53d6\u7684\u64cd\u4f5c\u6709<\/strong>\uff1acogroup()<\/code>\u3001groupWith()<\/code>\u3001join()<\/code>\u3001leftOuterJoin()<\/code>\u3001rightOuterJoin()<\/code>\u3001groupByKey()<\/code>\u3001reduceByKey()<\/code>\u3001combineByKey()<\/code>\u4ee5\u53calookup()<\/code>\u3002<\/p>\n

                                                                                                                            \uff084\uff09RDD\u5206\u533a\u539f\u5219<\/strong>\uff1a\u4f7f\u5f97\u5206\u533a\u7684\u4e2a\u6570\u5c3d\u91cf\u7b49\u4e8e\u96c6\u7fa4\u4e2d\u7684CPU\u6838\u5fc3\uff08core\uff09\u6570\u76ee<\/p>\n

                                                                                                                            \uff085\uff09\u5982\u4f55\u624b\u52a8\u8bbe\u7f6e\u5206\u533a<\/strong>\uff1a<\/p>\n

                                                                                                                              \n
                                                                                                                            • \u521b\u5efa RDD \u65f6\uff1a\u5728\u8c03\u7528 textFile \u548c parallelize \u65b9\u6cd5\u65f6\u5019\u624b\u52a8\u6307\u5b9a\u5206\u533a\u4e2a\u6570\u5373\u53ef\uff0c\u8bed\u6cd5\u683c\u5f0f\uff1asc.textFile(path, partitionNum)<\/li>\n
                                                                                                                            • \u901a\u8fc7\u8f6c\u6362\u64cd\u4f5c\u5f97\u5230\u65b0 RDD \u65f6\uff1a\u76f4\u63a5\u8c03\u7528 repartition \u65b9\u6cd5\u5373\u53ef<\/li>\n<\/ul>\n

                                                                                                                              \uff086\uff09RDD\u60f0\u6027\u673a\u5236<\/strong>\uff1a\u6267\u884c\u201c\u52a8\u4f5c\u201d\u7c7b\u578b\u64cd\u4f5c\u65f6\uff0c\u624d\u662f\u771f\u6b63\u7684\u8ba1\u7b97\u3002<\/p>\n

                                                                                                                              \uff087\uff09RDD\u7684\u6301\u4e45\u5316<\/strong>\uff1a\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u91cd\u590d\u8ba1\u7b97\u7684\u5f00\u9500\uff0c\u53ef\u4ee5\u4f7f\u7528persist()\u65b9\u6cd5\u5bf9\u4e00\u4e2aRDD\u6807\u8bb0\u4e3a\u6301\u4e45\u5316\u3002<\/p>\n

                                                                                                                              5\u3001\u952e\u503c\u5bf9RDD<\/h3>\n

                                                                                                                              \uff081\uff09\u6982\u5ff5<\/strong>\uff1a\u952e\u503c\u5bf9RDD\u7531\u4e00\u7ec4\u7ec4\u7684\u952e\u503c\u5bf9\u7ec4\u6210\uff0c\u8fd9\u4e9bRDD\u88ab\u79f0\u4e3aPairRDD\u3002PairRDD\u63d0\u4f9b\u4e86\u5e76\u884c\u64cd\u4f5c\u5404\u4e2a\u952e\u6216\u8de8\u8282\u70b9\u91cd\u65b0\u8fdb\u884c\u6570\u636e\u5206\u7ec4\u7684\u64cd\u4f5c\u63a5\u53e3,\u867d\u7136\u5927\u90e8\u5206Spark\u7684RDD\u64cd\u4f5c\u90fd\u652f\u6301\u6240\u6709\u79cd\u7c7b\u7684\u5355\u503cRDD\uff0c\u4f46\u662f\u6709\u5c11\u90e8\u5206\u7279\u6b8a\u7684\u64cd\u4f5c\u53ea\u80fd\u4f5c\u7528\u4e8e\u952e\u503c\u5bf9\u7c7b\u578b\u7684RDD\u3002
                                                                                                                              \uff082\uff09\u521b\u5efa\u65b9\u5f0f<\/strong>\uff1a\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\uff0c\u901a\u8fc7\u5e76\u884c\u96c6\u5408\uff08\u6570\u7ec4\uff09\u521b\u5efaRDD<\/p>\n

                                                                                                                              \uff083\uff09\u952e\u503c\u5bf9\u5e38\u7528\u8f6c\u6362\u64cd\u4f5c\u7684\u65b9\u6cd5\u53ca\u5176\u4f5c\u7528\uff1a<\/strong><\/p>\n

                                                                                                                                \n
                                                                                                                              • reduceByKey(func):\u4f7f\u7528func\u51fd\u6570\u5408\u5e76\u5177\u6709\u76f8\u540c\u952e\u7684\u503c<\/li>\n
                                                                                                                              • groupByKey()\uff1a\u5bf9\u5177\u6709\u76f8\u540c\u952e\u7684\u503c\u8fdb\u884c\u5206\u7ec4<\/li>\n
                                                                                                                              • Keys()\uff1akeys\u53ea\u4f1a\u628aPair RDD\u4e2d\u7684key\u8fd4\u56de\u5f62\u6210\u4e00\u4e2a\u65b0\u7684RDD<\/li>\n
                                                                                                                              • Values()\uff1a\u53ea\u4f1a\u628aPair RDD\u4e2d\u7684value\u8fd4\u56de\u5f62\u6210\u4e00\u4e2a\u65b0\u7684RDD<\/li>\n
                                                                                                                              • sortByKey()\uff1a\u8fd4\u56de\u4e00\u4e2a\u6839\u636e\u952e\u6392\u5e8f\u7684RDD<\/li>\n
                                                                                                                              • mapValues(func)\uff1a\u5bf9\u952e\u503c\u5bf9RDD\u4e2d\u7684\u6bcf\u4e2avalue\u90fd\u5e94\u7528\u4e00\u4e2a\u51fd\u6570\uff0c\u4f46\u662f\uff0ckey\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316<\/li>\n
                                                                                                                              • join\uff1a\u5185\u8fde\u63a5<\/li>\n
                                                                                                                              • combineByKey\uff1a\u805a\u5408\u5404\u5206\u533a\u7684\u5143\u7d20\uff0c\u800c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e8c\u5143\u7ec4<\/li>\n<\/ul>\n

                                                                                                                                \u516d\u3001Scala\u7ec3\u4e60\u5b9e\u4f8b\u6c47\u603b\u548c\u77e5\u8bc6\u70b9\u601d\u7ef4\u5bfc\u56fe\uff08\u8003\u8bd5\u4e2d\u9700\u8981\u7075\u6d3b\u8fd0\u7528\u5404\u79cd\u65b9\u6cd5\u3001\u51fd\u6570\uff09<\/h2>\n

                                                                                                                                \u5185\u5bb9\u8be6\u89c1\u6211\u7684\u53e6\u4e00\u7bc7\u535a\u5ba2\uff0c\u4f20\u9001\u95e8\u3002<\/p>\n

                                                                                                                                \u4e03\u3001Spark\u96c6\u7fa4Win10\u672c\u5730\u5f00\u53d1\u73af\u5883\u642d\u5efa\uff08\u4e0d\u9700\u8981\u80cc\uff0c\u4e86\u89e3\u5373\u53ef\uff09<\/h2>\n

                                                                                                                                \u5185\u5bb9\u8be6\u89c1\u6211\u7684\u53e6\u4e00\u7bc7\u535a\u5ba2\uff0c\u4f20\u9001\u95e8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"spark\u9700\u8981scala\u4ec0\u4e48\u6c34\u5e73_\u8003\u524d\u4e00\u5468\u5982\u4f55\u51b2\u523a\u4e00\u3001Spark\u7b80\u4ecb1\u3001\u4ec0\u4e48\u662fSpark\uff1f\uff08\u95ee\u7b54\u5fc5\u8003\uff09Spark\u662f\u4e00\u4e2a\u5feb\u901f\u3001\u5206\u5e03\u5f0f\u3001\u53ef\u6269\u5c55\uff08\u968f\u65f6...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6708"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6708"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6708\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}