Archive

Posts Tagged ‘Java’

Zookeeper C# Client with IKVM .NET

September 27, 2014 Leave a comment

Why ?

A lot of big data technology created with JVM. The language are vary from plain old Java, Scala and Clojure. There are a huge number of Apache project for this kind of stuff. The infrastructure for building a serious data product is amazingly mature in Java world.

The server is in Java . Ok. fine. But do we have to code in Java too ? At least as a client using the server ?. I know that JVM is awesome, but the language is not so well compare to C#. Especially the latest one C# 5. But for now we have to admit that .NET world is very lack of Server solution.

I always try to find a client library in C#/.NET but seems that it’s not really mature or has been already dead before birth. I will give you an example for this one.

 

Zookeeper is for the distributed zoo

Zookeeper has been used as a distributed process control. If you still remember the operating system 101 about process, threads, inter-process communication so this one should be easy for you to learn. If you familiar using lock or any synchronization mechanism like mutex, semaphore etc, just think this as way to do to coordinate multiple server synchronization. And it’s only expose a simple primitive like a file system. This is amazing and flexible. At the same time also make you do a lot of works to get the stuff working.

image

So we know that Zookeeper is good, it used by a lot of big data technology like Storm, Kafka, etc. So we need to find a client library to use it right ? Good luck finding it for .NET. There are a couple of library for doing that stuff but it’s either old or not up to date.

A couple of sample of that kind of library is :

https://www.nuget.org/packages/ZooKeeper.Net/

https://www.nuget.org/packages/ZooKeeperNet/

https://github.com/devhong/Zookeeper.Net

Read more…

Advertisements

6. Java vs C# (Extension Method)

January 31, 2010 5 comments

kita bisa menambakan method seolah2 method tersebut terletak di kelas tersebut.

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}

kita bisa menggunakannya sebagai berikut

using ExtensionMethods;
....
string s = "Hello Extension Methods";
int i = s.WordCount(); // seolah2 ada method WordCount pada string.
....

klo make java harus gini …

public class StringUtils
    {
        public static int WordCount(String str)
        {
              // something like this lah in java
             // return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }

manggilanya seperti ini

String s = "Hello Extension Methods";
int i = StringUtils.WordCount(s);

jelas c# lebih mudah dibaca dengan extension method…..

Categories: Uncategorized Tags: , ,

5. Java vs C# Lamda Expression

January 31, 2010 Leave a comment

Lamda expression membuat code delegate jadi lebih singkat

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => (n % 2 == 1)); // find the lambda expression here? and there's extension method too

mungkin ada yg bertanya2 darimana method Count yg ada di array integer? Itu extension method. Aku akan jelasin di postingan berikut

klo make java kita harus buat interface lagi dan melewatkannya sebagai strategy.

interface INumberSpesification {
    boolean isSatisfiedBy(int number);
}

class OddNumberSpesification extends INumberSpesification {
    public boolean isSatisfiedBy(int number) {
         return n % 2 == 1;
    }
}

class Numbers {
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
         
    public int count(INumberSpesification a) {
        // foreach numbers cek dengan a.isSatisfiedBy(_element).
        // jika benar maka increment count
       
        // return count
    }
}

simple is beauty. simple is fast.
more code more bugs

Categories: Uncategorized Tags: , ,

4. Java vs C# (Head to Head) LINQ

January 31, 2010 Leave a comment

klo di java mw cari elemen pada array yg lazim dilakukan adalah looping dan mengecek sampai kriteria yg diinginkan ketemu.

alangkah indahnya klo misalnya kita bisa query dari object….

LINQ is the absolute winner here…

public class Student
{
    public string First { get; set; }
    public string Last { get; set; }
    public int ID { get; set; }
    public List<int> Scores;
}

// Create a data source by using a collection initializer.
static List<Student> students = new List<Student>
{
   new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 92, 81, 60}},
   new Student {First="Claire", Last="O’Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}},
   new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {88, 94, 65, 91}},
   new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {97, 89, 85, 82}},
   new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {35, 72, 91, 70}},
   new Student {First="Fadi", Last="Fakhouri", ID=116, Scores= new List<int> {99, 86, 90, 94}},
   new Student {First="Hanying", Last="Feng", ID=117, Scores= new List<int> {93, 92, 80, 87}},
   new Student {First="Hugo", Last="Garcia", ID=118, Scores= new List<int> {92, 90, 83, 78}},
   new Student {First="Lance", Last="Tucker", ID=119, Scores= new List<int> {68, 79, 88, 92}},
   new Student {First="Terry", Last="Adams", ID=120, Scores= new List<int> {99, 82, 81, 79}},
   new Student {First="Eugene", Last="Zabokritski", ID=121, Scores= new List<int> {96, 85, 91, 60}},
   new Student {First="Michael", Last="Tucker", ID=122, Scores= new List<int> {94, 92, 91, 91} }
};

kita bisa melakukan query dari data pada list tersebut dengan LINQ.

caranya adalah sebagai berikut..

// Create the query.
// studentQuery is an IEnumerable<Student>
var smartStudentQuery =
    from student in students
    where student.Scores[0] > 90
    select student;

sudah pasti lebih mudah dengan LINQ klo mw cari2 di tumpukan object….
agree?

Categories: Uncategorized Tags: , ,

3. Java vs C# (Delegates and Events)

January 31, 2010 Leave a comment

Di c# jelas lebih mudah mengimplementasikan Observer pattern dengan adanya delegates dan event.

Klo di Java? harus buatin interface2 lagi. Konsep2 yg dipake sama dengan konsep listener yg ada di swing.

jelas c# lebih simple…

Categories: Uncategorized Tags: , ,

2. Java vs C# (Indexers)

January 31, 2010 2 comments

java version dari wrapper untuk list mahasiswa.

public class MahasiwaBlackList {
   private List<Mahasiwa> _mahasiswaBlackList = new ArrayList<Mahasiwa>();
   
   public void addBadMahasiswa(Mahasiwa mahasiswa) {
      _mahasiswaBlackList.add(mahasiswa);
   }
   
   public Mahasiwa getBadMahsiswa(int rating) {
      //if (_mahasiswaBlackList.size() == 0) throw ThereNoBadStudentException();
      return _mahasiswaBlackList.get(rating);
   }
   
   public static void main(String[] args) {
      MahasiwaBlackList a = new MahasiwaBlackList();
      a.addBadMahasiswa(new Mahasiwa());
      
      System.out.println(a.getBadMahsiswa(0)); // using method
   }

di c# lebih cool dengan indexers

public class MahasiswaBlackList
    {
        // default private
        List<Mahasiswa> _mahasiswaBlackList = new List<Mahasiswa>();

        public void AddBadMahasiswa(Mahasiswa mahasiswa)
        {
            _mahasiswaBlackList.Add(mahasiswa);
        }

        public Mahasiswa this[int rating]
        {
            get { return _mahasiswaBlackList[rating]; }
        }

        static void Main()
        {
            MahasiswaBlackList blackList = new MahasiswaBlackList();
            blackList.AddBadMahasiswa(new Mahasiswa {
                GPA = 0.1,
                Name = "Longgur",
                NIM = "11111111"
            });

            Console.WriteLine(blackList[0]); // using indexers => like an array isn't ?
        }
    }

yeah.. yeah…

Categories: Uncategorized Tags: , ,

1. Java vs C# (Properties)

January 31, 2010 Leave a comment

Mungkin udh basi ya mengenai fitur2 c# di 3.0. Tapi itu salah yg membuatku lebih prefer java ketimbang c#. Di postingan2 ke depan aku akan bahas mengenai komparasi dengan java.

Oke. beginilah kita membuat object (not really good object i think => karena hanya attribute dan setter getter) di java

public class Mahasiwa {
   private String nim;
   private String name;
   private long gpa;
   
   public String getNim() {
      return nim;
   }
   
   public void setNim(String nim) {
      this.nim = nim;
   }
   
   public String getName() {
      return name;
   }
   
   public void setName(String name) {
      this.name = name;
   }
   
   public long getGpa() {
      return gpa;
   }
   
   public void setGpa(long gpa) {
      this.gpa = gpa;
   }
}

buset. hanya mw buat object panjang banget. Sebenarnya bukan object sih tapi lebih ke arah DTO (data transfer object) sih. Eh. gak juga dengs. Klo DTO kan harus immutable. whateverlah…. oh.. data structure lah.. itu lebih cocok.. soalnya object harus punya behaviour. bukan hanya set get doang..

Line of code yg cukup banyak. Beginilah kita melakukannya di c# 3.0 dengan fitur auto implement property untuk hasil yg sama.

public class Mahasiswa
{
     public string NIM { get; set; }
     public string Name { get; set; }
     public long GPA { get; set; }
}

simple sekali bukan?

Categories: Uncategorized Tags: , ,