Google Firebase Tutorial Beginners.

In this tutorial will see how to connect to Firebase Firestore Database and persist data using rest web services.

For this will start from the rest web service that we built using spring boot in the previous blog post Build Restful web service using Spring boot and Java.

If you are a video guy here is the video which demonstrates connecting to Firebase and persists data

Go to and click on the got to console on the top right corner, it will take you to the console as shown below. Click on Add project and give it a name.

Firebase Console

Once created it will take you to your project console page as shown below, click on the database and create a collection named “users”.

Once created the collection, we will go to project settings -> service Accounts tab to generate the key and save it in your project, that we will use to connect to the firebase firestore database. We will also grab the admin SDK configuration snippet for java.

Now open the pom.xml and add the google firebase admin SDK dependency


Add a file named FirebaseInitialize which we will use to make a connection to firebase during the application startup. The class looks like this.

package com.example.demo.service;


import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;


public class FirebaseInitialize {
	public void initialize() {
		try {
		FileInputStream serviceAccount =
				  new FileInputStream("./serviceAccount.json");

				FirebaseOptions options = new FirebaseOptions.Builder()

	} catch (Exception e) {


@Service Annotation lets the spring bootup know that it is a service class and needs to be bound.
@PostConstruct Annotation specifies that the function initialize() should run before the launch of the application, so it creates a firebase connection.
We have the code that we copied from the firebase console in the earlier step pasted here.
The first line reads the serviceAccount.json which we generated on the firebase project settings page from step 1.
It connects to firebase with FirebaseApp.initializeApp using the options build using FirebaseOptionsBuilder which will have credentials that we extracted from serviceAccount.json and the databaseURL.

We will now write a Firebase Service class which will have four functions to do crud operations, createUser (to create new User), updateUser (to update existing User), getUserDetails(to retrieve user details), deleteUser (to delete existing User) the class looks like below.

package com.example.demo.service;

import java.util.concurrent.ExecutionException;

import org.springframework.stereotype.Service;

import com.example.demo.objects.Person;

public class FirebaseService {
	public String saveUserDetails(Person person) throws InterruptedException, ExecutionException {
		Firestore dbFirestore = FirestoreClient.getFirestore();
		ApiFuture<WriteResult> collectionsApiFuture = dbFirestore.collection("users").document(person.getName()).set(person);
		return collectionsApiFuture.get().getUpdateTime().toString();
	public Person getUserDetails(String name) throws InterruptedException, ExecutionException {
		Firestore dbFirestore = FirestoreClient.getFirestore();
		DocumentReference documentReference = dbFirestore.collection("users").document(name);
		ApiFuture<DocumentSnapshot> future = documentReference.get();
		DocumentSnapshot document = future.get();
		Person person = null;
		if(document.exists()) {
			person = document.toObject(Person.class);
			return person;
		}else {
			return null;
	public String updateUserDetails(Person person) throws InterruptedException, ExecutionException {
		Firestore dbFirestore = FirestoreClient.getFirestore();
		ApiFuture<WriteResult> collectionsApiFuture = dbFirestore.collection("users").document(person.getName()).set(person);
		return collectionsApiFuture.get().getUpdateTime().toString();
	public String deleteUser(String name) {
		Firestore dbFirestore = FirestoreClient.getFirestore();
		ApiFuture<WriteResult> writeResult = dbFirestore.collection("users").document(name).delete();
		return "Document with ID "+name+" has been deleted";

We will have this class also defined with @Service annotation so it will be available for auto wiring.
Since having the connection to the database initialized earlier we will extract the DB here to perform operations on that.
Firestore dbFirestore = FirestoreClient.getFirestore();
this will instantiate the firestore client object which we will use to access the collections we created.
ApiFuture collectionsApiFuture = dbFirestore.collection(“users”).document(person.getName()).set(person)
this line of syntax gets the collection “users” and tries to document with id (In our case i’m setting the id as the users name), if it didn’t find the document with specified id it will create the new document with Person object .
If there is an id already exists it will update the document, if you observer the function updateUser it will have the same logic as createUser.

If you have a problem following the tutorial, here is the video which demonstrates the complete tutorial.

Build A RESTful Web Service Using Java And Spring Boot

In this following tutorial, we will build a simple REST web service using java with spring boot.

What is a Web Service?
Web Service is a standard or common way of communication between a client and a server.

Restful Web Services, Web Service

The above diagram shows the pictorial representation of a web service call, the clients a computer, or a phone are basically clients, when you try to perform open facebook app your mobile phone app, it sends a web service request (Http Request) to get the data to show on your feed.

There are in general two kinds of web services in usage

  • SOAP (Simple Object Access Protocol).
  • REST (REpresentational State Transfer).

SOAP: SOAP as the name says it’s an access protocol to make requests and receive responses based on a protocol that will be defined between the client and the server.
There are many caveats of using SOAP one of it is you can only transfer and receive XML data when using a SOAP web service. Hence restful web services came into existence.

REST: Rest as the name says its a state of transferring data between clients and servers, there are no set protocols attached to it, you can send any kind of data, XML, JSON, TEXT, FILE format data using REST web services, in most cases, you will be using JSON format.

SPRING BOOT makes it so much easier to build REST web services from the ground up, it provides a lot of configuration inbuilt so that you can build web services/ microservices rapidly, it also provides inbuilt tomcat server which removes the hassle of having a server install in and deploy your JAR/WAR file like the old times, we will see how to build the rest web service using Spring boot, Java and maven build tool.

Using the spring tool suite we can create a sample spring boot project as shown in the below image. Go to File -> New -> Other Project and select Spring boot-> Spring boot Starter project it will create you a default spring boot project.

Spring tool suite create spring boot project.

Follow the steps and you will have your demo application created and you should be able to see a java file named (Demo is the name of the project) which will look something like this.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class DemoApplication {

	public static void main(String[] args) {, args);


@SpringBootApplication annotation takes care of three annotations combined
@Configuration: Tags the class as a source of bean definitions for the application context.
@EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application.
@ComponentScan: Tells Spring to look for other components, configurations, and services in the base package in our case com.example.demo letting it find the controllers.

If you never use Spring MVC you might have not known of these annotations. Spring boot starter project also creates a POM.XML like below which will have all the required dependencies except spring-boot-starter-web dependency which we need to add for creating Rest web service.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<description>Demo project for Spring Boot</description>






Now we will go ahead and create a controller class that will have our REST endpoints, which will look something like below.

package com.example.demo.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.object.Person;

public class UserController {
	public String getUserDetails(@RequestHeader String name) {
		return "Requesting Details of User"+name;
	public String createNewUser(@RequestBody Person person) {
		return "Created new User "+person.getName();
	public String updateUser(@RequestBody Person person) {
		return "Updated user "+person.getName();
	public String deleteUser(@RequestBody Person person) {
		return "Deleted User "+person.getName();


HTTP Methods: There are four main HTTP methods that we use in common for web service calls they are

  • GET (used to retrieve existing data).
  • POST (used to create/insert new data).
  • PUT (used to update existing data).
  • DELETE (used to delete data).

We have implemented all these methods in the above UserController class.
@RestController lets the spring boot application that this class is a controller class having rest endpoints so that spring configures this class to its HTTP routes.
@GetMapping, having this annotation makes the corresponding method is called when a get call is made to the endpoint /getUserDetails.
Similarly @PostMapping, @PutMapping, @DeleteMapping calls the corresponding methods/functions when those methods are used.
@RequestHeader captures the data sent in the header parameters and @RequestBody captures the data sent in the body of the Rest Call.

That is all you need to bring up the service, set up the run configurations in STS or whichever IDE you might be using pointing to the class as the main class, it boots up the spring application using inbuilt tomcat and your services will be available on PORT 8080 by default.

You can follow through this video if you need any help in getting it up and running

Restful Web Services Tutorial.

CSS Linear Gradient Function

CSS Linear Gradient

Linear Gradient function is introduced in CSS3 which helps to transform from one color to another seamlessly which would have been a herculean task to achieve, with linear-gradient() function you can achieve something like this.

Linear Gradient CSS
  • Linear Gradient function takes multiple parameters, you can give the rotation of the colors as deg, percent %, or direction, etc.. the syntax looks something like this.
linear-gradient(direction, color1, color2, ...);

The code for the above-shown image is as follows.


        body {
            display: flex;
            flex-direction: row;
            background: wheat;
            width: 100%;
            height: 100%;
            margin: 0;

        .div1 {
            width: 50%;
            height: 100%;
            background: wheat;
            color: white;

        .div2 {
            width: 50%;
            height: 100%;
            background: linear-gradient(45deg, yellow, red, transparent);
            color: white;

    <div class="div1">
    <div class="div2">


In the above example div1 class has a plain background where div2 class uses linear-gradient() with which we were able to achieve the transformation.

Here is complete video explaining linear-gradient() function and repeating-linear-gradient() which will see in an another post.

If you i’m using CSS Flexbox to achieve the side by side window view, if you want to see how you can achieve here is the tutorial for it Flexbox Basics .

What do you need to know to get into Data Science as a Beginner?

Data Science Flow

Data Science is a combination of Programming & Statistics, so to be a data scientist you need to have knowledge of at least one programming language, preferably Python/R as there is a good amount of people/communities who use these languages to build their models.

For a complete beginner, Python is easy to learn. Some of the basic tools used in data science from Python stack.

Jupyter Notebooks – IDE.
Pandas – library for data manipulation and analysis.
Numpy – library for scientific computing.
Matplotlib & Seaborn – library for data visualization.
Scikit-Learn – library for machine learning.

Good mathematical knowledge helps to make a better judgment while choosing a procedure (algorithm) based on the data available to you and also to diagnose the problems.

Data Science Prerequisites HackerHeap Diagram

If you don’t have time to go through the theory, start with a tutorial. Follow the tutorial step-by-step. After you complete a tutorial, apply what you learned to new datasets. You can find some sample datasets online ( If you try the same modeling on a new dataset, you might run into a new issue. Upon doing some research, you might discover data issues in the dataset like different formats, or missing values.

If you are looking for more resources offers some good and free courses.