Wednesday, April 22, 2015

Install Redis 3.0.0 on Ubuntu 14.04.2 Server

Redis now officially only supports Linux-based OS. Here I installed Redis in Ubuntu 14.04.2 Server and the version is 3.0. First I need to download it from here.

Open the terminal and input the command as follow:
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
After download completed, uncompress the zipped file:
  
tar xzv -f redis-3.0.0.tar.gz
Enter the redis-3.0.0 folder and excuted the make command:
 
cd redis-3.0.0
make 
If the make is not found, excute the command below to install it:
 
sudo apt-get install make
If the error message shows as below:
/bin/sh: 1: cc: not found
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory `/home/javakid/tools/nosql/redis-3.0.0/src'
make: *** [all] Error 2
Then installing build-essential is needed.
 sudo apt-get install build-essential
If something is missing as the message here:
In file included from adlist.c:34:0:
zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
                               ^
The solution is to make the lacking file:
cd deps 
make jemalloc
Go back to redis-3.0.0 and execute make command again.

If the error below occured:
cc: error: ../deps/hiredis/libhiredis.a: No such file or directory
cc: error: ../deps/lua/src/liblua.a: No such file or directory
make[1]: *** [redis-server] Error 1
make[1]: Leaving directory `/home/javakid/tools/nosql/redis-3.0.0/src'
make: *** [all] Error 2
Just go to the folder deps and make the lacking files:
cd deps
make hiredis lua jemalloc linenoise
Go back to parent folder and execute the make command after the files are made.
cd ../
make
When the make command is completed, execute the command below to start Redis.
 src/redis-server

Saturday, May 24, 2014

How to use Java to download file on internet

At first, I thought I would need to write thousands line of code to download the file on internet. But after studying some article, I found that it is easy to use Commons IO.
Let's start with the sample code below:
package idv.jk.stock;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class TestFirst {

 public static void main(String[] args) {
  try
  {
   String strUrl = "<The url of the file>";
   URL source = new URL(strUrl);
   String theStrDestDir = "<The folder you want to put the file>";
   File theStockDest = new File(theStrDestDir);
   FileUtils.forceMkdir(theStockDest);
   
   File destination = new File(theStrDestDir + 
                                   "<the saved filename>");
   
   FileUtils.copyURLToFile(source, destination);
   //File file = new File(".");
   System.out.println("File Downloaded!");
  } catch (MalformedURLException e)
  {
   e.printStackTrace();
  } catch (IOException e)
  {
   e.printStackTrace();
  }
 }

}
Here, let point out something worth mentioning. Also, you can read the API of Commons IO.

At line 21, I used FileUtils.copyURLToFile to build the folder to put the downloaded file. By using the method FileUtils.copyURLToFile, I don't have to worry about if the destination folder and its parent folder exit or not. FileUtils.copyURLToFile will create all the folders.

And the most important, at line 24, is using the method FileUtils.copyURLToFile to download the file from internet.

The first parameter is a URL object(API doc) containing the url of the file and the second is a File object (API doc)representing the directory to put the file.

References