Skip to content

Instantly share code, notes, and snippets.

View techisbeautiful's full-sized avatar

techisbeautiful

View GitHub Profile
PCollection<String> resultCollection = productCollection.apply(ParDo
.of(new DoFn<String, String>() {
@ProcessElement
public void process(ProcessContext processContext) {
double average = processContext.sideInput(averagePrice);
String strings = processContext.element();
assert strings!= null;
String[] splits = strings.split(",");
double price = Double.parseDouble(splits[3].trim());
if (price >= average) {
Schema productTypeSchema = Schema.of(Schema.Field.of("ProductTypeId", Schema.FieldType.INT32),
Schema.Field.of("ProductType", Schema.FieldType.STRING)
);
PCollection<String> productCollection =
pipeline.apply(TextIO.read().from("/data/section1/products.csv"))
.apply("FilterHeader", Filter.by(line ->
!line.isEmpty() && !line.contains("ProductId, ProductName, ProductTypeId, Price"))
);
PCollection<KV<Integer, String>> productTypeCollection =
pipeline.apply(TextIO.read().from(productTypes))
.apply("FilterHeader", Filter.by(line ->
!line.isEmpty() && !line.contains("ProductTypeId, ProductType")))
int linearSearch(int dataCollection[], int element) {
for (int index = 0; index < dataCollection.length; index++) {
if (dataCollection[index] == element)
return index;
}
return -1;
}
int binarySearch(int dataCollection[], int element) {
int left = 0, right = dataCollection.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
// Verify if element is present at mid
if (dataCollection[mid] == element)
return mid;
// If element greater, ignore left half
class MyGraph
{
private int vertices;
private LinkedList<Integer> adjacency[];
MyGraph(int v) {
vertices = v;
adjacency = new LinkedList[v];
for (int i=0; i<v; ++i)
adjacency[i] = new LinkedList();
class MyGraph {
private int vertices;
private LinkedList<Integer> adjacency[];
MyGraph(int v) {
vertices = v;
adjacency = new LinkedList[v];
for (int i = 0; i < v; ++i)
adjacency[i] = new LinkedList();
}
void bubbleSort(int dataCollection[])
{
int n = dataCollection.length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (dataCollection[j] > dataCollection[j + 1]) {
// swap arr[j+1] and arr[j]
int temp = dataCollection[j];
dataCollection[j] = dataCollection[j + 1];
dataCollection[j + 1] = temp;
static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int partition(int[] arr, int low, int high) {
// Pick pivot
int pivot = arr[high];
void merge(int dataCollection[], int left, int mid, int right) {
// Find size of two sub arrays to be merged
int n1 = mid - left + 1;
int n2 = right - mid;
int leftTempArray[] = new int[n1];
int rightTempArray[] = new int[n2];
// Set data to temp arrays
for (int i = 0; i < n1; ++i)