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

[经验分享] Solr中SimplePostTool的修改

[复制链接]

尚未签到

发表于 2016-12-15 10:38:52 | 显示全部楼层 |阅读模式
  将源代码修改为传递字符串,去掉files以及stdin模式。

package com;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* @author Clay Zhong - Email: zjclay@gmail.com
* @date Sep 15, 2008
*/
public class SimplePostTool {
public static final String DEFAULT_POST_URL = "http://localhost:8080/solr/update";
public static final String POST_ENCODING = "UTF-8";
private static final String SOLR_OK_RESPONSE_EXCERPT = "<int name=\"status\">0</int>";
private static final boolean DEFAULT_COMMIT = true;
protected URL solrUrl;
public SimplePostTool(URL solrUrl) {
this.solrUrl = solrUrl;
}
private class PostException extends RuntimeException {
PostException(String reason, Throwable cause) {
super(reason + " (POST URL=" + solrUrl + ")", cause);
}
}
public static void main(String[] args) {
StringBuffer request = new StringBuffer("<add><doc>");
request.append("<field name=\"id\">F8V7067-APL-KIT</field>");
request.append("<field name=\"name\">Belkin Mobile Power Cord for iPod w/ Dock</field>");
request.append("<field name=\"manu\">Belkin</field>");
request.append("<field name=\"cat\">electronics</field>");
request.append("<field name=\"cat\">connector</field>");
request.append("<field name=\"features\">car power adapter, white</field>");
request.append("<field name=\"weight\">4</field>");
request.append("<field name=\"price\">19.95</field>");
request.append("<field name=\"popularity\">1</field>");
request.append("<field name=\"inStock\">false</field>");
request.append("</doc></add>");
URL url = null;
try {
url = new URL(DEFAULT_POST_URL);
}
catch (MalformedURLException e) {
fatal("System Property 'url' is not a valid URL: " + url);
}
final SimplePostTool simplePostTool = new SimplePostTool(url);
try {
if (request.length() > 0) {
info("Posting args to " + url);
final StringWriter writer = new StringWriter();
simplePostTool.postData(new StringReader(request.toString()), writer);
warnIfNotExpectedResponse(writer.toString(), SOLR_OK_RESPONSE_EXCERPT);
}
if (DEFAULT_COMMIT) {
info("Commiting Solr index changes..");
final StringWriter writer = new StringWriter();
simplePostTool.commit(writer);
warnIfNotExpectedResponse(writer.toString(), SOLR_OK_RESPONSE_EXCERPT);
}
}
catch (IOException ioe) {
fatal("Unexpected IOException " + ioe);
}
}
/**
* Does a simple commit operation
*/
public void commit(Writer output) throws IOException {
postData(new StringReader("<commit/>"), output);
}
/**
* Reads data from the data reader and posts it to solr, writes to the response to output
*/
public void postData(Reader data, Writer output) {
HttpURLConnection urlc = null;
try {
urlc = (HttpURLConnection) solrUrl.openConnection();
try {
urlc.setRequestMethod("POST");
}
catch (ProtocolException e) {
throw new PostException("HttpURLConnection doesn't support POST?", e);
}
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "text/xml; charset=" + POST_ENCODING);
OutputStream out = urlc.getOutputStream();
try {
Writer writer = new OutputStreamWriter(out, POST_ENCODING);
pipe(data, writer);
writer.close();
}
catch (IOException e) {
throw new PostException("IOException while posting data", e);
}
finally {
if (out != null) out.close();
}
InputStream in = urlc.getInputStream();
try {
Reader reader = new InputStreamReader(in);
pipe(reader, output);
reader.close();
}
catch (IOException e) {
throw new PostException("IOException while reading response", e);
}
finally {
if (in != null) in.close();
}
}
catch (IOException e) {
fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
}
finally {
if (urlc != null) urlc.disconnect();
}
}
/**
* Pipes everything from the reader to the writer via a buffer
*/
private static void pipe(Reader reader, Writer writer) throws IOException {
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
writer.write(buf, 0, read);
}
writer.flush();
}
/**
* Check what Solr replied to a POST, and complain if it's not what we expected. TODO: parse the
* response and check it XMLwise, here we just check it as an unparsed String
*/
static void warnIfNotExpectedResponse(String actual, String expected) {
if (actual.indexOf(expected) < 0) {
warn("Unexpected response from Solr: '" + actual + "' does not contain '" + expected
+ "'");
}
}
static void warn(String msg) {
System.err.println("SimplePostTool WARNING: " + msg);
}
static void info(String msg) {
System.out.println("SimplePostTool: " + msg);
}
static void fatal(String msg) {
System.err.println("SimplePostTool FATAL: " + msg);
System.exit(1);
}
}

运维网声明 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-314667-1-1.html 上篇帖子: solr cloud with zookeeper 下篇帖子: Solr新建集合节点配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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