Apache Spark๋กœ Cloud Storage ์ปค๋„ฅํ„ฐ ์‚ฌ์šฉ


์ด ํŠœํ† ๋ฆฌ์–ผ์€ Apache Spark์—์„œ Cloud Storage ์ปค๋„ฅํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

๋ชฉํ‘œ

Java, Scala, Python์—์„œ ๊ฐ„๋‹จํ•œ WordCount Spark ์ž‘์—…์„ ์ž‘์„ฑํ•œ ๋‹ค์Œ Dataproc ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ์ž‘์—…์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋น„์šฉ

์ด ๋ฌธ์„œ์—์„œ๋Š” ๋น„์šฉ์ด ์ฒญ๊ตฌ๋  ์ˆ˜ ์žˆ๋Š” Google Cloud๊ตฌ์„ฑ์š”์†Œ( )๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • Compute Engine
  • Dataproc
  • Cloud Storage

ํ”„๋กœ์ ํŠธ ์‚ฌ์šฉ๋Ÿ‰์„ ๊ธฐ์ค€์œผ๋กœ ์˜ˆ์ƒ ๋น„์šฉ์„ ์‚ฐ์ถœํ•˜๋ ค๋ฉด ๊ฐ€๊ฒฉ ๊ณ„์‚ฐ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

Google Cloud ์‹ ๊ทœ ์‚ฌ์šฉ์ž๋Š” ๋ฌด๋ฃŒ ์ฒดํ—˜ํŒ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

์•„๋ž˜ ๋‹จ๊ณ„์— ๋”ฐ๋ผ ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์ฝ”๋“œ ์‹คํ–‰์„ ์ค€๋น„ํ•˜์„ธ์š”.

  1. ํ”„๋กœ์ ํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๋กœ์ปฌ ๋จธ์‹ ์— Dataproc, Compute Engine ๋ฐ Cloud Storage API๊ฐ€ ์‚ฌ์šฉ ์„ค์ •๋˜๊ณ  Google Cloud CLI๊ฐ€ ์„ค์น˜๋œ ์ƒํƒœ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

    1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
    2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    3. Verify that billing is enabled for your Google Cloud project.

    4. Enable the Dataproc, Compute Engine, and Cloud Storage APIs.

      Enable the APIs

    5. Create a service account:

      1. In the Google Cloud console, go to the Create service account page.

        Go to Create service account
      2. Select your project.
      3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

        In the Service account description field, enter a description. For example, Service account for quickstart.

      4. Click Create and continue.
      5. Grant the Project > Owner role to the service account.

        To grant the role, find the Select a role list, then select Project > Owner.

      6. Click Continue.
      7. Click Done to finish creating the service account.

        Do not close your browser window. You will use it in the next step.

    6. Create a service account key:

      1. In the Google Cloud console, click the email address for the service account that you created.
      2. Click Keys.
      3. Click Add key, and then click Create new key.
      4. Click Create. A JSON key file is downloaded to your computer.
      5. Click Close.
    7. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

    8. Install the Google Cloud CLI.

    9. ์™ธ๋ถ€ ID ๊ณต๊ธ‰์—…์ฒด(IdP)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋จผ์ € ์ œํœด ID๋กœ gcloud CLI์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    10. gcloud CLI๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

      gcloud init
    11. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    12. Verify that billing is enabled for your Google Cloud project.

    13. Enable the Dataproc, Compute Engine, and Cloud Storage APIs.

      Enable the APIs

    14. Create a service account:

      1. In the Google Cloud console, go to the Create service account page.

        Go to Create service account
      2. Select your project.
      3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

        In the Service account description field, enter a description. For example, Service account for quickstart.

      4. Click Create and continue.
      5. Grant the Project > Owner role to the service account.

        To grant the role, find the Select a role list, then select Project > Owner.

      6. Click Continue.
      7. Click Done to finish creating the service account.

        Do not close your browser window. You will use it in the next step.

    15. Create a service account key:

      1. In the Google Cloud console, click the email address for the service account that you created.
      2. Click Keys.
      3. Click Add key, and then click Create new key.
      4. Click Create. A JSON key file is downloaded to your computer.
      5. Click Close.
    16. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.

    17. Install the Google Cloud CLI.

    18. ์™ธ๋ถ€ ID ๊ณต๊ธ‰์—…์ฒด(IdP)๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ ๋จผ์ € ์ œํœด ID๋กœ gcloud CLI์— ๋กœ๊ทธ์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    19. gcloud CLI๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

      gcloud init
    20. Cloud Storage ๋ฒ„ํ‚ท์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํŠœํ† ๋ฆฌ์–ผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋ ค๋ฉด Cloud Storage๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•  ์ค€๋น„๊ฐ€ ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธ์—์„œ ์ƒˆ ๋ฒ„ํ‚ท์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

      1. In the Google Cloud console, go to the Cloud Storage Buckets page.

        Go to Buckets

      2. Click Create.
      3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
        1. In the Get started section, do the following:
          • Enter a globally unique name that meets the bucket naming requirements.
          • To add a bucket label, expand the Labels section (), click Add label, and specify a key and a value for your label.
        2. In the Choose where to store your data section, do the following:
          1. Select a Location type.
          2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
          3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

            Set up cross-bucket replication

            1. In the Bucket menu, select a bucket.
            2. In the Replication settings section, click Configure to configure settings for the replication job.

              The Configure cross-bucket replication pane appears.

              • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
              • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
              • Click Done.
        3. In the Choose how to store your data section, do the following:
          1. Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
          2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
        4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
        5. In the Choose how to protect object data section, do the following:
          • Select any of the options under Data protection that you want to set for your bucket.
            • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
            • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
            • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
              • To enable Object Retention Lock, click the Enable object retention checkbox.
              • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
          • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
      4. Click Create.

    21. ๋กœ์ปฌ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ปฌ ๋จธ์‹ ์— ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด ํŠœํ† ๋ฆฌ์–ผ์— ์‚ฌ์šฉํ•  Cloud Storage ๋ฒ„ํ‚ท์˜ ์ด๋ฆ„๊ณผ Google Cloud ํ”„๋กœ์ ํŠธ ID๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๊ธฐ์กด ๋˜๋Š” ์ƒˆ Dataproc ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ด๋ฆ„๊ณผ ๋ฆฌ์ „์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ ์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์‚ฌ์šฉํ•  ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      PROJECT=project-id
      
      BUCKET_NAME=bucket-name
      
      CLUSTER=cluster-name
      
      REGION=cluster-region Example: "us-central1"
      

    22. Dataproc ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ง€์ •๋œ Compute Engine ์˜์—ญ์—์„œ ๋‹จ์ผ ๋…ธ๋“œ Dataproc ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

      gcloud dataproc clusters create ${CLUSTER} \
          --project=${PROJECT} \
          --region=${REGION} \
          --single-node
      

    23. Cloud Storage ๋ฒ„ํ‚ท์— ๊ณต๊ฐœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๊ณต๊ฐœ ๋ฐ์ดํ„ฐ Shakespeare ํ…์ŠคํŠธ ์Šค๋‹ˆํŽซ์„ Cloud Storage ๋ฒ„ํ‚ท์˜ input ํด๋”์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.

      gcloud storage cp gs://pub/shakespeare/rose.txt \
          gs://${BUCKET_NAME}/input/rose.txt
      

    24. ์ž๋ฐ”(Apache Maven), Scala(SBT), Python ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

    25. Spark WordCount ์ž‘์—… ์ค€๋น„

      ์•„๋ž˜์˜ ํƒญ์„ ์„ ํƒํ•˜์—ฌ ๋‹จ๊ณ„์— ๋”ฐ๋ผ ํด๋Ÿฌ์Šคํ„ฐ์— ์ œ์ถœํ•  ์ž‘์—… ํŒจํ‚ค์ง€ ๋˜๋Š” ํŒŒ์ผ์„ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ ์ž‘์—… ์œ ํ˜• ์ค‘ ํ•˜๋‚˜๋ฅผ ์ค€๋น„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      ์ž๋ฐ”

      1. pom.xml ํŒŒ์ผ์„ ๋กœ์ปฌ ๋จธ์‹ ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ pom.xml ํŒŒ์ผ์€ Scala ๋ฐ Spark ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…์† ํ•ญ๋ชฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” Dataproc ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ๋Ÿฐํƒ€์ž„ ์‹œ ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•จ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด provided ๋ฒ”์œ„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ปค๋„ฅํ„ฐ๊ฐ€ ํ‘œ์ค€ HDFS ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ pom.xml ํŒŒ์ผ์€ Cloud Storage ์ข…์† ํ•ญ๋ชฉ์„ ์ง€์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Spark ์ž‘์—…์ด Cloud Storage ํด๋Ÿฌ์Šคํ„ฐ ํŒŒ์ผ(gs://๋กœ ์‹œ์ž‘ํ•˜๋Š” URI๊ฐ€ ์žˆ๋Š” ํŒŒ์ผ)์— ์•ก์„ธ์Šคํ•  ๋•Œ, ์‹œ์Šคํ…œ์ด ์ž๋™์œผ๋กœ Cloud Storage ์ปค๋„ฅํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Cloud Storage์˜ ํŒŒ์ผ์— ์•ก์„ธ์Šคํ•ฉ๋‹ˆ๋‹ค.
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
        
          <groupId>dataproc.codelab</groupId>
          <artifactId>word-count</artifactId>
          <version>1.0</version>
        
          <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
          </properties>
        
          <dependencies>
            <dependency>
              <groupId>org.scala-lang</groupId>
              <artifactId>scala-library</artifactId>
              <version>Scala version, for example, 2.11.8</version>
              <scope>provided</scope>
            </dependency>
            <dependency>
              <groupId>org.apache.spark</groupId>
              <artifactId>spark-core_Scala major.minor.version, for example, 2.11</artifactId>
              <version>Spark version, for example, 2.3.1</version>
              <scope>provided</scope>
            </dependency>
          </dependencies>
        </project>
      2. ์•„๋ž˜ ๋‚˜์—ด๋œ WordCount.java ์ฝ”๋“œ๋ฅผ ๋กœ์ปฌ ๋จธ์‹ ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
        1. src/main/java/dataproc/codelab ๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ง‘ํ•ฉ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
          mkdir -p src/main/java/dataproc/codelab
          
        2. WordCount.java๋ฅผ ๋กœ์ปฌ ๋จธ์‹ ์˜ src/main/java/dataproc/codelab์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค.
          cp WordCount.java src/main/java/dataproc/codelab
          

        WordCount.java๋Š” Cloud Storage์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„ ํ…์ŠคํŠธ ํŒŒ์ผ ๊ฒฐ๊ณผ๋ฅผ Cloud Storage์— ์“ฐ๋Š” Java์˜ Spark ์ž‘์—…์ž…๋‹ˆ๋‹ค.

        package dataproc.codelab;
        
        import java.util.Arrays;
        import org.apache.spark.SparkConf;
        import org.apache.spark.api.java.JavaPairRDD;
        import org.apache.spark.api.java.JavaRDD;
        import org.apache.spark.api.java.JavaSparkContext;
        import scala.Tuple2;
        
        public class WordCount {
          public static void main(String[] args) {
            if (args.length != 2) {
              throw new IllegalArgumentException("Exactly 2 arguments are required: <inputUri> <outputUri>");
            }
            String inputPath = args[0];
            String outputPath = args[1];
            JavaSparkContext sparkContext = new JavaSparkContext(new SparkConf().setAppName("Word Count"));
            JavaRDD<String> lines = sparkContext.textFile(inputPath);
            JavaRDD<String> words = lines.flatMap(
                (String line) -> Arrays.asList(line.split(" ")).iterator()
            );
            JavaPairRDD<String, Integer> wordCounts = words.mapToPair(
                (String word) -> new Tuple2<>(word, 1)
            ).reduceByKey(
                (Integer count1, Integer count2) -> count1 + count2
            );
            wordCounts.saveAsTextFile(outputPath);
          }
        }
      3. ํŒจํ‚ค์ง€๋ฅผ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.
        mvn clean package
        
        ์„ฑ๊ณต์ ์œผ๋กœ ๋นŒ๋“œ๋˜๋ฉด target/word-count-1.0.jar์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
      4. ํŒจํ‚ค์ง€๋ฅผ Cloud Storage๋กœ ์Šคํ…Œ์ด์ง•ํ•ฉ๋‹ˆ๋‹ค.
        gcloud storage cp target/word-count-1.0.jar \
            gs://${BUCKET_NAME}/java/word-count-1.0.jar
        

      Scala

      1. build.sbt ํŒŒ์ผ์„ ๋กœ์ปฌ ๋จธ์‹ ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์Œ build.sbt ํŒŒ์ผ์€ Scala ๋ฐ Spark ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ข…์† ํ•ญ๋ชฉ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” Dataproc ํด๋Ÿฌ์Šคํ„ฐ๊ฐ€ ๋Ÿฐํƒ€์ž„ ์‹œ ์ด๋Ÿฌํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ œ๊ณตํ•จ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด provided ๋ฒ”์œ„๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ปค๋„ฅํ„ฐ๊ฐ€ ํ‘œ์ค€ HDFS ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฏ€๋กœ build.sbt ํŒŒ์ผ์€ Cloud Storage ์ข…์† ํ•ญ๋ชฉ์„ ์ง€์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Spark ์ž‘์—…์ด Cloud Storage ํด๋Ÿฌ์Šคํ„ฐ ํŒŒ์ผ(gs://๋กœ ์‹œ์ž‘ํ•˜๋Š” URI๊ฐ€ ์žˆ๋Š” ํŒŒ์ผ)์— ์•ก์„ธ์Šคํ•  ๋•Œ, ์‹œ์Šคํ…œ์ด ์ž๋™์œผ๋กœ Cloud Storage ์ปค๋„ฅํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Cloud Storage์˜ ํŒŒ์ผ์— ์•ก์„ธ์Šคํ•ฉ๋‹ˆ๋‹ค.
        scalaVersion := "Scala version, for example, 2.11.8"
        
        name := "word-count"
        organization := "dataproc.codelab"
        version := "1.0"
        
        libraryDependencies ++= Seq(
          "org.scala-lang" % "scala-library" % scalaVersion.value % "provided",
          "org.apache.spark" %% "spark-core" % "Spark version, for example, 2.3.1" % "provided"
        )
      2. word-count.scala๋ฅผ ๋กœ์ปฌ ๋จธ์‹ ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” Cloud Storage์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„ ํ…์ŠคํŠธ ํŒŒ์ผ ๊ฒฐ๊ณผ๋ฅผ Cloud Storage์— ์“ฐ๋Š” Java์˜ Spark ์ž‘์—…์ž…๋‹ˆ๋‹ค.
        package dataproc.codelab
        
        import org.apache.spark.SparkContext
        import org.apache.spark.SparkConf
        
        object WordCount {
          def main(args: Array[String]) {
            if (args.length != 2) {
              throw new IllegalArgumentException(
                  "Exactly 2 arguments are required: <inputPath> <outputPath>")
            }
        
            val inputPath = args(0)
            val outputPath = args(1)
        
            val sc = new SparkContext(new SparkConf().setAppName("Word Count"))
            val lines = sc.textFile(inputPath)
            val words = lines.flatMap(line => line.split(" "))
            val wordCounts = words.map(word => (word, 1)).reduceByKey(_ + _)
            wordCounts.saveAsTextFile(outputPath)
          }
        }
      3. ํŒจํ‚ค์ง€๋ฅผ ๋นŒ๋“œํ•ฉ๋‹ˆ๋‹ค.
        sbt clean package
        
        ์„ฑ๊ณต์ ์œผ๋กœ ๋นŒ๋“œ๋˜๋ฉด target/scala-2.11/word-count_2.11-1.0.jar์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
      4. ํŒจํ‚ค์ง€๋ฅผ Cloud Storage๋กœ ์Šคํ…Œ์ด์ง•ํ•ฉ๋‹ˆ๋‹ค.
        gcloud storage cp target/scala-2.11/word-count_2.11-1.0.jar \
            gs://${BUCKET_NAME}/scala/word-count_2.11-1.0.jar
        

      Python

      1. word-count.py๋ฅผ ๋กœ์ปฌ ๋จธ์‹ ์— ๋ณต์‚ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” PySpark๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Cloud Storage์—์„œ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ์ฝ๊ณ  ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„ ํ…์ŠคํŠธ ํŒŒ์ผ ๊ฒฐ๊ณผ๋ฅผ Cloud Storage์— ์“ฐ๋Š” Python์˜ Spark ์ž‘์—…์ž…๋‹ˆ๋‹ค.
        #!/usr/bin/env python
        
        import pyspark
        import sys
        
        if len(sys.argv) != 3:
          raise Exception("Exactly 2 arguments are required: <inputUri> <outputUri>")
        
        inputUri=sys.argv[1]
        outputUri=sys.argv[2]
        
        sc = pyspark.SparkContext()
        lines = sc.textFile(sys.argv[1])
        words = lines.flatMap(lambda line: line.split())
        wordCounts = words.map(lambda word: (word, 1)).reduceByKey(lambda count1, count2: count1 + count2)
        wordCounts.saveAsTextFile(sys.argv[2])

      ์ž‘์—… ์ œ์ถœ

      ๋‹ค์Œ gcloud ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ WordCount ์ž‘์—…์„ Dataproc ํด๋Ÿฌ์Šคํ„ฐ์— ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค.

      ์ž๋ฐ”

      gcloud dataproc jobs submit spark \
          --cluster=${CLUSTER} \
          --class=dataproc.codelab.WordCount \
          --jars=gs://${BUCKET_NAME}/java/word-count-1.0.jar \
          --region=${REGION} \
          -- gs://${BUCKET_NAME}/input/ gs://${BUCKET_NAME}/output/
      

      Scala

      gcloud dataproc jobs submit spark \
          --cluster=${CLUSTER} \
          --class=dataproc.codelab.WordCount \
          --jars=gs://${BUCKET_NAME}/scala/word-count_2.11-1.0.jar \
          --region=${REGION} \
          -- gs://${BUCKET_NAME}/input/ gs://${BUCKET_NAME}/output/
      

      Python

      gcloud dataproc jobs submit pyspark word-count.py \
          --cluster=${CLUSTER} \
          --region=${REGION} \
          -- gs://${BUCKET_NAME}/input/ gs://${BUCKET_NAME}/output/
      

      ์ถœ๋ ฅ ๋ณด๊ธฐ

      ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋ฉด ๋‹ค์Œ gcloud CLI ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜์—ฌ WordCount ์ถœ๋ ฅ์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

      gcloud storage cat gs://${BUCKET_NAME}/output/*
      

      WordCount ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๋น„์Šทํ•˜๊ฒŒ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

      (a,2)
      (call,1)
      (What's,1)
      (sweet.,1)
      (we,1)
      (as,1)
      (name?,1)
      (any,1)
      (other,1)
      (rose,1)
      (smell,1)
      (name,1)
      (would,1)
      (in,1)
      (which,1)
      (That,1)
      (By,1)
      

      ์‚ญ์ œ

      ํŠœํ† ๋ฆฌ์–ผ์„ ์™„๋ฃŒํ•œ ํ›„์—๋Š” ๋งŒ๋“  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜์—ฌ ํ• ๋‹น๋Ÿ‰ ์‚ฌ์šฉ์„ ์ค‘์ง€ํ•˜๊ณ  ์š”๊ธˆ์ด ์ฒญ๊ตฌ๋˜์ง€ ์•Š๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์„น์…˜์€ ์ด๋Ÿฌํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ์‚ฌ์šฉ ์ค‘์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

      ํ”„๋กœ์ ํŠธ ์‚ญ์ œ

      ๋น„์šฉ์ด ์ฒญ๊ตฌ๋˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ํŠœํ† ๋ฆฌ์–ผ์—์„œ ๋งŒ๋“  ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

      ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

      1. In the Google Cloud console, go to the Manage resources page.

        Go to Manage resources

      2. In the project list, select the project that you want to delete, and then click Delete.
      3. In the dialog, type the project ID, and then click Shut down to delete the project.

      Dataproc ํด๋Ÿฌ์Šคํ„ฐ ์‚ญ์ œ

      ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋Œ€์‹  ํ”„๋กœ์ ํŠธ ๋‚ด์˜ ํด๋Ÿฌ์Šคํ„ฐ๋งŒ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

      Cloud Storage ๋ฒ„ํ‚ท ์‚ญ์ œ

    26. In the Google Cloud console, go to the Cloud Storage Buckets page.

      Go to Buckets

    27. Click the checkbox for the bucket that you want to delete.
    28. To delete the bucket, click Delete, and then follow the instructions.
    29. ๋‹ค์Œ ๋‹จ๊ณ„