Permalink
Browse files

init

  • Loading branch information...
0 parents commit 22e5076c4d85c9c76dcea84ca481296c144db4af @enesakar committed Apr 13, 2012
36 pom.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>hazelcast-root</artifactId>
+ <groupId>com.hazelcast</groupId>
+ <version>2.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>hazelcast_hbase</artifactId>
+
+ <dependencies>
+
+ <dependency>
+ <artifactId>hazelcast</artifactId>
+ <groupId>com.hazelcast</groupId>
+ <version>2.0.2</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.hbase</groupId>
+ <artifactId>hbase</artifactId>
+ <version>0.92.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.hadoop</groupId>
+ <artifactId>hadoop-core</artifactId>
+ <version>1.0.2</version>
+ </dependency>
+
+
+ </dependencies>
+
+</project>
@@ -0,0 +1,129 @@
+package com.hazelcast.hbase;
+
+import com.hazelcast.core.MapStore;
+import org.apache.hadoop.hbase.KeyValue;
+import org.apache.hadoop.hbase.client.*;
+import org.apache.hadoop.hbase.util.Bytes;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.logging.Logger;
+
+
+public class HBaseMapStore implements MapStore<String, User> {
+ private Logger logger;
+
+ public static void main(String[] args) {
+ HBaseMapStore mapstore = new HBaseMapStore();
+// User user = new User();
+// user.setAge(17);
+// user.setName("mehmet");
+// user.setLocation("istanbul");
+// user.setDetails("details details details");
+// mapstore.store("user-"+user.getName(), user );
+ System.out.println(mapstore.load("user-mehmet"));
+ }
+
+ @Override
+ public User load(String key) {
+ User user = new User();
+ HTable htable = HBaseService.getInstance().getHtable();
+ try {
+ Get get = new Get(Bytes.toBytes(key));
+ Result r = htable.get(get);
+ if(r.isEmpty())
+ return null;
+ byte[] bname = r.getValue(Bytes.toBytes("cf_basic"), Bytes.toBytes("name"));
+ byte[] blocation = r.getValue(Bytes.toBytes("cf_basic"), Bytes.toBytes("location"));
+ byte[] bage = r.getValue(Bytes.toBytes("cf_basic"), Bytes.toBytes("age"));
+ byte[] bdetails = r.getValue(Bytes.toBytes("cf_text"), Bytes.toBytes("details"));
+ user.setName(Bytes.toString(bname));
+ user.setLocation(Bytes.toString(blocation));
+ user.setDetails(Bytes.toString(bdetails));
+ user.setAge(Bytes.toInt(bage));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ return user;
+ }
+
+
+ @Override
+ public void store(String key, User user) {
+ HTable htable = HBaseService.getInstance().getHtable();
+ Put put = new Put(Bytes.toBytes(key));
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("name"), Bytes.toBytes(user.getName()));
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("age"), Bytes.toBytes(user.getAge()));
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("location"), Bytes.toBytes(user.getLocation()));
+ put.add(Bytes.toBytes("cf_text"), Bytes.toBytes("details"), Bytes.toBytes(user.getDetails()));
+ try {
+ htable.put(put);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void storeAll(Map<String, User> userMap) {
+ HTable htable = HBaseService.getInstance().getHtable();
+ List<Row> rlist = new LinkedList<Row>();
+ try {
+ for (String key : userMap.keySet()) {
+ Put put = new Put(Bytes.toBytes(key));
+ User user = userMap.get(key);
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("name"), Bytes.toBytes(user.getName()));
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("age"), Bytes.toBytes(user.getAge()));
+ put.add(Bytes.toBytes("cf_basic"), Bytes.toBytes("location"), Bytes.toBytes(user.getLocation()));
+ put.add(Bytes.toBytes("cf_text"), Bytes.toBytes("details"), Bytes.toBytes(user.getDetails()));
+ rlist.add(put);
+ }
+ htable.batch(rlist);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void delete(String key) {
+ HTable htable = HBaseService.getInstance().getHtable();
+ Delete delete = new Delete(Bytes.toBytes(key));
+ try {
+ htable.delete(delete);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Override
+ public void deleteAll(Collection<String> keys) {
+ HTable htable = HBaseService.getInstance().getHtable();
+ List<Row> rlist = new LinkedList<Row>();
+ try {
+ for (String key : keys) {
+ Delete delete = new Delete(Bytes.toBytes(key));
+ rlist.add(delete);
+ }
+ htable.batch(rlist);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ @Override
+ public Map<String, User> loadAll(Collection<String> strings) {
+ // will not be implemented
+ return null;
+ }
+
+ @Override
+ public Set<String> loadAllKeys() {
+ // will not be implemented
+ return null;
+ }
+}
@@ -0,0 +1,32 @@
+package com.hazelcast.hbase;
+
+import org.apache.hadoop.hbase.client.HTable;
+
+import java.io.IOException;
+
+
+public class HBaseService {
+ private static HBaseService ourInstance = new HBaseService();
+ private HTable htable;
+
+
+ public static HBaseService getInstance() {
+ return ourInstance;
+ }
+
+ public HTable getHtable() {
+ return htable;
+ }
+
+ public void setHtable(HTable htable) {
+ this.htable = htable;
+ }
+
+ private HBaseService() {
+ try {
+ htable = new HTable("user");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
@@ -0,0 +1,28 @@
+package com.hazelcast.hbase;
+
+import com.hazelcast.core.Hazelcast;
+import com.hazelcast.core.IMap;
+
+public class Main {
+ public static void main(String[] args) {
+ IMap<String, User> map = Hazelcast.getMap("map");
+ System.out.println( map.get("user-mehmet") );
+ User user = new User();
+ user.setName("Enes Akar");
+ user.setAge(29);
+ user.setLocation("Istanbul");
+ user.setDetails("software developer .....");
+ map.put("u-5",user);
+ User user2 = new User();
+ user2.setName("Mehmet Dogan");
+ user2.setAge(29);
+ user2.setLocation("Istanbul");
+ user2.setDetails("software developer .....");
+ map.put("u-6",user2);
+ System.out.println( map.get("u-5") );
+ System.out.println( map.get("u-6") );
+ map.remove("u-5");
+ System.out.println( map.get("u-5") );
+ }
+
+}
@@ -0,0 +1,53 @@
+package com.hazelcast.hbase;
+
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+ private String name;
+ private String location;
+ private Integer age;
+ private String details;
+
+ public String getDetails() {
+ return details;
+ }
+
+ public void setDetails(String details) {
+ this.details = details;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getLocation() {
+ return location;
+ }
+
+ public void setLocation(String location) {
+ this.location = location;
+ }
+
+ public Integer getAge() {
+ return age;
+ }
+
+ public void setAge(Integer age) {
+ this.age = age;
+ }
+
+ @Override
+ public String toString() {
+ return "User{" +
+ "name='" + name + '\'' +
+ ", location='" + location + '\'' +
+ ", age=" + age +
+ ", details='" + details + '\'' +
+ '}';
+ }
+}
Oops, something went wrong.

0 comments on commit 22e5076

Please sign in to comment.