[PYTHON] Japanische Übersetzung des Apache Spark-Dokuments - Schnellstart

Die japanische Übersetzung der folgenden Seite. Verwenden wir alle Spark. http://spark.apache.org/docs/latest/quick-start.html

Die japanische Übersetzung des Spark-Umgebungsbauhandbuchs für AWS EC2 finden Sie ebenfalls unten. Schauen Sie also bitte mal rein.

Wenn Sie in der japanischen Übersetzung etwas Seltsames finden, teilen Sie uns dies bitte in den Kommentaren mit.

Quick Start

This tutorial provides a quick introduction to using Spark. We will first introduce the API through Spark’s interactive shell (in Python or Scala), then show how to write standalone applications in Java, Scala, and Python. See the programming guide for a more complete reference.

Dieses Tutorial ist eine kurze Einführung in die Verwendung von Spark. Wir werden die API zunächst über die Spark Interactive Shell (Python oder Scala) einführen. Dann zeige ich Ihnen, wie Sie eine eigenständige Anwendung in Java, Scala und Python schreiben. Weitere Informationen finden Sie im Programmierhandbuch.

http://spark.apache.org/docs/latest/programming-guide.html

To follow along with this guide, first download a packaged release of Spark from the Spark website. Since we won’t be using HDFS, you can download a package for any version of Hadoop.

Um diesen Leitfaden zu verstehen, holen Sie sich bitte das erste von Spark veröffentlichte Paket von unserer Website. HDFS wird nicht verwendet, daher ist jede Version von Hadoop ausreichend.

Interactive Analysis with the Spark Shell

Interaktive Analyse mit Spark Shell

Basics

Spark’s shell provides a simple way to learn the API, as well as a powerful tool to analyze data interactively. It is available in either Scala (which runs on the Java VM and is thus a good way to use existing Java libraries) or Python. Start it by running the following in the Spark directory:

Die Spark-Shell ist ein kollaboratives Tool, mit dem APIs leicht erlernt und Daten interaktiv analysiert werden können. Es kann mit Scala (das auf JVMs ausgeführt wird und am besten mit Java-Bibliotheken verwendet wird) oder Python verwendet werden. Starten Sie die Spark-Shell, indem Sie den folgenden Befehl im Spark-Verzeichnis ausführen.

Scala


./bin/spark-shell

Python


./bin/pyspark

Spark’s primary abstraction is a distributed collection of items called a Resilient Distributed Dataset (RDD). RDDs can be created from Hadoop InputFormats (such as HDFS files) or by transforming other RDDs. Let’s make a new RDD from the text of the README file in the Spark source directory:

Die wichtigste Abstraktion von Spark ist eine geteilte Sammlung, die vom Resilient Distributed Dataset (RDD) aufgerufen wird. RDDs können aus Hadoop InputFormas (wie HDF-Dateien) oder anderen transformierten RDDs erstellt werden. Erstellen wir eine neue RDD aus der README-Textdatei im Spark-Quellverzeichnis.

Scala


scala> val textFile = sc.textFile("README.md")
textFile: spark.RDD[String] = spark.MappedRDD@2ee9b6e3

Python


>>> textFile = sc.textFile("README.md")

RDDs have actions, which return values, and transformations, which return pointers to new RDDs. Let’s start with a few actions:

Die RDD enthält eine Aktion, die einen Wert oder eine Konvertierung eines Zeigers in eine neue RDD zurückgibt. Beginnen wir einige Aktionen.

Scala


scala> textFile.count() //Anzahl der Elemente (Anzahl der Zeilen) in dieser RDD
res0: Long = 126

scala> textFile.first() //Erster Punkt der RDD (erste Zeile)
res1: String = # Apache Spark

Python


>>> textFile.count() # Number of items in this RDD
126

>>> textFile.first() # First item in this RDD
u'# Apache Spark'

Now let’s use a transformation. We will use the filter transformation to return a new RDD with a subset of the items in the file.

Verwenden wir nun die Transformation. Wir verwenden eine Filtertransformation, die eine neue RDD als Teilmenge der aus der Datei erstellten RDD zurückgibt.

Scala


scala> val linesWithSpark = textFile.filter(line => line.contains("Spark"))
linesWithSpark: spark.RDD[String] = spark.FilteredRDD@7dd4af09

Python


>>> linesWithSpark = textFile.filter(lambda line: "Spark" in line)

We can chain together transformations and actions:

Sie können auch Transformationen und Aktionen verketten.

Scala


scala> textFile.filter(line => line.contains("Spark")).count() // How many lines contain "Spark"?
res3: Long = 15

Python


>>> textFile.filter(lambda line: "Spark" in line).count() # How many lines contain "Spark"?
15

More on RDD Operations Erfahren Sie mehr über RDD-Vorgänge.

RDD actions and transformations can be used for more complex computations. Let’s say we want to find the line with the most words:

RDD-Aktionen und -Transformationen können für komplexere Berechnungen verwendet werden.

scala


scala> textFile.map(line => line.split(" ").size).reduce((a, b) => if (a > b) a else b)
res4: Long = 15

This first maps a line to an integer value, creating a new RDD. reduce is called on that RDD to find the largest line count. The arguments to map and reduce are Scala function literals (closures), and can use any language feature or Scala/Java library. For example, we can easily call functions declared elsewhere. We’ll use Math.max() function to make this code easier to understand:

Zuerst erstellen wir eine neue RDD mit einer Zuordnung von Zeilen zu ganzzahligen Werten. Reduzieren wird auf dieser RDD aufgerufen, um die größte Zeile zu finden. Die Zuordnungs- und Reduzierungsargumente sind die Schließfunktionen von Scala, und jede Scala- und Java-Bibliothek kann verwendet werden. Sie können beispielsweise einfach eine Funktion aufrufen, die an einer beliebigen Stelle deklariert ist. Es wäre hilfreich, die Funktion Math.max () in diesem Code zu verwenden.

scala


scala> import java.lang.Math
import java.lang.Math

scala> textFile.map(line => line.split(" ").size).reduce((a, b) => Math.max(a, b))
res5: Int = 15

One common data flow pattern is MapReduce, as popularized by Hadoop. Spark can implement MapReduce flows easily:

Eines der gängigen Datenflussmuster ist MapReduce, das in Hadoop berühmt wurde. Mit Spark können Sie problemlos einen MapReduce-Flow implementieren.

Scala


scala> val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
wordCounts: spark.RDD[(String, Int)] = spark.ShuffledAggregatedRDD@71f027b8

Here, we combined the flatMap, map and reduceByKey transformations to compute the per-word counts in the file as an RDD of (String, Int) pairs. To collect the word counts in our shell, we can use the collect action:

Um die Anzahl der Wörter in der Datei zu zählen, wird flatMap kombiniert und map- und redeceByKey-Konvertierung durchgeführt, um eine RDD des String- und Int-Paares zu generieren. Mit der Aktion "Sammeln" können Sie die Anzahl der Wörter in der Shell erfassen.

Scala


scala> wordCounts.collect()
res6: Array[(String, Int)] = Array((means,1), (under,2), (this,3), (Because,1), (Python,2), (agree,1), (cluster.,1), ...)

Caching

Spark also supports pulling data sets into a cluster-wide in-memory cache. This is very useful when data is accessed repeatedly, such as when querying a small “hot” dataset or when running an iterative algorithm like PageRank. As a simple example, let’s mark our linesWithSpark dataset to be cached:

Spark kann Datasets aus einem clusterweiten In-Memory-Cache abrufen. Dies ist sehr nützlich, wenn Sie wiederholt auf Daten zugreifen, kleine heiße Datensätze abfragen und interaktive Algorithmen wie den Seitenrang verwenden.

Markieren wir als einfaches Beispiel das linesWithSpark-Dataset als Cache.

Scala


scala> linesWithSpark.cache()
res7: spark.RDD[String] = spark.FilteredRDD@17e51082

scala> linesWithSpark.count()
res8: Long = 15

scala> linesWithSpark.count()
res9: Long = 15

It may seem silly to use Spark to explore and cache a 100-line text file. The interesting part is that these same functions can be used on very large data sets, even when they are striped across tens or hundreds of nodes. You can also do this interactively by connecting bin/spark-shell to a cluster, as described in the programming guide.

Es mag lächerlich erscheinen, mit Spark eine Textdatei mit 100 Zeilen zwischenzuspeichern. Das Interessante ist, dass diese Funktionen auch in großen Datenmengen mit Dutzenden oder Hunderten von gestreiften Knoten verwendet werden können. Sie können mit bin / spark-shell eine Verbindung zu Ihrem Cluster herstellen und diese interaktiv ausführen, wie im Programmierhandbuch beschrieben.

Standalone-Anwendungen

Scala

Now say we wanted to write a standalone application using the Spark API. We will walk through a simple application in both Scala (with SBT), Java (with Maven), and Python.

Lassen Sie uns nun eine Möglichkeit eröffnen, eine eigenständige Anwendung zu schreiben, die die Spark-API verwendet. Schauen wir uns eine einfache Anwendung in Scala (mit SBT), Java (mit Maven) und Python an.

We’ll create a very simple Spark application in Scala. So simple, in fact, that it’s named SimpleApp.scala:

Lassen Sie uns eine sehr einfache Spark-Anwendung erstellen. Ich habe es SimpleApp.scala genannt, weil es so einfach und inhaltlich ist.

SimpleApp.scala


/* SimpleApp.scala */
import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object SimpleApp {
  def main(args: Array[String]) {
    val logFile = "YOUR_SPARK_HOME/README.md" //Pfad auf Ihrer Maschine
    val conf = new SparkConf().setAppName("Simple Application")
    val sc = new SparkContext(conf)
    val logData = sc.textFile(logFile, 2).cache()
    val numAs = logData.filter(line => line.contains("a")).count()
    val numBs = logData.filter(line => line.contains("b")).count()
    println("Lines with a: %s, Lines with b: %s".format(numAs, numBs))
  }
}

This program just counts the number of lines containing ‘a’ and the number containing ‘b’ in the Spark README. Note that you’ll need to replace YOUR_SPARK_HOME with the location where Spark is installed. Unlike the earlier examples with the Spark shell, which initializes its own SparkContext, we initialize a SparkContext as part of the program.

Dieses Programm zählt nur die Anzahl der Zeilen, die a und b in der README-Datei von Spark enthalten. Ändern Sie YOUR_SPARK_HOME in den Pfad des Ordners, in dem Sie Spark installiert haben. Im Gegensatz zum vorherigen Beispiel mit der Spark-Shell initialisieren wir hier unseren eigenen SparkContext, der als Teil des Programms initialisiert wird.

We pass the SparkContext constructor a SparkConf object which contains information about our application.

Ich übergebe ein SparkConf-Objekt mit Anwendungsinformationen an den SparkContext-Konstruktor.

Our application depends on the Spark API, so we’ll also include an sbt configuration file, simple.sbt which explains that Spark is a dependency. This file also adds a repository that Spark depends on:

Da unsere Anwendung auf der Spark-API basiert, enthält sie auch die sbt-Konfigurationsdatei. Diese Datei fügt auch die Repositorys hinzu, von denen Spark abhängt.

python


name := "Simple Project"

version := "1.0"

scalaVersion := "2.10.4"

libraryDependencies += "org.apache.spark" %% "spark-core" % "1.0.0"

resolvers += "Akka Repository" at "http://repo.akka.io/releases/"

For sbt to work correctly, we’ll need to layout SimpleApp.scala and simple.sbt according to the typical directory structure. Once that is in place, we can create a JAR package containing the application’s code, then use the spark-submit script to run our program.

Damit sbt ordnungsgemäß funktioniert, legen Sie SympleApp.scala und simple.sbt mit einer allgemeinen Verzeichnisstruktur an. Nach der Bereitstellung können Sie ein JAR-Paket mit Ihrem Anwendungscode generieren und das Programm mit dem Spark-Submit-Skript ausführen.

# Your directory layout should look like this
$ find .
.
./simple.sbt
./src
./src/main
./src/main/scala
./src/main/scala/SimpleApp.scala

# Package a jar containing your application
$ sbt package
...
[info] Packaging {..}/{..}/target/scala-2.10/simple-project_2.10-1.0.jar

# Use spark-submit to run your application
$ YOUR_SPARK_HOME/bin/spark-submit \
  --class "SimpleApp" \
  --master local[4] \
  target/scala-2.10/simple-project_2.10-1.0.jar
...
Lines with a: 46, Lines with b: 23

Java

This example will use Maven to compile an application jar, but any similar build system will work. We’ll create a very simple Spark application, SimpleApp.java:

In diesem Beispiel wird Maven zum Kompilieren der Anwendungs-JAR verwendet, funktioniert jedoch auf einem ähnlichen Build-System. Lassen Sie uns eine sehr einfache Spark-Anwendung namens SimpleApp.java erstellen.

SimpleApp.java


/* SimpleApp.java */
import org.apache.spark.api.java.*;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.Function;

public class SimpleApp {
  public static void main(String[] args) {
    String logFile = "YOUR_SPARK_HOME/README.md"; // Should be some file on your system
    SparkConf conf = new SparkConf().setAppName("Simple Application");
    JavaSparkContext sc = new JavaSparkContext(conf);
    JavaRDD<String> logData = sc.textFile(logFile).cache();

    long numAs = logData.filter(new Function<String, Boolean>() {
      public Boolean call(String s) { return s.contains("a"); }
    }).count();

    long numBs = logData.filter(new Function<String, Boolean>() {
      public Boolean call(String s) { return s.contains("b"); }
    }).count();

    System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs);
  }
}

This program just counts the number of lines containing ‘a’ and the number containing ‘b’ in a text file. Note that you’ll need to replace YOUR_SPARK_HOME with the location where Spark is installed.

Dieses Programm zählt nur die Anzahl der Zeilen in einer Textdatei, die a und b enthalten. Ersetzen Sie YOUR_SPARK_HOME durch den Pfad Ihres Spark-Installationsordners.

As with the Scala example, we initialize a SparkContext, though we use the special JavaSparkContext class to get a Java-friendly one. We also create RDDs (represented by JavaRDD) and run transformations on them. Finally, we pass functions to Spark by creating classes that extend spark.api.java.function.Function. The Spark programming guide describes these differences in more detail.

Wie im Scala-Beispiel müssen Sie den SparkContext initialisieren. Verwenden Sie jedoch als besonderen Hinweis die JavaSparkContext-Klasse, um einen SparkContext zu erhalten, der besser mit Java kompatibel ist. In ähnlicher Weise wird Java RDD ersetzt, um RDD zu generieren, und es wird eine Konvertierung durchgeführt. Erweitern Sie schließlich spark.api.java.funtction.Function, um eine Klasse zu erstellen und die Funktion an Spark zu übergeben. Das Spark-Programmierhandbuch beschreibt diesen Unterschied.

http://spark.apache.org/docs/latest/programming-guide.html

To build the program, we also write a Maven pom.xml file that lists Spark as a dependency. Note that Spark artifacts are tagged with a Scala version.

Um dieses Programm zu erstellen, beschreiben Sie die Abhängigkeiten von Spark in Mavens Datei pom.xml wie folgt. Bitte beachten Sie, dass Spark-Artefakte mit der Scala-Version gekennzeichnet sind.

pom.xml


<project>
  <groupId>edu.berkeley</groupId>
  <artifactId>simple-project</artifactId>
  <modelVersion>4.0.0</modelVersion>
  <name>Simple Project</name>
  <packaging>jar</packaging>
  <version>1.0</version>
  <repositories>
    <repository>
      <id>Akka repository</id>
      <url>http://repo.akka.io/releases</url>
    </repository>
  </repositories>
  <dependencies>
    <dependency> <!-- Spark dependency -->
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.10</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>
</project>

We layout these files according to the canonical Maven directory structure:

Ordnen Sie diese Dateien in einer Verzeichnisstruktur an, die der Methode von Maven entspricht.

$ find .
./pom.xml
./src
./src/main
./src/main/java
./src/main/java/SimpleApp.java

Now, we can package the application using Maven and execute it with ./bin/spark-submit.

Sie können dann Maven verwenden, um Ihre Anwendung als Spark-Anwendung zu verpacken, die Sie mit dem Skript ./bin/spark-submit ausführen können.

# Package a jar containing your application
$ mvn package
...
[INFO] Building jar: {..}/{..}/target/simple-project-1.0.jar

# Use spark-submit to run your application
$ YOUR_SPARK_HOME/bin/spark-submit \
  --class "SimpleApp" \
  --master local[4] \
  target/simple-project-1.0.jar
...
Lines with a: 46, Lines with b: 23

Python

Now we will show how to write a standalone application using the Python API (PySpark). As an example, we’ll create a simple Spark application, SimpleApp.py:

Nun wollen wir sehen, wie eine eigenständige Anwendung mit der Python-API (PySpark) geschrieben wird. Erstellen Sie beispielsweise eine einfache Spark-Anwendung mit dem Namen SimpleApp.py.

SimpleApp.py


"""SimpleApp.py"""
from pyspark import SparkContext

logFile = "YOUR_SPARK_HOME/README.md"  # Should be some file on your system
sc = SparkContext("local", "Simple App")
logData = sc.textFile(logFile).cache()

numAs = logData.filter(lambda s: 'a' in s).count()
numBs = logData.filter(lambda s: 'b' in s).count()

print "Lines with a: %i, lines with b: %i" % (numAs, numBs)

This program just counts the number of lines containing ‘a’ and the number containing ‘b’ in a text file. Note that you’ll need to replace YOUR_SPARK_HOME with the location where Spark is installed.

Dieses Programm (unten abgekürzt)

As with the Scala and Java examples, we use a SparkContext to create RDDs. We can pass Python functions to Spark, which are automatically serialized along with any variables that they reference.

Verwenden Sie SparkContext, um RDDs wie in den Scala- und Java-Beispielen zu generieren. Sie können Python-Funktionen direkt an Spark übergeben und sie werden automatisch mit Argumentwerten und Referenzen serialisiert.

For applications that use custom classes or third-party libraries, we can also add code dependencies to spark-submit through its --py-files argument by packaging them into a .zip file (see spark-submit --help for details). SimpleApp is simple enough that we do not need to specify any code dependencies.

Für Anwendungen, die benutzerdefinierte Klassen oder Bibliotheken von Drittanbietern verwenden, können Sie die Abhängigkeit übergeben, um eine Zip-Datei mit der Option --py-files zu senden. (Weitere Informationen finden Sie unter --help in spark-submit) Wenn Sie sich SimpleApp ansehen, sehen Sie, dass Sie keinen speziellen Abhängigkeitscode schreiben müssen.

We can run this application using the bin/spark-submit script:

Sie können das Skript bin / spark-submit verwenden, um diese Anwendung zu starten.

# Use spark-submit to run your application
$ YOUR_SPARK_HOME/bin/spark-submit \
  --master local[4] \
  SimpleApp.py
...
Lines with a: 46, Lines with b: 23

Where to Go from Here

Congratulations on running your first Spark application! For an in-depth overview of the API, start with the Spark programming guide, or see “Programming Guides” menu for other components. For running applications on a cluster, head to the deployment overview. Finally, Spark includes several samples in the examples directory (Scala, Java, Python). You can run them as follows:

Herzlichen Glückwunsch zum Start Ihrer ersten Spark-Anwendung.

# For Scala and Java, use run-example:
./bin/run-example SparkPi

# For Python examples, use spark-submit directly:
./bin/spark-submit examples/src/main/python/pi.py

Dies ist das Ende des Schnellstarts.

Recommended Posts

Japanische Übersetzung des Apache Spark-Dokuments - Schnellstart
Japanische Übersetzung des Apache Spark-Dokuments - Einreichen von Anträgen
Japanische Übersetzung des Apache Spark-Dokuments - Übersicht über den Cluster-Modus
Python-Schnellstart
sosreport Japanische Übersetzung
Mann systemd japanische Übersetzung
stromlinienförmige Erklärung japanische Übersetzung
Streamlit Tutorial Japanische Übersetzung
Apache Spark Starter Kits
man systemd.service Japanische Übersetzung
man nftables Japanische Übersetzung
Dockerfile Reference Japanische Übersetzung
docker-compose --help japanische Übersetzung
Docker helfen japanische Übersetzung
Pandas Benutzerhandbuch "Multi-Index / Advanced Index" (offizielles Dokument Japanische Übersetzung)
Pandas Benutzerhandbuch "Manipulieren fehlender Daten" (offizielles Dokument Japanische Übersetzung)