This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from activeviam/databricks-cluster-vector
Databricks cluster vector
- Loading branch information
Showing
19 changed files
with
1,154 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
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
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,36 @@ | ||
package io.atoti.spark; | ||
|
||
import io.atoti.spark.aggregation.SumArray; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import io.atoti.spark.operation.Quantile; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
import org.apache.spark.sql.Dataset; | ||
import org.apache.spark.sql.Row; | ||
import org.apache.spark.sql.SparkSession; | ||
|
||
public class Main { | ||
|
||
static Dotenv dotenv = Dotenv.load(); | ||
static SparkSession spark = | ||
SparkSession.builder() | ||
.appName("Spark Atoti") | ||
.config("spark.master", "local") | ||
.config("spark.databricks.service.clusterId", dotenv.get("clusterId")) | ||
.getOrCreate(); | ||
|
||
public static void main(String[] args) { | ||
spark.sparkContext().addJar("./target/spark-lib-0.0.1-SNAPSHOT.jar"); | ||
final Dataset<Row> dataframe = spark.read().table("array"); | ||
SumArray price_simulations = new SumArray( | ||
"price_simulations_sum", "price_simulations" | ||
); | ||
Quantile quantile = new Quantile("quantile", price_simulations, 95f); | ||
List<Row> rows = AggregateQuery.aggregate( | ||
dataframe, Arrays.asList("id"), Arrays.asList(price_simulations), Arrays.asList(quantile)) | ||
.collectAsList(); | ||
System.out.println(rows); | ||
} | ||
} |
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,83 @@ | ||
package io.atoti.spark; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.PriorityQueue; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import scala.collection.JavaConverters; | ||
import scala.collection.Seq; | ||
|
||
class ArrayElement { | ||
int index; | ||
long value; | ||
|
||
public ArrayElement(int index, long value) { | ||
this.index = index; | ||
this.value = value; | ||
} | ||
} | ||
|
||
public class Utils { | ||
public static <T> ArrayList<T> convertScalaArrayToArray(Seq<T> arr) { | ||
return new ArrayList<T>(JavaConverters.asJavaCollectionConverter(arr).asJavaCollection()); | ||
} | ||
|
||
public static Seq<Long> convertToArrayListToScalaArraySeq(List<Long> arr) { | ||
return JavaConverters.asScalaBuffer(arr).iterator().toSeq(); | ||
} | ||
|
||
public static long t(ArrayElement a, ArrayElement b) { | ||
return b.value - a.value; | ||
} | ||
|
||
public static PriorityQueue<ArrayElement> constructMaxHeap(ArrayList<Long> arr) { | ||
PriorityQueue<ArrayElement> pq = | ||
new PriorityQueue<ArrayElement>( | ||
(ArrayElement a, ArrayElement b) -> Long.compare(a.value, b.value)); | ||
pq.addAll( | ||
IntStream.range(0, arr.size()) | ||
.mapToObj((int k) -> new ArrayElement(k, arr.get(k))) | ||
.collect(Collectors.toList())); | ||
return pq; | ||
} | ||
|
||
public static long quantile(ArrayList<Long> arr, float percent) { | ||
PriorityQueue<ArrayElement> pq = constructMaxHeap(arr); | ||
int index = (int) Math.floor(arr.size() * (100 - percent) / 100); | ||
|
||
for (int i = arr.size() - 1; i > index; i--) { | ||
pq.poll(); | ||
} | ||
|
||
return pq.poll().value; | ||
} | ||
|
||
public static int quantileIndex(ArrayList<Long> arr, float percent) { | ||
PriorityQueue<ArrayElement> pq = constructMaxHeap(arr); | ||
int index = (int) Math.floor(arr.size() * (100 - percent) / 100); | ||
|
||
for (int i = arr.size() - 1; i > index; i--) { | ||
pq.poll(); | ||
} | ||
|
||
return pq.poll().index; | ||
} | ||
|
||
public static int findKthLargestElement(ArrayList<Integer> arr, int k) { | ||
if (k < arr.size()) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
} | ||
|
||
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(Comparator.reverseOrder()); | ||
|
||
pq.addAll(arr); | ||
|
||
for (int i = 0; i < k; i++) { | ||
pq.poll(); | ||
} | ||
|
||
return pq.peek(); | ||
} | ||
} |
Oops, something went wrong.