All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hydraql.thrift.HBaseThriftTemplate Maven / Gradle / Ivy

/**
 * 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 com.hydraql.thrift;

import com.hydraql.common.callback.RowMapper;
import com.hydraql.common.model.data.HBaseRowData;
import com.hydraql.common.model.data.HBaseRowDataWithMultiVersions;
import com.hydraql.common.query.GetRowParam;
import com.hydraql.common.query.GetRowsParam;
import com.hydraql.common.query.ScanParams;

import java.util.List;
import java.util.Map;

/**
 * @author leojie 2020/12/27 11:41 下午
 */
public class HBaseThriftTemplate implements IHBaseThriftOperations {

  private final HBaseThriftPool pool;

  public HBaseThriftTemplate(String host, int port) {
    HBaseThriftPoolConfig config = new HBaseThriftPoolConfig();
    pool = new HBaseThriftPool(config, host, port);
  }

  public HBaseThriftTemplate(String host, int port, int poolSize) {
    HBaseThriftPoolConfig config = new HBaseThriftPoolConfig();
    config.setMaxTotal(poolSize);
    config.setMaxIdle(poolSize);
    pool = new HBaseThriftPool(config, host, port);
  }

  public HBaseThriftTemplate(String host, int port, HBaseThriftPoolConfig config) {
    pool = new HBaseThriftPool(config, host, port);
  }

  /**
   * Clear active connection objects actively in connection pool if you need.
   */
  public void clearThriftPool() {
    pool.clearInternalPool();
  }

  @Override
  public void save(String tableName, String rowKey, Map data) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.save(tableName, rowKey, data);
    }
  }

  @Override
  public void saveBatch(String tableName, Map> data) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.saveBatch(tableName, data);
    }
  }

  @Override
  public  void save(T t) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.save(t);
    }
  }

  @Override
  public  void saveBatch(List list) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.saveBatch(list);
    }
  }

  @Override
  public  T getRow(GetRowParam getRowParam, Class clazz) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRow(getRowParam, clazz);
    }
  }

  @Override
  public  T getRow(String tableName, GetRowParam getRowParam, RowMapper rowMapper) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRow(tableName, getRowParam, rowMapper);
    }
  }

  @Override
  public HBaseRowData getRow(String tableName, GetRowParam getRowParam) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRow(tableName, getRowParam);
    }
  }

  @Override
  public  List getWithMultiVersions(GetRowParam getRowParam, Class clazz) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getWithMultiVersions(getRowParam, clazz);
    }
  }

  @Override
  public  List getWithMultiVersions(String tableName, GetRowParam getRowParam,
      RowMapper rowMapper) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getWithMultiVersions(tableName, getRowParam, rowMapper);
    }
  }

  @Override
  public HBaseRowDataWithMultiVersions getWithMultiVersions(String tableName,
      GetRowParam getRowParam) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getWithMultiVersions(tableName, getRowParam);
    }
  }

  @Override
  public  List getRows(GetRowsParam getRowsParam, Class clazz) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRows(getRowsParam, clazz);
    }
  }

  @Override
  public  List getRows(String tableName, GetRowsParam getRowsParams, RowMapper rowMapper) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRows(tableName, getRowsParams, rowMapper);
    }
  }

  @Override
  public List getRows(String tableName, GetRowsParam getRowsParam) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.getRows(tableName, getRowsParam);
    }
  }

  @Override
  public  List scan(ScanParams scanQueryParams, Class clazz) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.scan(scanQueryParams, clazz);
    }
  }

  @Override
  public  List scan(String tableName, ScanParams scanQueryParams, RowMapper rowMapper) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.scan(tableName, scanQueryParams, rowMapper);
    }
  }

  @Override
  public List scan(String tableName, ScanParams scanQueryParams) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      return hBaseThrift.scan(tableName, scanQueryParams);
    }
  }

  @Override
  public List scanWithMultiVersions(String tableName,
      ScanParams scanParams) {
    return null;
  }

  @Override
  public  void delete(T t) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.delete(t);
    }
  }

  @Override
  public void delete(String tableName, String rowKey) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.delete(tableName, rowKey);
    }
  }

  @Override
  public void delete(String tableName, String rowKey, String familyName) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.delete(tableName, rowKey, familyName);
    }
  }

  @Override
  public void delete(String tableName, String rowKey, String familyName, List qualifiers) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.delete(tableName, rowKey, familyName, qualifiers);
    }
  }

  @Override
  public void delete(String tableName, String rowKey, String familyName, String... qualifiers) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.delete(tableName, rowKey, familyName, qualifiers);
    }
  }

  @Override
  public  void deleteBatch(List list) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.deleteBatch(list);
    }
  }

  @Override
  public void deleteBatch(String tableName, List rowKeys) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.deleteBatch(tableName, rowKeys);
    }
  }

  @Override
  public void deleteBatch(String tableName, List rowKeys, String familyName) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.deleteBatch(tableName, rowKeys, familyName);
    }
  }

  @Override
  public void deleteBatch(String tableName, List rowKeys, String familyName,
      List qualifiers) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.deleteBatch(tableName, rowKeys, familyName, qualifiers);
    }
  }

  @Override
  public void deleteBatch(String tableName, List rowKeys, String familyName,
      String... qualifiers) {
    try (HBaseThrift hBaseThrift = pool.getResource()) {
      hBaseThrift.deleteBatch(tableName, rowKeys, familyName, qualifiers);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy