一、pom.xml
<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>Spark</groupId>
<artifactId>Spark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<id>compile-scala</id>
<phase>compile</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile-scala</id>
<phase>test-compile</phase>
<goals>
<goal>add-source</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>2.12.15</scalaVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<classpathScope>compile</classpathScope>
<mainClass>cn.spark.study.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、执行类,main方法
import org.apache.spark.SparkConf
import org.apache.spark.SparkContext
import scala.concurrent.duration.{ Duration, DurationInt }
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.collection.mutable.ListBuffer
import java.util.concurrent.Executors
import scala.concurrent.Await
import org.apache.spark.rdd.RDD
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.apache.hadoop.fs.Path
object Distcp {
def main(args: Array[String]): Unit = {
type OptionMap = Map[Symbol, Any]
if (args.length == 0) println("hhhhhh")
val arglist = args.toList
def nextOption(map : OptionMap, list: List[String]) : OptionMap = {
def isSwitch(s : String) = (s(0) == '-')
list match {
case Nil => map
case "-i" :: value => nextOption(map ++ Map('ignoreFailure -> 1), list.tail)
case "-m" :: value :: tail =>
nextOption(map ++ Map('maxconcurrency -> value.toInt), tail)
case string :: Nil => nextOption(map ++ Map('outfile -> string), list.tail)
case string :: tail => nextOption(map ++ Map('infile -> string), tail)
case option :: opt2 :: tail if isSwitch(opt2) =>
println("Unknown option "+option)
sys.exit(1)
}
}
val options = nextOption(Map(),arglist)
println(options)
val sourceFolder = String.valueOf(options(Symbol("infile"))) //"hdfs://localhost:9000/test"
val targetFolder = String.valueOf(options(Symbol("outfile")))//"hdfs://localhost:9000/target"
val concurrency = (options(Symbol("maxconcurrency"))).toString.toInt
val ignoreFailure = options(Symbol("ignoreFailure")).toString.toInt
val sparkConf = new SparkConf().setAppName("sparkbyexamples.com").setMaster("local[1]")
val sc = new SparkContext(sparkConf)
val sb = new StringBuffer();
var fileNames = new ListBuffer[String]()
val conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000")
traverseDir(conf, sourceFolder, fileNames);
fileNames.foreach(
fileName =>
try {
sc.textFile(fileName, concurrency).saveAsTextFile(fileName.replace(sourceFolder, targetFolder));
} catch {
case t: Throwable => t.printStackTrace()
if(ignoreFailure==0){
throw new Exception("failed to copy "+fileName)
}
})
}
def traverseDir(hdconf: Configuration, path: String, filePaths: ListBuffer[String]) {
val files = FileSystem.get(hdconf).listStatus(new Path(path))
files.foreach { fStatus =>
{
if (!fStatus.isDirectory) {
filePaths += fStatus.getPath.toString
} else if (fStatus.isDirectory) {
traverseDir(hdconf, fStatus.getPath.toString, filePaths)
}
}
}
}
}
三、生成jar包
四、运行
1、查看目录
hadoop fs -ls /home/student5/niezb
2、新建空目录bing
hadoop fs -mkdir -p /home/student5/niezb/bing
3、使用Distcp移动/home/student5/niezb下的所有文件和目录到/home/student5/niezb/bing下
spark-submit --class Distcp --master local[*] /home/student5/niezb/Spark-0.0.1-SNAPSHOT.jar -i -m 3 "/home/student5/niezb" "/home/student5/niezb/bing"
4、查看结果
嘻嘻嘻!