Search This Blog

Hazelcast with python

Assumption: You are aware of, how to start hazelcast if not check here.

In today’s era python is must know language. It’s being used widely to implement regular BAU logic to special machine learning. In the parallel world hazelcast is providing extremely useful in-memory data grid which solve the problem of having restricted size of data in memory to process, hence increasing the overall performance of systems.

Let’s see how we can connect to hazelcast from our python code.

The available hazelcast client is compatible with python 2.7 version.

Let’s do this in steps

Prerequisite : hazelcast instance must be up and running and you should have the credentials and ip information 

Step1

Install python version 2.7 from here

Step 2

Install hazelcast python client. Execute below command
pip install hazelcast-python-client

Sometimes because of some restrictions we can’t install it directly. Use proxy in that case as
pip install --proxy http://<user>:<pwd>@<proxy>:<port> hazelcast-python-client

Step 3

  • Create a new python module(intelliJ) or project (eclipse)
  • Create a new python file as hazelClient.py
  • Add code as below (git)

config = hazelcast.ClientConfig() 


# use the credentials which has been used to start the hazelcast instance (pre-requisite) 
config.group_config.name = "user"
config.group_config.password = "pwd"


# localhost is the hostname or IP address, e.g. 'localhost:5701' or 143.91.84.22:5701 
config.network_config.addresses.append('localhost')

# basic logging setup to see client logs

logging.basicConfig()
logging.getLogger().setLevel(logging.INFO) 


# creating hazelcast client instance
client = hazelcast.HazelcastClient(config) 


my_map = client.get_map("test_map")
my_map.put("1", "data") 


client.shutdown()



How to read file in Java


Reading file is a very common scenario. There are different ways to read a file in Java using java’s specific reading classes and also using third party utility jars. Lets look at some of the ways of reading files in java

  1. Using Google’s Guava 
List<String> guavaList = Files.readLines(new File("/home/myfiles/test.dat"), Charsets.UTF_8);
  1. Using Apache Commons IO

List<String> apacheList = FileUtils.readLines(new File("/home/myfiles/test.dat"));


Both of these methods will read whole file line by line and will return a List of String with each line as list’s content. Now since it reads the whole file at once it would require to load the file in memory. This would not be a good way to read large files. For reading large files we can consider using BufferedReader which provides the stream from file and make data available.

Apache commons also provide the utility method which reads the file line by line using a LineIterator. LineIterator works similar to the iterators we have in java for iterating different collections (check here).

sample code as below

FileInputStream fi = new FileInputStream(new File("c:/home/myfiles/test.dat"));
BufferedReader br = new BufferedReader(new InputStreamReader(fi));
System.out.println("***** Using Buffered Reader *****");
String line=null;
while((line = br.readLine())!=null){
System.out.println(line);
}
br.close();


using apache commons

LineIterator iterator = FileUtils.lineIterator(new File("c:/home/myfiles/test.dat"));


You can find the same code @ git 



How to create weblogic domain in weblogic 10.3

I am using windows 10 with weblogic 10.3. I am assuming that you have already installed weblogic 10.3 on your machine. Click here for weblogic 10.3 installation details.

Follow below steps to create a new domain in weblogic

Step 1 : Click on Windows button on left corner and search for config. It will show configuration wizard. Click on that.

Step 2 : It will open an Oracle’s window for domain creation. Select “Create a new Weblogic domain” and click on Next.

Step 3 : Select “ generate a domain . . . . “  and click on Next.

Steps 4 :  On this screen create a username and password of your choice , which would be used for login on admin console of your domain. Memorize the credentials or keep it in safe place. Click on Next.

Step 5 : On this screen, You can configure whether you want to create domain for development purposes or it’s a production domain and which jdk version you want to use. Weblogic is bundled with sun jdk and jrockit but it also provide option to choose a custom jdk. Choose your options accordingly.For this tutorial I am using development mode with sunJDk provided by weblogic.

Step 6 :  There are some pre-defined domain configuration (defined by weblogic), on this screen you can choose to modify that or can go with existing pre-defined configuration. For demonstration purposes, I will choose to configure it by my own (you can select “No” and move forward). Select “Yes” and click Next.

Step 7 : On this screen you can configure a RDBMS secure store which is nothing but creating datasource. We can do that later as well. Since “I don’t want to change anything here” I will choose so and click on next.

Step 8 : On this screen , you can configure your admin server eg. Name of server, listening port, whether it should handle SSL request or not etc. Default Listen port address is 7001, I am using a custom listening port (10001) which I know is not being used on my machine. Click on Next.

Step 9 : Through this screen we can add managed servers to your domain, numbers could vary based on your application’s requirement. Managed servers are nothing but an instance of weblogic server which can host your application and using a load balancer you can manage the load. This can be discussed in detail in clustering. I am not adding any specific managed server and will run only admin server. Click on Next.

Step 10 : Using this screen, we can add multiple machines on which we can start weblogic server instances and using admin server we can configure all the managed servers (from local as well as remote machines which would be added here) under one cluster. So, a Weblogic cluster consists of managed servers residing on different machines. Since we are setting up a simple domain we will not add any machine here. Click on Next.

Step 11 : It’s just a review screen about the information we entered on previous screens. Click Next.

Step 12 : Enter the name and location of your domain and click on create.
Woow! ! ! ! Your new domain is created

Step 13:  After clicking create you will see screen as below. Select “Start Admin Server” and click on Done. It will openup a windows console and admin server will get started in Running mode.

Step 14: Open any browser and access this url : http://localhost:<port>/console , replace port with listen port number that you have entered in step 8. And use the same username password to login.

Starting and Stopping Servers
           1) Go to your domains location : E:\bea\user_projects\domains\blogDomain
           2) Execute startWebLogic.cmd or startWebLogic.sh based on your OS type. This will start the admin server for that domain.
          3) To Stop the server – go on the running console and do ctrl+C

Java Interview Question : Reverse a Sentence

Problem : Reverse a given sentence without using any library function. Say if the given sentence is "I am a Java Student", output must be "Student Java a am I"

Solution :  This kind of question can have another flavor which may need to print the output in exact reverse order i.e. output should be "tnedutS avaJ a ma I". This would not be difficult to implement with or without using any library function as we are not breaking the sequence of character which need to be printed. But, In case of actual question we have to read each word from end and then print it. There could be different ways to handle this problem but I have used recursion to achieve the same. Code as below.

public class ReverseSentence {

 public static void main(String[] args) {
  String s = "I am a Java Student";
  StringBuilder sb = new StringBuilder(s);
  System.out.println("Using java api : "+ sb.reverse());
  System.out.println("Output as required : " +reverseMe(s));
  

 }
 private static String reverseMe(String s) {
  System.out.println("logger ::" + s); 
  int i = s.indexOf(" ");// getting the index of space which is delimeter in case of sentence
  return i==-1 ? s : reverseMe(s.substring(i+1)) + " " + s.substring(0, i);
  
 }

}
Output :
Using java api : tnedutS avaJ a ma I
logger ::I am a Java Student
logger ::am a Java Student
logger ::a Java Student
logger ::Java Student
logger ::Student
Output as required : Student Java a am I