From 312281ca1bfe08fc62fb0fba9b4cce4bfd5f42f7 Mon Sep 17 00:00:00 2001 From: fmoga Date: Sat, 18 Jun 2011 14:32:14 +0000 Subject: TUSCANY-3522: Apply HBase-API.patch submitted by Eranda git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1137192 13f79535-47bb-0310-9956-ffa450edef68 --- collaboration/GSoC-2011-Eranda/hbase/pom.xml | 56 +++++++++++++ .../apache/tuscany/nosqldatastore/Database.java | 26 ++++++ .../org/apache/tuscany/nosqldatastore/Group.java | 27 +++++++ .../org/apache/tuscany/nosqldatastore/Session.java | 26 ++++++ .../tuscany/nosqldatastore/SessionFactory.java | 36 +++++++++ .../nosqldatastore/hbase/HBaseDatatabase.java | 92 ++++++++++++++++++++++ .../tuscany/nosqldatastore/hbase/HBaseGroup.java | 81 +++++++++++++++++++ .../tuscany/nosqldatastore/hbase/HBaseSession.java | 68 ++++++++++++++++ .../tuscany/nosqldatastore/hbase/HBaseTest.java | 48 +++++++++++ 9 files changed, 460 insertions(+) create mode 100644 collaboration/GSoC-2011-Eranda/hbase/pom.xml create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java create mode 100644 collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java (limited to 'collaboration') diff --git a/collaboration/GSoC-2011-Eranda/hbase/pom.xml b/collaboration/GSoC-2011-Eranda/hbase/pom.xml new file mode 100644 index 0000000000..fb454ef78a --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/pom.xml @@ -0,0 +1,56 @@ + + + + 4.0.0 + org.apache.tuscany + nosql-datastore-hbase + 0.0.1 + + jar + tuscany + http://maven.apache.org + + UTF-8 + + + + + org.apache.hbase + hbase + 0.90.3 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + + + + diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java new file mode 100644 index 0000000000..ad80b06f04 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Database.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore; + +public interface Database { + Group createGroup(String groupId); + Group getGroup(String groupId); + void deleteGroup(String groupId); +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java new file mode 100644 index 0000000000..634ab59cf0 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Group.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore; + +public interface Group { + void addEntry(String key, Object value); + void updateEntry(String key, Object value); + void deleteEntry(String key); + Object getValue(String key); +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java new file mode 100644 index 0000000000..5aa9641715 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/Session.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore; + +public interface Session { + Database createDatabase(String databaseName); + Database getDatabase(String databaseName); + void deleteDatabase(String databaseName); +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java new file mode 100644 index 0000000000..bc645bc118 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/SessionFactory.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore; + +import org.apache.tuscany.nosqldatastore.hbase.HBaseSession; + +public class SessionFactory { + + private static org.apache.tuscany.nosqldatastore.Session session; + + private SessionFactory(){ + } + + public static Session getHBaseSession(){ + session = new HBaseSession("hbase"); + return session; + } + +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java new file mode 100644 index 0000000000..5d49fd76da --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseDatatabase.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore.hbase; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.*; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.hadoop.hbase.client.HTable; +import org.apache.tuscany.nosqldatastore.Database; +import org.apache.tuscany.nosqldatastore.Group; + +import java.io.IOException; + +public class HBaseDatatabase implements Database{ + + private HTable table; + private HBaseAdmin admin; + + HBaseDatatabase(String dbname, HBaseAdmin admin){ + Configuration conf = HBaseConfiguration.create(); + try { + try{ + HTableDescriptor descriptor = new HTableDescriptor(dbname); + admin.createTable(descriptor); + } catch (MasterNotRunningException e) { + e.printStackTrace(); + } catch (ZooKeeperConnectionException e) { + e.printStackTrace(); + } catch (TableExistsException e){ + System.out.println("connected to existing database...."); + } finally { + table = new HTable(conf,dbname); + System.out.println(new String(table.getTableName())); + this.admin = admin; + table.flushCommits(); + } + }catch (IOException ex) { + ex.printStackTrace(); + } + + } + + public Group createGroup(String groupId) { + + HColumnDescriptor cf= new HColumnDescriptor(groupId.getBytes()); + + try { + admin.disableTable(table.getTableName()); + admin.addColumn(table.getTableName(),cf); + admin.enableTable(table.getTableName()); + } catch (IOException e) { + System.out.println("Error: Creating the group "+groupId); + e.printStackTrace(); + } + + Group group = new HBaseGroup(table, groupId); + return group; + } + + public Group getGroup(String groupId) { + Group group = new HBaseGroup(table, groupId); + return group; + } + + public void deleteGroup(String groupId) { + try { + admin.disableTable(table.getTableName()); + admin.deleteColumn(table.getTableName(),groupId.getBytes()); + admin.enableTable(table.getTableName()); + } catch (IOException e) { + System.out.println("Error: Deleting the group "+groupId); + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java new file mode 100644 index 0000000000..f57b70ee60 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseGroup.java @@ -0,0 +1,81 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore.hbase; + +import org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.client.*; +import org.apache.tuscany.nosqldatastore.Group; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class HBaseGroup implements Group { + + private HTable table; + private String cf; + + HBaseGroup(HTable table, String cf){ + this.table = table; + this.cf = cf; + } + + public void addEntry(String key, Object value) { + Put put = new Put(key.getBytes()); + put.add(cf.getBytes(),key.getBytes(),((String)value).getBytes()); + try { + table.put(put); + table.flushCommits(); + } catch (IOException e) { + System.out.println("Error: Adding the entry for the Key "+key); + e.printStackTrace(); + } + + } + + public void updateEntry(String key, Object value) { + addEntry(key,value); + } + + public void deleteEntry(String key) { + Delete delete = new Delete(key.getBytes()); + delete.deleteColumn(cf.getBytes(), key.getBytes()); + try { + table.delete(delete); + table.flushCommits(); + } catch (IOException e) { + System.out.println("Error: Deleting the entry for the Key "+key); + e.printStackTrace(); + } + } + + public Object getValue(String key) { + Result result = null; + Get get = new Get(key.getBytes()); + try { + result = table.get(get); + } catch (IOException e) { + System.out.println("Error: Retrieving the value or the Key "+key); + e.printStackTrace(); + } + return new String(result.getValue(cf.getBytes(),key.getBytes())); + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java new file mode 100644 index 0000000000..83648eadc0 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/main/java/org/apache/tuscany/nosqldatastore/hbase/HBaseSession.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tuscany.nosqldatastore.hbase; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.MasterNotRunningException; +import org.apache.hadoop.hbase.ZooKeeperConnectionException; +import org.apache.hadoop.hbase.client.HBaseAdmin; +import org.apache.tuscany.nosqldatastore.Database; +import org.apache.tuscany.nosqldatastore.Session; + +import java.io.IOException; + +public class HBaseSession implements Session{ + + private String session; + private HBaseAdmin admin; + + public HBaseSession(String session){ + this.session = session; + Configuration conf = HBaseConfiguration.create(); + try { + admin = new HBaseAdmin(conf); + } catch (MasterNotRunningException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } catch (ZooKeeperConnectionException e) { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + public Database createDatabase(String databaseName) { + Database database = new HBaseDatatabase(databaseName, admin); + return database; + } + + public Database getDatabase(String databaseName) { + Database database = new HBaseDatatabase(databaseName, admin); + return database; + } + + public void deleteDatabase(String databaseName) { + try { + admin.disableTable(databaseName); + admin.deleteTable(databaseName); + } catch (IOException e) { + System.out.println("Error: Creating the database "+databaseName); + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } +} diff --git a/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java b/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java new file mode 100644 index 0000000000..1d51187176 --- /dev/null +++ b/collaboration/GSoC-2011-Eranda/hbase/src/test/java/org/apache/tuscany/nosqldatastore/hbase/HBaseTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.nosqldatastore.hbase; + +import org.apache.tuscany.nosqldatastore.Database; +import org.apache.tuscany.nosqldatastore.Group; +import org.apache.tuscany.nosqldatastore.Session; +import org.apache.tuscany.nosqldatastore.SessionFactory; + +public class HBaseTest { + + Session session; + + private HBaseTest(){ + session = SessionFactory.getHBaseSession(); + } + + public static void main(String[] args){ + HBaseTest test = new HBaseTest(); + test.start(); + } + + public void start(){ + Database database = session.createDatabase("SampleDB"); + Group group = database.createGroup("group1"); + group.addEntry("eranda", "Eranda Sooriyabandara"); + System.out.println(group.getValue("eranda")); + group.deleteEntry("eranda"); + database.deleteGroup("group1"); + session.deleteDatabase("SampleDB"); + } +} -- cgit v1.2.3