1. 환경설정

http://kysepark.blogspot.com/2016/03/intellij-spark.html


2. pom.xml 에 Spark dependency 추가


<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cyberx</groupId>
<artifactId>IntellijMavenProject</artifactId>
<version>1.0</version>
<name>${project.artifactId}</name>
<description>My wonderfull scala app</description>
<inceptionYear>2015</inceptionYear>
<licenses>
<license>
<name>My License</name>
<url>http://....</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.5</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>

<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.11</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.11</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<args>

</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>


3. scala object 생성 후 Edit configuration - program arguments에 local[*] 추가 

 


4. 기본 예제 작성 (New Scala class - kind 는 object)


import org.apache.spark.{SparkContext, SparkConf}

object wordCount {
def main(args: Array[String]) : Unit = {
val conf = new SparkConf().setAppName("wordCount").setMaster(args(0))
val sc = new SparkContext(conf)
val filePath = "/spark/README.txt"
val inputRDD = sc.textFile(filePath)
val matchTerm = "spark"

val numMatches = inputRDD.filter(line => line.contains(matchTerm)).count()
val lines = inputRDD.count()
val firstLine = inputRDD.first()

println("%s lines in %s contains %s".format(numMatches, filePath, matchTerm))
println("%s lines".format(lines))
println("%s is first line".format(firstLine))
System.exit(0)
}
}


5. 실행 결과 확인



 




'hadoop,yarn, Hive > Spark' 카테고리의 다른 글

Pair RDD기본  (0) 2018.07.10
Spark 기본  (0) 2018.07.10
spark + intellij +maven 환경설정 및 기본예제  (0) 2018.07.10

https://m.blog.naver.com/PostView.nhn?blogId=2feelus&logNo=220714398973&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F

'JAVA > JAVA8' 카테고리의 다른 글

CompletableFuture  (0) 2018.07.03
[kata - 6 kyu ] Find The Parity Outlier  (0) 2018.06.29
square 수 구하기 간단 알고리즘  (0) 2017.06.19
Java 8 java.util.function package tutorial  (0) 2017.06.16
java.util.function apply() method 소개  (0) 2017.06.16

- 조건

Integer 배열에서 odd number 1개이고, even number가 여러개이면 odd number 리턴

Integer 배열에서 even number 1개이고, odd number가 여러개이면 even number 리턴


1)

import java.util.Arrays;


public class FindOutlier{

    public static int find(int[] integers) {

        int sum = Arrays.stream(integers).limit(3).map(i -> Math.abs(i) % 2).sum();

        int mod = (sum == 0 || sum == 1) ? 1 : 0;

        return Arrays.stream(integers).parallel() // call parallel to get as much bang for the buck on a "large" array

                .filter(n -> Math.abs(n) % 2 == mod).findFirst().getAsInt();

    }

}


2)

import java.util.Arrays;

public class FindOutlier{

  static int find(int[] integers) {

    int[] array = Arrays.stream(integers).filter(i -> i % 2 == 0).toArray();  

    return array.length == 1 ? array[0] : Arrays.stream(integers).filter(i -> i % 2 != 0).findAny().getAsInt();

  }

}


3)

import java.util.Arrays;


public class FindOutlier{

  static int find(int[] integers){

  return (Arrays.stream(integers).filter(item -> item%2 == 0).count() == 1)?Arrays.stream(integers).filter(item -> item%2 == 0).sum():Arrays.stream(integers).filter(item -> item%2 != 0).sum();

}}

'JAVA > JAVA8' 카테고리의 다른 글

CompletableFuture  (0) 2018.07.03
[kata - 6 kyu ] Find The Parity Outlier  (0) 2018.06.29
square 수 구하기 간단 알고리즘  (0) 2017.06.19
Java 8 java.util.function package tutorial  (0) 2017.06.16
java.util.function apply() method 소개  (0) 2017.06.16

+ Recent posts