Skip to content

Map – Java, Scala, Ruby

December 25, 2015

begin
names = Hash.new

startTime = Time.now

(0..200000).each do |i|
#puts “adding #{i} to the list.”
# pushes the i variable on the *end* of the list
names[i] = “Jagan”+i.to_s
end

names.each_pair {|key,value| puts “#{key} = #{value}”}
endTime = Time.now

p startTime
p endTime
end

 

 

package com.jasokan.test;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class TestMap {

public static void main(String[] args) {
Map<Integer, String> nameMap = new HashMap<Integer, String>();

Calendar startTime = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(“HH:mm:ss”);

for (int i = 0; i < 200000; i++) {
nameMap.put(new Integer(i), “Jagan” + i);
}

for (Integer name : nameMap.keySet()) {

String key = name.toString();
String value = nameMap.get(name).toString();
System.out.println(key + ” ” + value);

}

Calendar endTime = Calendar.getInstance();
System.out.println(sdf.format(startTime.getTime()));
System.out.println(sdf.format(endTime.getTime()));
}

}

 

 

import java.util.Calendar

object TestMap {
def main(args: Array[String]) {

var names: Map[Int, String] = Map()

val startTime = Calendar.getInstance().getTime()

for (i <- 1 to 200000) {
names += (i -> (“Jagan” + i.toString()));

}
names.keys.foreach { i =>
print(“Key = ” + i)
println(” Value = ” + names(i))
}

val endTime = Calendar.getInstance().getTime()

println(startTime)
println(endTime)

}
}

Run the programs to get the time taken 🙂

But an interesting problem to observe

Please watch the Creation and printing of Map in Scala!

 

 

 

Advertisements

Concurrent calls to an API using Ruby

December 18, 2015

Got an interesting problem

  1. We need to read a csv file in chunks
  2. Process the chunk of data (transformation)
  3. Once the data is transformed, call the URLs concurrently

This is to avoid the sequential operation. For example, you have a REST API by which you update the address of the employees.

The addresses are going to be different and their details as well (employee id and others)

The bottleneck will be the database which can be handled by throttling the count of concurrent calls.

There are two gems which came handy to solve this problem

  1. Typhoeus
  2. Smarter CSV

Code sample

Reading the csv file in chunks

options = {:chunk_size => 30}
n = SmarterCSV.process(file_name, options) do |chunk|
process(chunk, otherArguments)
end

Creating the chunk requests

def process(chunk,otherArguments)
urlArray = Array.new
chunk.each do |row|
request_body_map = {
:test => row[:test]
}
urlArray << row[:test].to_s + request_body_map.to_json
end
callConcurrent(urlArray, url)
end

Calling the requests Concurrently

def callConcurrent(requestDetails, url)
bodyString = “”
hydra = Typhoeus::Hydra.new
requestDetails.each do|website|
request = Typhoeus::Request.new(
url,
method: :put,
body: bodyString
)
request.on_complete do |response|
p request + ” === ” + response.code.to_s;
end
hydra.queue(request)
end
hydra.run
end

 

There is a significant improvement in processing the data. Identify the load and tweak the chunk count and you will be able to achieve better performance.

 

Creating Capped Collections using Ruby – Sample Program

August 18, 2015

require ‘mongo’

begin

client = Mongo::Client.new([ ‘<hostname/ip>:<port>’ ],:database => ‘<databasename>’, :user => ‘<username>’, :password =>'<passwprd>’)

collection_name = {

“COLLECTION1” => 1610612736,
“COLLECTION2” => 1610612736 #SIZE in bytes (Here it is 1.5G)
}

collection_name.each do |collectionValue|
collectionDetail = client[collectionValue[0], :capped => true, :size =>collectionValue[1]]
collectionDetail.create
end

end

end

Running Scala

February 23, 2015

As mentioned earlier, using the Scala – IDE for running the program. But how we can run it from the command line was the question and using the following steps, one can run their Scala program. Please note, the following sequence needs Scala-IDE! In the following example, i’m casbah for MongoDB!

Following is the package structure of the firstTrial project.

scalarun-1

Click on the export in the File menu and following window will be displayed

scalarun-2

scalarun-3

Use the jar file and set the classpath.

Set classpath=h:\mongo-java-driver-2.13.0.jar;h:\slf4j-api-1.7.10.jar;h:\slf4j-log4j12-1.7.10.jar;h:\log4j-1.2.17.jar;h:\log4j-core-2.1.jar;h:\scala-library-2.11.5.jar;h:\json4s-core_2.11-3.2.11.jar;h:\paranamer-2.5.6.jar;h:\json4s-native_2.11-3.2.11.jar;h:\casbah-commons_2.11-2.8.0.jar;h:\casbah-core_2.11-2.8.0.jar;h:\casbah-gridfs_2.11-2.8.0.jar;h:\casbah-query_2.11-2.8.0.jar;C:\Users\Jagannathan\Downloads\scala-library.jar;C:\Users\Jagannathan\Desktop\firstTrial.jar;%CLASSPATH%;.

Run the program as

Java com.jagan.myscala.package

You will get the output!

Will write more details in the next post.

The roads

February 12, 2015

This is a nostalgic post due to the amount of time which took to phrase it.

Off late, there are several things which are debated on the web.

The basics of being successful in India is difficult as you have to be a successful

  • Son
  • Father
  • Husband
  • Brother
  • Coder
  • Worker and others

According to everyone their perspective is always correct. No one can understand what is the essential part of winning over other’s confidence. India is a country where there are several beliefs. You have your own Religion, Region, State, County and others. Added to these, there are several family beliefs.

So, there is no right or wrong and the action taken during that situation is correct according to that person. Everyone can point fingers, but not taking decision is also a wrong decision. So, please decrypt yourselves 🙂

Empathy – This word is very much needed to be successful for every human being. What is Empathy?

Empathy is the capacity to understand what another person is experiencing from within the other person’s frame of reference, ie, the capacity to place oneself in another’s shoes.

This needs good amount of maturity. This is where an adult differs from a Kid. I can cite so many instances where i had the experience of convincing my kids. Both will be adamant to have an Icecream. They cannot think about us on the aftermath of having an icecream when you have cold.

Follow me and let’s go denotes the same. That is the difference the empathy creates. A single hand cannot create sound.

Recently read a message in facebook and i like it. Team work is not group of people who work together. It is a group of people who trust each other. This is one of the well written statement!

All we are talking about is about our perspectives. There is GenX and GenY. God only know how these cyber-era kids are going to be. The meaning of life is different for them. Let me take an example, most of the people who started working after 2K would have seen a computer during the school final. But in today’s world, they are seeing the computer as soon as they are born!

The situations are going to be different everyday and the business and the perception changes as the day progresses.

If you feel, this is abstract, i wanted to be in this way. Drop a mail to jasokan@gmail.com for further debates 🙂

Scala – First Experience

February 12, 2015

Using Eclipse IDE and the details are as follows

Scala IDE build of Eclipse SDK
Build id: 3.0.4-2.11-20140723-2253-Typesafe

Scala Library – 2.11.2

A phenomenal change in the mindset.

The first learning is

  • def defines a method
  • val defines a fixed value (cannot be modified)
  • var defines a variable (can be modified)

Took sometime to understand.

How to read a file and move the contents to a list? (“\n” being the line separator)

val fileContentsList= io.Source.fromFile(“h:/SampleTripFile.txt”).getLines.toList

main method is also there

def main(args: Array[String]) {

Used Casbah (Scala toolkit for MongoDB).

Yes, good learning and enjoying the constructs and the simplicity.

Ruby – Time in Millis

January 31, 2015

The scenario which i encountered is to convert the date from database and convert it to Time in Millis (Example – 1422720326688)

The reason is the third party API which we use forces us to give the time in mills since unix epoch.

Following is the code snippet which can be used. I love Ruby for the expressive nature.

time = “#{rowDistinct[2]}” #- Date from Database – 2014-12-26 10:35:22 format
dTime = DateTime.parse(time)
aTime = dTime.to_time

aTime.to_i # – This will give the result in 1422720326688 format

Happy Rubying 🙂