private static Logger logger = Logger.getLogger(SolrUtils.class);
private static SolrUtils instance = null;//对外对象实例名称
/*单机的配置参数*/
private static LBHttpSolrServer enterpriseNewServer = null;//单机企业solr服务名称
private static String enterpriseNewSolrUrl = null;//单机企业solr服务名称
private static LBHttpSolrServer communityServer = null;//单机社区solr服务名称
private static String communitySolrUrl=null;//单机社区solr服务名称
private static LBHttpSolrServer merchantServer = null;//单机商户solr服务名称
private static String merchantSolrUrl=null;//单机商户solr服务名称
private static LBHttpSolrServer publicSentimentServer = null;//单机企业舆情solr服务名称
private static String publicSentimentSolrUrl=null;//单机企业舆情solr服务名称
private static boolean isOfClusterModel=false;//集群和单机的开关,true表示集群,false表示单机
/*集群的配置参数*/
private CloudSolrServer enterpriseNewClusterServer;///集群中企业solr的服务
private CloudSolrServer communityNewClusterServer;//集群中社区solr的服务
private CloudSolrServer merchantNewClusterServer;//集群中商户solr的服务
private CloudSolrServer publicSentimentClusterServer;//集群中企业舆情solr的服务
private static String enterpriseClusterCore=null;//集群中企业solr的core的实例
private static String communityClusterCore=null;//集群中社区solr的core的实例
private static String merchantClusterCore=null;//集群中商户solr的core的实例
private static String publicSentimentClusterCore=null;//企业舆情
private static String zkHost=null;//集群zookeeper host地址
private SolrUtils() {
}
public static SolrUtils getInstance() {
if (instance == null) {
// 给类加锁 防止线程并发
synchronized (SolrUtils.class) {
if (instance == null) {
instance = new SolrUtils();
}
}
}
return instance;
}
/**
* 获取基本solr core的连接地址
*/
static{
isOfClusterModel=PropertiesUtils.get("connection.solr.model").equals("true")?true:false;//单机和集群的切换方法
try {
if(isOfClusterModel){//获取集群模式
zkHost=PropertiesUtils.get("zookeeper.cluster.url");//zk地址
enterpriseClusterCore = PropertiesUtils.get("enterprisenew.solr.cluster.core");
communityClusterCore = PropertiesUtils.get("communitynew.solr.cluster.core");
merchantClusterCore = PropertiesUtils.get("merchantnew.solr.cluster.core");
publicSentimentClusterCore = PropertiesUtils.get("publicSentiment.solr.cluster.core");
logger.info("solr的连接方式为集群");
}
else{//获取单机模式
enterpriseNewSolrUrl = PropertiesUtils.get("enterprisenew.solr.url");
communitySolrUrl = PropertiesUtils.get("community.solr.url");
merchantSolrUrl = PropertiesUtils.get("merchant.solr.url");
publicSentimentSolrUrl = PropertiesUtils.get("publicSentiment.solr.url");
logger.info("solr的连接方式为单机");
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
*
获取企业solr core的服务器,分为:单机和集群两种模式
*/
public SolrServer getEnterpriseNewServer(){
if(isOfClusterModel){//获取集群模式
if(enterpriseNewClusterServer==null){
enterpriseNewClusterServer=new CloudSolrServer(zkHost+"/solr");
enterpriseNewClusterServer.setZkClientTimeout(10000000);
enterpriseNewClusterServer.setZkConnectTimeout(10000000);
enterpriseNewClusterServer.setDefaultCollection(enterpriseClusterCore);
}
return enterpriseNewClusterServer;
}
else{//获取单机模式
if(enterpriseNewServer==null){
try {
enterpriseNewServer=new LBHttpSolrServer(enterpriseNewSolrUrl.split(";"));
enterpriseNewServer.setConnectionTimeout(10000000);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return enterpriseNewServer;
}
}
/**
* 获取社区solr core的服务器,分为:单机和集群两种模式
*/
public SolrServer getCommunityServer(){
if(isOfClusterModel){
if(communityNewClusterServer==null){
communityNewClusterServer=new CloudSolrServer(zkHost+"/solr");
communityNewClusterServer.setZkClientTimeout(10000000);
communityNewClusterServer.setZkConnectTimeout(10000000);
communityNewClusterServer.setDefaultCollection(communityClusterCore);
}
return communityNewClusterServer;
}
else{
if(communityServer==null){
try {
System.out.println(communitySolrUrl);
communityServer=new LBHttpSolrServer(communitySolrUrl.split(";"));
communityServer.setConnectionTimeout(10000000);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
return communityServer;
}
/**
*
* 获取商户solr core的服务器,分为:单机和集群两种模式
*/
public SolrServer getMerchantServer(){
if(isOfClusterModel){
if(merchantNewClusterServer==null){
merchantNewClusterServer=new CloudSolrServer(zkHost+"/solr");
merchantNewClusterServer.setZkClientTimeout(10000000);
merchantNewClusterServer.setZkConnectTimeout(10000000);
merchantNewClusterServer.setDefaultCollection(merchantClusterCore);
}
return merchantNewClusterServer;
}
else{
if(merchantServer==null){
try {
merchantServer=new LBHttpSolrServer(merchantSolrUrl.split(";"));
merchantServer.setConnectionTimeout(10000000);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return merchantServer;
}
}
/**
*
* 获取企业舆情solr core的服务器,分为:单机和集群两种模式
*/
public SolrServer getPublicSentimentServer(){
if(isOfClusterModel){
if(publicSentimentClusterServer==null){
publicSentimentClusterServer=new CloudSolrServer(zkHost+"/solr");
publicSentimentClusterServer.setZkClientTimeout(10000000);
publicSentimentClusterServer.setZkConnectTimeout(10000000);
publicSentimentClusterServer.setDefaultCollection(publicSentimentClusterCore);
}
return publicSentimentClusterServer;
}
else{
if(publicSentimentServer==null){
try {
publicSentimentServer=new LBHttpSolrServer(publicSentimentSolrUrl.split(";"));
publicSentimentServer.setConnectionTimeout(10000000);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
return publicSentimentServer;
}
}
/**
* 向指定Solr地址批量添加数据
* @param solrUrl
* @param docs
* @throws Exception
*/
@SuppressWarnings("unchecked")
public void addAll(SolrServer solr,Collection extends Object> objs) throws Exception {
if(objs == null){
throw new RuntimeException("Object collection can not be null!");
}
if(objs.size() == 0){
return;
}
//LBHttpSolrServer solr = getSolrServer(solrUrl);
if(objs.iterator().next() instanceof SolrInputDocument){
solr.add((Collection
)objs);
} else {
solr.addBeans(objs);
}
solr.commit();
}
public static void main(String[] args) {
queryInfo();
}
public static void queryInfo(){
String sql="*:*";
// 拼接solr的查询条件
SolrQuery query = new SolrQuery();
query.setQuery(sql);
query.setStart(0);
query.setRows(10);
QueryResponse rsp=null;
int totalRecord=0;
try {
SolrServer ss =SolrUtils.getInstance().getPublicSentimentServer();
rsp=ss.query(query);
SolrDocumentList solrList = rsp.getResults();
totalRecord=(int)solrList.getNumFound();
if(solrList!=null&&solrList.size()>0){
for (SolrDocument solrDocument : solrList) {
String id=solrDocument.getFieldValue("plaintiffName").toString();//
System.out.println(id);
}
}
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 根据 指定查询条件从Solr中查询数据,并以SolrQueryResult对象的形式返回,其中包含List对象和totalCount
* @param solrUrl
* @param query
* @param returnClass 返回的List集合的泛型
* @return
* @throws Exception
*/
public SolrQueryResult queryAndGetSolrQueryResult(SolrServer solr, SolrQuery query, Class returnClass) throws Exception {
SolrQueryResult result = new SolrQueryResult();
if(query == null){
throw new RuntimeException("SolrQuery object can not be null!");
}
if(returnClass == null){
throw new RuntimeException("Return class can not be null!");
}
QueryResponse resp = solr.query(query);
List resultList = resp.getBeans(returnClass);
long totalCount = resp.getResults().getNumFound();
result.setResultList(resultList);
result.setTotalCount(totalCount);
return result;
}
具体文件:见百度网盘solr文件夹