-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example that reads a local file, writes to a DFS path provided by th... #3347
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0c89558
Add example that reads a local file, writes to a DFS path provided by…
rnowling a931d70
Fix import order
rnowling 1b314f0
Add scaladoc
rnowling df59b65
Fix if statements
rnowling 94a4691
Fix space
rnowling 44415b9
Fix local wc style
rnowling b9edf12
Fix spark wc style
rnowling f74c160
Fix else statement style
rnowling 7d9a8df
Fix string style
rnowling 07c6132
Fix string style
rnowling b0ef9ea
Fix string style
rnowling af8ccb7
Don't use java.io.File since DFS may not be POSIX-compatible
rnowling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
examples/src/main/scala/org/apache/spark/examples/DFSReadWriteTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.examples | ||
|
||
import java.io.File | ||
import scala.io.Source._ | ||
import org.apache.spark.{SparkContext, SparkConf} | ||
import org.apache.spark.SparkContext._ | ||
|
||
/** | ||
* Simple test for reading and writing to a distributed | ||
* file system. This example does the following: | ||
* | ||
* 1. Reads local file | ||
* 2. Computes word count on local file | ||
* 3. Writes local file to a DFS | ||
* 4. Reads the file back from the DFS | ||
* 5. Computes word count on the file using Spark | ||
* 6. Compares the word count results | ||
*/ | ||
object DFSReadWriteTest { | ||
|
||
private var localFilePath: File = new File(".") | ||
private var dfsDirPath: String = "" | ||
|
||
private val NPARAMS = 2 | ||
|
||
private def readFile(filename: String): List[String] = { | ||
val lineIter: Iterator[String] = fromFile(filename).getLines() | ||
val lineList: List[String] = lineIter.toList | ||
lineList | ||
} | ||
|
||
private def printUsage() { | ||
val usage: String = "DFS Read-Write Test\n" + | ||
"\n" + | ||
"Usage: localFile dfsDir\n" + | ||
"\n" + | ||
"localFile - (string) local file to use in test\n" + | ||
"dfsDir - (string) DFS directory for read/write tests\n" | ||
|
||
println(usage) | ||
} | ||
|
||
private def parseArgs(args: Array[String]) { | ||
if(args.length != NPARAMS) { | ||
printUsage() | ||
System.exit(1) | ||
} | ||
|
||
var i = 0 | ||
|
||
localFilePath = new File(args(i)) | ||
if(!localFilePath.exists) { | ||
System.err.println("Given path (" + args(i) + ") does not exist.\n") | ||
printUsage() | ||
System.exit(1) | ||
} | ||
|
||
if(!localFilePath.isFile) { | ||
System.err.println("Given path (" + args(i) + ") is not a file.\n") | ||
printUsage() | ||
System.exit(1) | ||
} | ||
|
||
i += 1 | ||
dfsDirPath = args(i) | ||
} | ||
|
||
def runLocalWordCount(fileContents: List[String]): Int = { | ||
fileContents.flatMap(_.split(" ")) | ||
.flatMap(_.split("\t")) | ||
.filter(_.size > 0) | ||
.groupBy(w => w) | ||
.mapValues(_.size) | ||
.values | ||
.sum | ||
} | ||
|
||
def main(args: Array[String]) { | ||
parseArgs(args) | ||
|
||
println("Performing local word count") | ||
val fileContents = readFile(localFilePath.toString()) | ||
val localWordCount = runLocalWordCount(fileContents) | ||
|
||
println("Creating SparkConf") | ||
val conf = new SparkConf().setAppName("DFS Read Write Test") | ||
|
||
println("Creating SparkContext") | ||
val sc = new SparkContext(conf) | ||
|
||
println("Writing local file to DFS") | ||
val dfsFilename = dfsDirPath + "/dfs_read_write_test" | ||
val fileRDD = sc.parallelize(fileContents) | ||
fileRDD.saveAsTextFile(dfsFilename) | ||
|
||
println("Reading file from DFS and running Word Count") | ||
val readFileRDD = sc.textFile(dfsFilename) | ||
|
||
val dfsWordCount = readFileRDD | ||
.flatMap(_.split(" ")) | ||
.flatMap(_.split("\t")) | ||
.filter(_.size > 0) | ||
.map(w => (w, 1)) | ||
.countByKey() | ||
.values | ||
.sum | ||
|
||
sc.stop() | ||
|
||
if(localWordCount == dfsWordCount) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: space after |
||
println(s"Success! Local Word Count ($localWordCount) " + | ||
s"and DFS Word Count ($dfsWordCount) agree.") | ||
} else { | ||
println(s"Failure! Local Word Count ($localWordCount) " + | ||
s"and DFS Word Count ($dfsWordCount) disagree.") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style:
Also, you can use string interpolation since this is scala. It looks clearer:
|
||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a java doc on what this does? Also maybe it makes sense not call this
*Test