Java tutorial
/* * Copyright 2013 Jive Software, Inc * * Licensed 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 com.jivesoftware.os.rcvs.hbase098; import com.jivesoftware.os.rcvs.api.RowColumnValueStore; import com.jivesoftware.os.rcvs.api.RowColumnValueStoreInitializer; import com.jivesoftware.os.rcvs.api.RowColumnValueStoreMarshaller; import com.jivesoftware.os.rcvs.api.timestamper.Timestamper; import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.apache.hadoop.hbase.client.HConnectionManager; import org.merlin.config.Config; import org.merlin.config.defaults.IntDefault; import org.merlin.config.defaults.StringDefault; public class HBaseRowColumnValueStoreInitializer implements RowColumnValueStoreInitializer<Exception> { static public interface HBase098RowColumnValueStoreConfig extends Config { @StringDefault("unspecifiedHBaseZookeeperQuorum") public String getHBaseZookeeperQuorum(); public void setHBaseZookeeperQuorum(String hbaseZookeeperQuorum); @IntDefault(2181) public Integer getHBaseZookeeperPort(); public void setHBaseZookeeperPort(Integer hbaseZookeeperPort); @IntDefault(24) public int getMarshalThreadPoolSize(); public void setMarshalThreadPoolSize(int marshalThreadPoolSize); } private final org.apache.hadoop.conf.Configuration hbaseConfig; private final ExecutorService marshalExecutor; public HBaseRowColumnValueStoreInitializer(HBase098RowColumnValueStoreConfig config) { this(config, defaultHbaseConfig(config)); } public HBaseRowColumnValueStoreInitializer(HBase098RowColumnValueStoreConfig config, org.apache.hadoop.conf.Configuration hbaseConfig) { this.hbaseConfig = hbaseConfig; this.marshalExecutor = Executors.newFixedThreadPool(config.getMarshalThreadPoolSize()); } private static org.apache.hadoop.conf.Configuration defaultHbaseConfig( HBase098RowColumnValueStoreConfig config) { org.apache.hadoop.conf.Configuration hbaseConfig = new org.apache.hadoop.conf.Configuration(); hbaseConfig.clear(); hbaseConfig.set("hbase.zookeeper.quorum", config.getHBaseZookeeperQuorum()); hbaseConfig.set("hbase.zookeeper.property.clientPort", String.valueOf(config.getHBaseZookeeperPort())); return hbaseConfig; } @Override public <T, R, C, V> RowColumnValueStore<T, R, C, V, Exception> initialize(String tableNameSpace, String tableName, String columnFamilyName, RowColumnValueStoreMarshaller<T, R, C, V> marshaller, Timestamper timestamper) throws IOException { HBaseTableConfiguration hBaseTableConfiguration = new HBaseTableConfiguration(hbaseConfig, tableNameSpace, tableName, columnFamilyName); hBaseTableConfiguration.ensureTableProvisioned(true); return new HBaseRowColumnValueStore<>( HConnectionManager.createConnection(hbaseConfig, Executors.newCachedThreadPool()), hBaseTableConfiguration.getFinalName(), hBaseTableConfiguration.getColumnFamilyName(), marshaller, timestamper, marshalExecutor); } @Override public <T, R, C, V> RowColumnValueStore<T, R, C, V, Exception> initialize(String tableNameSpace, String tableName, String columnFamilyName, String[] additionalColumnFamilies, RowColumnValueStoreMarshaller<T, R, C, V> marshaller, Timestamper timestamper) throws IOException { String[] columnFamilies = new String[additionalColumnFamilies.length + 1]; columnFamilies[0] = columnFamilyName; for (int i = 0; i < additionalColumnFamilies.length; i++) { columnFamilies[i + 1] = additionalColumnFamilies[i]; } HBaseTableConfiguration hBaseTableConfiguration = new HBaseTableConfiguration(hbaseConfig, tableNameSpace, tableName, columnFamilies); hBaseTableConfiguration.ensureTableProvisioned(true, true); return new HBaseRowColumnValueStore<>( HConnectionManager.createConnection(hbaseConfig, Executors.newCachedThreadPool()), hBaseTableConfiguration.getFinalName(), hBaseTableConfiguration.getColumnFamilyName(), marshaller, timestamper, marshalExecutor); } @Override public <T, R, C, V> RowColumnValueStore<T, R, C, V, Exception> initialize(String tableNameSpace, String tableName, String columnFamilyName, int ttlInSeconds, int minVersions, int maxVersions, RowColumnValueStoreMarshaller<T, R, C, V> marshaller, Timestamper timestamper) throws IOException { HBaseTableConfiguration hBaseTableConfiguration = new HBaseTableConfiguration(hbaseConfig, tableNameSpace, tableName, columnFamilyName, ttlInSeconds, minVersions, maxVersions); hBaseTableConfiguration.ensureTableProvisioned(true); return new HBaseRowColumnValueStore<>( HConnectionManager.createConnection(hbaseConfig, Executors.newCachedThreadPool()), hBaseTableConfiguration.getFinalName(), hBaseTableConfiguration.getColumnFamilyName(), marshaller, timestamper, marshalExecutor); } }