示例#1
0
  /* (non-Javadoc)
   * @see com.mozilla.bagheera.hazelcast.persistence.MapStoreBase#init(com.hazelcast.core.HazelcastInstance, java.util.Properties, java.lang.String)
   */
  @Override
  public void init(HazelcastInstance hazelcastInstance, Properties properties, String mapName) {
    super.init(hazelcastInstance, properties, mapName);

    Configuration conf = HBaseConfiguration.create();
    for (String name : properties.stringPropertyNames()) {
      if (name.startsWith("hbase.")
          || name.startsWith("hadoop.")
          || name.startsWith("zookeeper.")) {
        conf.set(name, properties.getProperty(name));
      }
    }

    prefixDate =
        Boolean.parseBoolean(properties.getProperty("hazelcast.hbase.key.prefix.date", "false"));
    int hbasePoolSize = Integer.parseInt(properties.getProperty("hazelcast.hbase.pool.size", "10"));
    tableName = Bytes.toBytes(properties.getProperty("hazelcast.hbase.table", mapName));
    family = Bytes.toBytes(properties.getProperty("hazelcast.hbase.column.family", "data"));
    qualifier = Bytes.toBytes(properties.getProperty("hazelcast.hbase.column.qualifier", "json"));

    pool = new HTablePool(conf, hbasePoolSize);

    try {
      HBaseAdmin hbaseAdmin = new HBaseAdmin(conf);
      if (!hbaseAdmin.tableExists(tableName)) {
        HTableDescriptor desc = new HTableDescriptor(tableName);
        HColumnDescriptor columnDesc = new HColumnDescriptor(family);
        columnDesc.setCompressionType(Algorithm.LZO);
        columnDesc.setBlockCacheEnabled(true);
        columnDesc.setBlocksize(65536);
        columnDesc.setInMemory(false);
        columnDesc.setMaxVersions(1);
        columnDesc.setTimeToLive(Integer.MAX_VALUE);
        desc.addFamily(columnDesc);
        hbaseAdmin.createTable(desc);
      }
    } catch (Exception e) {
      throw new RuntimeException("Error creating table!", e);
    }

    // register with MapStoreRepository
    MapStoreRepository.addMapStore(mapName, this);
  }
  public static void createAndConfigBaseTable(
      Configuration conf, byte[] tableName, byte[] columnFamily, String[] indexedColumnNames)
      throws IOException {
    // create a table with column familiy columnFamily
    HBaseAdmin admin = new HBaseAdmin(conf);
    HTableDescriptor desc = new HTableDescriptor(tableName);
    // specify indexable columns.
    for (int i = 0; i < indexedColumnNames.length; i++) {
      desc.setValue(
          INDEX_INDICATOR + (i + 1),
          Bytes.toString(columnFamily) + INDEX_DELIMITOR + indexedColumnNames[i]);
    }
    HColumnDescriptor descColFamily = new HColumnDescriptor(columnFamily);
    // configure to set KEEP_DELETED_CELLS => 'true'
    descColFamily.setKeepDeletedCells(true);
    descColFamily.setTimeToLive(HConstants.FOREVER);
    descColFamily.setMaxVersions(Integer.MAX_VALUE);

    desc.addFamily(descColFamily);
    admin.createTable(desc);
  }