pom.xml文件配置以下: java
<?xml version="1.0"?> <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.example</groupId> <artifactId>hou</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hou</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spark.version>2.2.0</spark.version> <scala.version>2.11</scala.version> </properties> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_${scala.version}</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_${scala.version}</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-catalyst_2.11</artifactId> <version>${spark.version}</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>14.0.1</version> </dependency> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.11.8</version> </dependency> </dependencies> <build> <plugins> <plugin> <inherited>true</inherited> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- scala and java mix compilation --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project>
示例程序以下:sql
package com.example.hou import org.apache.spark.sql.SparkSession import org.apache.spark.SparkConf object TestApp { def main(args: Array[String]): Unit = { val spark = SparkSession.builder() .appName("TextApp") .master("local[2]") .getOrCreate() //只要寫到包名就能夠了...sql.text,不用這樣寫...sql.text.DefaultSource // val df = spark.read.format("com.example.hou") // .option("path","C://code//data.txt").load() //val df = spark.sqlContext.read.format("com.example.hou").load("C://code//data.txt") val df = spark.read.textFile("C://code//data.txt") df.createOrReplaceTempView("test") spark.sql("select * from test").show() println("Application Ended...") spark.stop() } }