基础的solr工具类
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import java.util.Collection;
import java.util.List;
@Component
@ComponentScan
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class SolrJUtil {
@Autowired
private SolrClient solrClient;
/**
* 查询solr数据
*
* @param collection solr表名
* @param queryStr 查询条件
* @param startRow 开始条数
* @param pageSize 每页条数
* @param sortField 排序字段
* @param orderType 排序类型 1:升序,2:降序
* @return
* @throws Exception
*/
public QueryResponse getSolrInfo(String collection, String queryStr, Integer startRow, Integer pageSize,
String sortField, Integer orderType) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
SolrQuery query = new SolrQuery();
query.setQuery(queryStr);
query.setStart(startRow);
query.setRows(pageSize);
if (!StringUtils.isEmpty(sortField) && !ObjectUtils.isEmpty(orderType) && orderType.intValue() > 0) {
if (orderType.equals(1)) {
query.setSort(sortField, SolrQuery.ORDER.asc);
} else if (orderType.equals(2)) {
query.setSort(sortField, SolrQuery.ORDER.desc);
}
}
QueryResponse response = cloudSolrClient.query(collection, query);
return response;
}
/**
* 查询solr数据
*
* @param collection solr表名
* @param queryStr 查询条件
* @param startRow 开始条数
* @param pageSize 每页条数
* @param sortField 排序字段
* @param orderType 排序类型 1:升序,2:降序
* @param defType 查询权重排序
* @param mm 控制多词联合查询命中的数量
* @param fl 指定返回结果字段
* @return
* @throws Exception
*/
public QueryResponse getSolrInfo(String collection, String queryStr, Integer startRow, Integer pageSize,
String sortField, Integer orderType, String defType, String mm, String fl) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
SolrQuery query = new SolrQuery();
query.setQuery(queryStr);
query.setStart(startRow);
query.setRows(pageSize);
query.set("defType", defType);
query.set("mm", mm);
query.set("fl", fl);
if (!StringUtils.isEmpty(sortField) && !ObjectUtils.isEmpty(orderType) && orderType.intValue() > 0) {
if (orderType.equals(1)) {
query.setSort(sortField, SolrQuery.ORDER.asc);
} else if (orderType.equals(2)) {
query.setSort(sortField, SolrQuery.ORDER.desc);
}
}
QueryResponse response = cloudSolrClient.query(collection, query);
return response;
}
/**
* 查询solr数据返回List集合
*
* @param type 数据类型
* @param collection solr表名
* @param queryStr 查询条件
* @param startRow 开始条数
* @param pageSize 每页条数
* @param sortField 排序字段
* @param orderType 排序类型 1:升序,2:降序
* @param <T> 数据泛型
* @return
* @throws Exception
*/
public <T> List<T> getSolrInfo(Class<T> type, String collection, String queryStr, Integer startRow, Integer pageSize,
String sortField, Integer orderType) throws Exception {
QueryResponse response = getSolrInfo(collection, queryStr, startRow, pageSize, sortField, orderType);
return response.getBeans(type);
}
/**
* solr批量添加数据
*
* @param collection solr表名
* @param beans 数据对象集合
* @throws Exception
*/
public void addSolr(String collection, Collection<?> beans) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
cloudSolrClient.addBeans(collection, beans);
cloudSolrClient.commit(collection);
}
/**
* solr添加数据
*
* @param collection solr表名
* @param object 数据对象
* @throws Exception
*/
public void addSolr(String collection, Object object) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
cloudSolrClient.addBean(collection, object);
cloudSolrClient.commit(collection);
}
/**
* SolrInputDocument添加数据
*
* @param collection solr表名
* @param doc solr Document数据
* @throws Exception
*/
public void addSolr(String collection, SolrInputDocument doc) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
cloudSolrClient.add(collection, doc);
cloudSolrClient.commit(collection);
}
/**
* 删除solr中的数据
*
* @param collection solr表名
* @param queryStr 条件
* @throws Exception
*/
public void deleteSolrByQuery(String collection, String queryStr) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
cloudSolrClient.deleteByQuery(collection, queryStr);
cloudSolrClient.commit(collection);
}
/**
* 删除solr中的数据
*
* @param collection solr表名
* @param id 唯一编号
* @throws Exception
*/
public void deleteSolrById(String collection, String id) throws Exception {
CloudSolrClient cloudSolrClient = (CloudSolrClient) solrClient;
cloudSolrClient.deleteById(collection, id);
cloudSolrClient.commit(collection);
}
/**
* 清除全文检索中搜索语句中间多余的空格
*
* @param param 参数
* @return
*/
public String cleanSpace(String param) {
String trim = param.trim(); // 先清除字符串前后的空格
if (trim.length() == 0) {
return "*:*"; // 输入为空 默认查询全部
}
String[] split = trim.split(" ");// 按照"
// "切分字符串,如果中间有多个空格,那么切分后的数组中这些空格的长度为0
StringBuffer sb = new StringBuffer();
for (String string : split) {
// 排除长度为0的元素,也就是排除多余的空格
if (0 != string.length()) {
sb.append(string + " AND ");
}
}
// 截取搜索字符串
String query = sb.toString().substring(0, sb.toString().length() - 5);
return query;
}
}
Hbase工具类
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@ComponentScan
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class HBaseUtil {
private static final Logger log = LoggerFactory.getLogger(HBaseUtil.class);
@Autowired
private Connection connection;
/**
* 连接Hbase表
*
* @param tabName 表名
* @return
*/
public Table getTable(String tabName) {
TableName tableName = TableName.valueOf(tabName);
Table table = null;
try {
table = connection.getTable(tableName);
} catch (IOException e) {
log.error(e.getMessage());
}
return table;
}
/**
* 获取Hbase中所有表名
*
* @return
*/
public List<String> getAllTableNames() {
Admin admin = null;
try {
admin = connection.getAdmin();
} catch (IOException e) {
log.error(e.getMessage());
}
List<String> tableNameLists = new ArrayList<>();
try {
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
tableNameLists.add(tableName.getNameAsString());
}
} catch (IOException e) {
log.error("获取所有表的表名失败", e);
} finally {
close(admin, null, null);
}
return tableNameLists;
}
/**
* 指定表名、行键、列族、列添加数据
*
* @param tableName 表名
* @param rowKey 行键
* @param familyName 列族
* @param columns 列名
* @param values 列数据值
*/
public void putData(String tableName, String rowKey, String familyName, String[] columns, String[] values) {
Table table = null;
try {
table = getTable(tableName);
Put put = new Put(Bytes.toBytes(rowKey));
if (columns != null && values != null && columns.length == values.length) {
for (int i = 0; i < columns.length; i++) {
if (columns[i] != null && values[i] != null) {
put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columns[i]), Bytes.toBytes(values[i]));
} else {
throw new NullPointerException(MessageFormat.format(
"列名和列数据都不能为空,column:{0},value:{1}", columns[i], values[i]));
}
}
}
table.put(put);
log.debug("putData add or update data Success,rowKey:" + rowKey);
} catch (Exception e) {
log.error(MessageFormat.format("为表添加or更新数据失败,tableName:{0},rowKey:{1},familyName:{2}", tableName, rowKey, familyName), e);
} finally {
close(null, null, table);
}
}
/**
* 根据表名获取表中的所有信息
*
* @param tableName 表名
* @return
*/
public Map<String, Map<String, String>> getResultScanner(String tableName) {
Scan scan = new Scan();
Map<String, Map<String, String>> result = new HashMap<>();
ResultScanner rs = null;
// 获取表
Table table = null;
try {
table = getTable(tableName);
rs = table.getScanner(scan);
for (Result r : rs) {
// 每一行数据
Map<String, String> columnMap = new HashMap<>();
String rowKey = null;
// 行键,列族和列限定符一起确定一个单元(Cell)
for (Cell cell : r.listCells()) {
if (rowKey == null) {
rowKey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
}
columnMap.put(
// 列限定符(单列)
Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength()),
// 列族
Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
}
if (rowKey != null) {
result.put(rowKey, columnMap);
}
}
} catch (IOException e) {
log.error(MessageFormat.format("遍历查询指定表中的所有数据失败,tableName:{0}", tableName), e);
} finally {
close(null, rs, table);
}
return result;
}
/**
* 关闭连接
*
* @param admin 管理类
* @param rs 扫描的结果
* @param table 表类
*/
public void close(Admin admin, ResultScanner rs, Table table) {
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
log.error("关闭Admin失败", e);
}
if (rs != null) {
rs.close();
}
if (table != null) {
rs.close();
}
if (table != null) {
try {
table.close();
} catch (IOException e) {
log.error("关闭Table失败", e);
}
}
}
}
}
solr的Config配置
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@ComponentScan
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class SolrConfig {
@Value("${solr.zk-host}")
private String zkHost;
@Bean
public CloudSolrClient solrClient() {
CloudSolrClient.Builder builder = new CloudSolrClient.Builder();
builder.withZkHost(zkHost);
CloudSolrClient solrClient = builder.build();
return solrClient;
}
}
hbase的Config配置
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.jmx.support.RegistrationPolicy;
import java.io.IOException;
@Configuration
@ComponentScan
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class HbaseConfig {
private static final Logger log = LoggerFactory.getLogger(HbaseConfig.class);
@Value("${hbase.zookeeper.quorum}")
private String quorum;
@Value("${hbase.zookeeper.clientPort}")
private String clientPort;
/**
* 注入Hbase连接
*
* @return
* @throws Exception
*/
@Bean
public Connection connection() {
org.apache.hadoop.conf.Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", quorum);
conf.set("hbase.zookeeper.property.clientPort", clientPort);
conf.setInt("hbase.rpc.timeout", 10000);
conf.setInt("hbase.client.operation.timeout", 10000);
conf.setInt("hbase.client.scanner.timeout.period", 10000);
Connection connection = null;
try {
connection = ConnectionFactory.createConnection(conf);
} catch (Exception e) {
log.error(e.toString());
}
return connection;
}
}
使用:接口调用
controller层 (部分伪代码 ) solr当中进行sql拼接查询出符合条件的rowkey,详细信息去hbase当中查找
总数取solr当中的总数 SolrDocumentList.getNumFound()