How to Sum Array of Ints in Java

Aggregating data in an array is a common programming task. This can easily be done in Java by initializing a variable to hold the summed value, looping over the elements in the array, and adding these values to the total. The sumArray method below is a good example of how to sum the values in

read more

Creating JSON with JSON.simple (Java)

JSON is a popular way to represent and transfer data. Creating JSON with JSON.simple (a Java library from Google) is very easy. JSON.simple also performs very well compared to other Java JSON libraries when parsing a variety of file sizes (see results of performance tests here). Below is a simple example of building a JSON

read more

Hadoop MapReduce Example – Aggregating Text Fields

Below is a simple Hadoop MapReduce example. This example is a little different than the standard “Word Count” example in that it takes (tab) delimited text, and counts the occurrences of values in a certain field. More details about the implementation are included below as well.

You can see above in the Map class

read more

How to Reverse a Linked List

A common algorithm question is “How to Reverse a Linked List”. Below the SimpleLinkedList (Java) class contains a simple example to follow. The class SimpleLinkedList contains one field, head, which is used to keep track of the head (first) node of the linked list.

The nested class Node is the class used to create

read more

Recursive Binary Search Example

Binary Search is a classic algorithm used to find an item in an ordered list/array of items. This list/array of items must be ordered for binary search to work. The basic idea of Binary Search is to: Take the midpoint between the smallest and largest elements. Determine if item being searched for is smaller or

read more

Iterative Binary Search Example

Binary Search is a classic algorithm used to find an item in an ordered list/array of items. This list/array of items must be ordered for binary search to work. The basic idea of Binary Search is to: Take the midpoint between the smallest and largest elements. Determine if item being searched for is smaller or

read more

How to Extract Nested JSON Data in Spark

JSON is a very common way to store data. But JSON can get messy and parsing it can get tricky. Here are a few examples of parsing nested data structures in JSON using Spark DataFrames (examples here done with Spark 1.6.0). Our sample.json file:

Assuming you already have a SQLContext object created, the examples

read more

How to Change Branch Name in Git (Local and Remote)

Changing a branch’s name in Git is a common task and fortunately very easy to do. Changing the name of a local branch: git -m newBranchName If you are not on the branch being renamed, you must specify this branch in the command: git -m oldBranchName newBranchName There is no direct way to rename a

read more

How to Read / Write JSON in Spark

Needing to read and write JSON data is a common big data task. Thankfully this is very easy to do in Spark using Spark SQL DataFrames. Spark SQL can automatically infer the schema of a JSON dataset, and use it to load data into a DataFrame object. A DataFrame’s schema is used when writing JSON

read more

How to Exclude Fields using Gson

There are a lot of great Java libraries for working with JSON. One of these is Google’s Gson, which has the goal of providing simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa. Often when serializing objects into JSON you will want to exclude certain object fields. There a couple of

read more