fbpx

BlobCity DB joins Hacktoberfest 2019

BlobCity DB is proud to join Hacktoberfest. We are here to help you contribute into our next generation database, with an opportunity to forever change the way enterprises store and process their data.

If you have never contributed to an open source project, but would like to contribute, then Hacktoberfest is the best time of the year to start your open source journey.

What is Hacktoberfest?

It is a month long celebration of open source, celebrated in the month of October. Hacking + October + Fest = Hacktoberfest.

During Hacktoberfest, open source project maintainers, guide first time contributors to start contributing in their respective repositories. Guidance includes offering understanding of an open source project, and helping with Github Pull Requests and helping abide by the best practices of the repository. In return, the contributor stands a chance to win some limited edition Hacktoberfest T-Shits from the organisers of the fest, which is DigitalOcean and Dev.

What is BlobCity DB

BlobCity DB is a blazing fast open source NoSQL database. It is commonly used as a Data Lake solution. We call it a database as it is more of a database than a Data Lake, but can pretty much do anything a Data Lake does. BlobCity DB specialises in storing 17 formats of data. It is ACID compliant, supports SQL, offers Java & Scala based stored procedures and allows storing of data on disk and in-memory.

How to Participate

First register on Hacktoberfest, choose an open source project, choose an open issue from within the open source project, solve the issue and submit your code in the form of a pull request. If the maintainer of the repository accepts your pull requests, you will have a qualified entry into the contest. A pull request being accepted is not important, but it should not be marked as inappropriate by the repository maintainer.

1. Register on Hacktoberfest

Register on Hacktoberfest with your Github account. If you don’t have a Github account, you can make one for free. Setup your Github profile, so that repository maintainers can know your background when you send them a pull request.

https://hacktoberfest.digitalocean.com

2. Choose an Issue

You can choose an open issue from BlobCity DB and start working on it. We have issues specially marked for Hacktoberfest. You may choose issues from any other open source project too.

https://github.com/blobcity/db/issues

3. Connect with us

We are happy to help in getting you started. Join our Slack community and ask us anything on Hacktoberfest & BlobCity DB.

https://slack.blobcity.com

4. Fork the repository

Once you have decided you want to solve a particular issue, fork the repository on Github.

5. Resolve & Send Pull Request

Now you can choose an issue of your choice, and work on it. Test your new code, and commit it to your fork once your are done. You can now submit your work by generating a pull requests from your fork to the main repository. Now hope that the maintainer of the repository likes your work and accepts your pull request. Remember, it is not necessary for your pull request to be merged for you to have a qualified entry in Hacktoberfest.

That’s how simple it is. Choose your open source project and start contributing today. It’s time to give back to the open source we all love šŸ™‚

Count number of elements in Iterator – Java

The most elegant way to get the size of an Iterator or the count of number of elements in an Iterator is by using the utility method provided within the Guava library.

int size = Iterators.size(myIterator);

myIterator must be an implementation of Iterator<T>. Here T can be of any type.

Note:Ā The iterator will be consumed when the size is returned, so the same iterator may not be used for any other purpose.

Getting Guava Library

Library Source on GitHub

Maven

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>27.0-jre</version>
  <!-- or, for Android: -->
  <version>27.0-android</version>
</dependency>

Gradle

dependencies {
  compile 'com.google.guava:guava:27.0-jre'
  // or, for Android:
  api 'com.google.guava:guava:27.0-android'
}

 

Create String from an array in Java

String.join(",", collection);

The above statement uses join function within the String class to join the elements of the collection into the String. The elements are joint using the specified delimiter which is a “,” in this case.

final List<String> collection = new ArrayList<>();
collection.add("Hello");
collection.add("World");

final String joinedString = String.join(",", collection);

System.out.println(joinedString);

The above examples joins an array list collection of Strings to create a single comma delimited string.

Output

Hello,World

Integer Collection to String

The String.join function only works on String collections. However if you do happen to have a collection of Integers and need to join these integers into a single string delimited by comma, the below example shows how.

final  List<Integer> intList = new ArrayList<>();
intList.add(10);
intList.add(20);
intList.add(30);

final String joinedIntString = String.join(",", intList.stream().map(x -> "" + x).collect(Collectors.toList()));

System.out.println(joinedIntString);

Output

10,20,30

The .map function maps each Integer element into a String element simply by appending the Integer to an empty string.

 

Read Excel sheet in Java

The example below shows opening and reading Excel documents, using Apache POI library.

File file = new File("sample.xlsx");
Workbook workbook = WorkbookFactory.create(file);

workbook.sheetIterator().forEachRemaining(sheet -> {
    for(int i = 0; i < sheet.getLastRowNum(); i++) {
        Row row = sheet.getRow(i);
        for(int j = 0; j < row.getLastCellNum(); j++) {
            Cell cell = row.getCell(j);
            System.out.println(cell.getStringCellValue());
        }
    }
});

The above example prints a string value of all cells in a sequential order, for all sheets presentĀ inside the workbook.

Maven import for Apache POI library is mentioned below. The library can be downloaded from other sources as well.

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>

The Workbook object represents a single Excel document. The Excel may contain multiple sheets within it, wherein each sheet is represented by the Sheet object.

workbook.sheetIterator().forEachRemaining(sheet -> {});

Is used to iterate over all sheets present inside the Workbook. It is possible for the Workbook to not have any sheet.

sheet.getLastRowNum();

Gets the number of rows present in the respective sheet. This value is used to iterate through all rows present in the sheet. The row count is expected to differ per sheet.

Row row = sheet.getRow(i);

Gets the complete row at the specified row index. The row indexes start from 0. A row is a collection of cells, where each cell is a column. A row does not contain data values. A Row contains cells and each Cell in the Row contains a data value.

row.getLastCellNum();

Is used to get the number of cells in the Row. Typically each row in the sheet is expected to have same number of cells. However if some rows have merged cells, the number of cells in one row maybe different than the number of cells in another row in the same sheet. It is thereby important to get the cell count for each row and then iterate through each cell in the respective row.

Cell cell = row.getCell(j);

Gets a single Cell present at position j from within the row. The cell index starts from 0.

cell.getStringCellValue();

Assuming the cell contains a String value, the same can be got by using the getStringCellValue() function on the cell object. The cell may however contain numerical data, date-time field or maybe a formula. The contents of the cell need to be picked up appropriately to prevent errors.

Reverse a Java List, Array

Since Java 8, the order of elements in a List or its subclasses such as ArrayList can be reversed using a utility function provided as part of the Collections class.

Collections.reverse(list);

The list is reversed in place, which means the elements in the list are reversed inside the same object.

Collections.reverse(array); //DOES NOT WORK!

The implementation works only on an implementation of List. However if you would like to reverse the order of elements in an Array, and would like to do so with brevity of code without being concerned about expense of the operation, then a possible solution is as follows

List<Integer> list = Arrays.asList(array);
Collections.reverse(list);
array = list.toArray(new Integer[list.size()]);

The above code converts the array to list, reverses the list and then converts the list back to array. Please avoid this if possible, and use a List throughout your program instead of using Arrays.

JSON.equals in Java to compare two JSONs

JSON.areEqual(json1,json2)

The function returns true if the two JSON’s are equals and false if they are unequal. The parameter can be a JSONObject of type org.json.JSONObject or a JSON String.

The JSON utility is available as part of BlobCity Commons

Download JAR | View Source on GitHub

com.blobcity.json.JSON.areEqual("{}", "{}"); -> true
JSON.areEqual("{\"a\": \"1\"}", "{}"); -> false

The function checks for the complete JSON. Every element of the JSON must be equal for the equals check to pass. The following givesĀ areEquals => false

{
  "name": "Tom",
  "country": "USA"
}
{
  "name": "Tom",
  "country": "US"
}

Deeps checks are also support. Nested JSON’s must be equal for the JSON to be equal. The below 2 conditions emails the same.

areEqual => true

{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "Lane 1, USA"
  }
}
{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "Lane 1, USA"
  }
}

areEqual => false

{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "Lane 1, USA"
  }
}
{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "My lane"
  }
}

areEqual => false

{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "Lane 1, USA"
  }
}
{
  "name": "Tom",
  "country": "USA",
  "address": {
    "line1": "Lane 1, USA",
    "zip": "19700"
  }
}

Array comparions are also supported, and array elements must be in same order in both JSON’s for the equals to pass.

areEqual => true

{
  "name": "Tom",
  "roles": ["admin", "user"]
}
{
  "name": "Tom",
  "roles": ["admin", "user"]
}

areEqual => false

{
  "name": "Tom",
  "roles": ["user", "admin"]
}
{
  "name": "Tom",
  "roles": ["admin", "user"]
}

 

Iterate Over Keys of JSONObject – Java

JSON’s are commonly used in many Java programs, with most common libraries being org.json and com.google.gson. The examples below show how to iterate over keys of both json objects in Java 8.

With com.json.JSONObject

jsonObject.keys().forEachRemaining(key -> System.out.println(key));
jsonObject.keySet().forEach(key -> System.out.println(key));
jsonObject.keySet().parallelStream().forEach(key -> System.out.println(key));

The above methods simply iterate through all the first level keys within a JSONObject and print them to the console. The .keys() function returns an Iterator and does not support parallel execution. The .keySet() function returns a Set of the keys and maybe integrated in a single thread or in parallel.

If printing to console was the only objective, the lambda expression can be better used as

jsonObject.keySet().forEach(System.out::println);

With com.google.gson.JsonObject

jsonObject.entrySet().forEach(entry -> System.out.println(entry.getKey()));
jsonObject.entrySet().parallelStream().forEach(entry -> System.out.println(entry.getKey()));

The Google’s JsonObject implementation provides for iteration over the key-value pairs inside the JsonObject. So it is not just the key, but the value also can be obtained using a single iteration.

jsonObject.entrySet().parallelStream().forEach(entry -> {
    System.out.println(entry.getKey());
    System.out.println(entry.getValue().getAsString());
});

retainIf and removeIf for Java Collections

Iterate over Java collection classes to conditionally retain or remove values from the collection.

CollectionUtil.retainIf(list, element -> element > 5);

Retains all elements in the collection that have a value greater than 5.


CollectionUtil.removeIf(list, element -> element > 5);

Removes all elements from the list that have a value greater than 5.


The CollectionUtil class is available as part of BlobCity java-commons open source distribution of useful Java utilities.

https://github.com/blobcity/java-commons

Jar file:Ā https://github.com/blobcity/java-commons/tree/master/targetĀ 

Complete Implementation

List<Integer> list1 = new ArrayList<>(Arrays.asList(1,5,10,30,4,8,11));
List<Integer> list2 = new ArrayList<>(Arrays.asList(1,5,10,30,4,8,11));

/* Retains all elements in list that have a value > 5 */
CollectionUtil.retainIf(list1, element -> element > 5);

System.out.println("List 1");
list1.forEach(System.out::println);

/* Removes all elements in list that have a value > 5 */
CollectionUtil.removeIf(list2, element -> element > 5);

System.out.println("\nList 2");
list2.forEach(System.out::println);

Output

List 1
10
30
8
11

List 2
1
5
4