设为首页 收藏本站
查看: 722|回复: 0

[经验分享] solr code quick start

[复制链接]

尚未签到

发表于 2016-12-15 08:40:57 | 显示全部楼层 |阅读模式
  客户端项目依赖:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.petstore</groupId>
<artifactId>dtrace</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dtrace</name>
<dependencies>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-core</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</project>
  客户端测试代码:

package mytest;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.CoreContainer;
public class SolrTest {
public static void test1() throws Exception {
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace");
solrServer.setRequestWriter(new BinaryRequestWriter());
int size = 100;
long begin = System.currentTimeMillis();
for (int i = 0; i < size; ++i) {
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", i);
doc1.addField("rowkey", "100 p");
doc1.addField("cf", "[{30:50}]");
doc1.addField("timestamp", System.currentTimeMillis());
solrServer.add(doc1);
}
long end = System.currentTimeMillis();
System.out.println(" add cost:" + (end - begin) + "ms");
begin = System.currentTimeMillis();
solrServer.commit();
end = System.currentTimeMillis();
//System.out.println(" commit " + size + " cost:" + (end - begin) + " ms");
}
static ExecutorService service = Executors.newFixedThreadPool(20);
static CoreContainer container = new CoreContainer("/duitang/data/solr");
static {
container.load();
}
static EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, "dtrace");
public static void _test2(int round) throws Exception {
int count = 10000;
int size = count * round;
final CountDownLatch latch = new CountDownLatch(count);
final HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/dtrace");
solrServer.setRequestWriter(new BinaryRequestWriter());
long begin = System.currentTimeMillis();
for (int i = size - count; i < size; ++i) {
final int id = i;
service.submit(new Runnable() {
public void run() {
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", id);
doc1.addField("rowkey",
"12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890 12345678901234567890");
doc1.addField(
"cf",
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
doc1.addField("timestamp", System.currentTimeMillis());
try {
solrServer.add(doc1);
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
latch.await();
long end = System.currentTimeMillis();
System.out.println(" add[" + size + "] " + count + " cost:" + (end - begin) + "ms");
//begin = System.currentTimeMillis();
//solrServer.commit(false, false, true);
//solrServer.commit();
//end = System.currentTimeMillis();
//System.out.println(" commit " + size + " cost:" + (end - begin) + "ms");
//service.shutdown();
}
public static void test2() throws Exception {
ExecutorService service = Executors.newFixedThreadPool(1);
int count = 10000;
final CountDownLatch latch = new CountDownLatch(count);
long begin = System.currentTimeMillis();
for (int i = 7342; i <= count; ++i) {
final int index = i;
service.submit(new Runnable() {
public void run() {
try {
_test2(index);
latch.countDown();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
latch.await();
long end = System.currentTimeMillis();
System.out.println(" add finish " + count + " cost:" + (end - begin) + "ms");
}
public static void test3() throws Exception {
//CoreContainer container = new CoreContainer("/duitang/data/solr");
//container.load();
//EmbeddedSolrServer solrServer = new EmbeddedSolrServer(container, "dtrace");
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", 1);
doc1.addField("rowkey", "100 p");
doc1.addField("cf", "[{30:50}]");
doc1.addField("timestamp", System.currentTimeMillis());
solrServer.add(doc1);
//solrServer.commit();
System.out.println("ok");
}
public static void close() {
solrServer.shutdown();
}
public static void main(String[] args) throws Exception {
test2();
//close();
//RAMDirectory rdir = new RAMDirectory();
//String fileList[] = rdir.listAll();
//for (int i = 0; i < fileList.length; i++) {
//
//}
//FileSystem fs = FileSystem.get(null);
// fs.startLocalOutput(fsOutputFile, tmpLocalFile)
//FSDirectory.open(path)
//IndexWriter indexWriter = new IndexWriter();
}
}

  下面是通过SolrCore的方式启动Solr

SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr");
SolrResourceLoader.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr");
CoreContainer.Initializer init = new CoreContainer.Initializer();
CoreContainer cores = init.initialize();
String corename = "dtrace";
SolrCore core = cores.getCore(corename);
SolrConfig config = core.getSolrConfig();
SolrRequestParsers parser = new SolrRequestParsers(config);
String path = "/select";
SolrRequestHandler handler = core.getRequestHandler(path);
HttpServletRequest req = new HttpServletRequestMock();
parser.parse(core, path, req);
handler = core.getRequestHandler(null);
SolrQueryRequest solrReq = parser.parse(core, path, req);
SolrQueryResponse solrRsp = new SolrQueryResponse();
core.execute(handler, solrReq, solrRsp);
System.out.println(solrRsp.getValues());
System.out.println("================");
//OutputStreamWriter writer = new OutputStreamWriter(System.out);
//XMLWriter.writeResponse(writer, solrReq, solrRsp);
//writer.close();
OutputStreamWriter writer = new OutputStreamWriter(System.out);
JSONResponseWriter JSONResponseWriter = new JSONResponseWriter();
JSONResponseWriter.write(writer, solrReq, solrRsp);
writer.close();
System.exit(-1);

public class HttpServletRequestMock implements HttpServletRequest {
@Override
public Map getParameterMap() {
// TODO Auto-generated method stub
HashMap param = new HashMap();
param.put("q", new String[] { "*:*" });
return param;
}
@Override
public String getMethod() {
// TODO Auto-generated method stub
return "GET";
}
}
  下面是通过JettySolrRunner跑

package mytest;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.core.SolrResourceLoader;
import com.alimama.mdrill.solr.realtime.ShardPartion;
import com.alimama.mdrill.utils.IndexUtils;
public class MySolrRunTester {
public static void test1() throws Exception {
//SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/");
//SolrResourceLoader.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/");
SolrResourceLoader
.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-mdrill/src/main/resources/solr/");
SolrResourceLoader
.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-mdrill/src/main/resources/solr/");
//SolrResourceLoader.SetSolrHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/");
//SolrResourceLoader
//.SetSchemaHome("/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/");
//SolrConfig solrConfig = new SolrConfig(
//"/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/conf/solrconfig.xml");
//InputSource is = new InputSource(solrConfig.getResourceLoader().openSchema(
//"/duitang/dist/app/branches/mdrill/trunk/adhoc-core/solr/conf/schema.xml"));
ShardPartion.base = "/group/tbdp-etao-adhoc/p4padhoc/tabletest"; //必须的,如果不配置会有异常,SolrCore中会使用ShardPartion.getHdfsRealtimePath
ShardPartion.taskIndex = 1;
ShardPartion.index = IndexUtils.getHdfsForder(1);
//String hdfsconf = "/duitang/dist/sys/hadoop-1.2.1/conf/"; //必须的,如果不配置会有异常,ReadOnlyDirectory.getConf()
//HadoopUtil.setHdfsConfDir(hdfsconf);
JettySolrRunner jetty = new JettySolrRunner("/solr", 1210);
jetty.start();
while (true)
{
Thread.sleep(1000);
}
}
public static void main(String[] args) throws Exception {
test1();
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-314440-1-1.html 上篇帖子: solr学习资料收集 下篇帖子: Solr 全文索引详解(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表