diff options
author | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2011-06-18 14:32:14 +0000 |
---|---|---|
committer | fmoga <fmoga@13f79535-47bb-0310-9956-ffa450edef68> | 2011-06-18 14:32:14 +0000 |
commit | 312281ca1bfe08fc62fb0fba9b4cce4bfd5f42f7 (patch) | |
tree | 5e59500a09715d04bdf72b3dc98a3b247e5eab75 /collaboration/GSoC-2011-Eranda/hbase | |
parent | 2fd232ad1680d2164bfb445b87299742f8af347d (diff) |
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
Diffstat (limited to 'collaboration/GSoC-2011-Eranda/hbase')
9 files changed, 460 insertions, 0 deletions
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 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- +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. +--> +<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"> + <modelVersion>4.0.0</modelVersion> + <groupId>org.apache.tuscany</groupId> + <artifactId>nosql-datastore-hbase</artifactId> + <version>0.0.1</version> + + <packaging>jar</packaging> + <name>tuscany</name> + <url>http://maven.apache.org</url> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + + <dependencies> + <dependency> + <groupId>org.apache.hbase</groupId> + <artifactId>hbase</artifactId> + <version>0.90.3</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>2.0.2</version> + <configuration> + <source>1.5</source> + <target>1.5</target> + </configuration> + </plugin> + </plugins> + </build> +</project> 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"); + } +} |