public class DataImportTest {
private HttpSolrServer httpSolrServer;
private static final ObjectMapper MAPPER = new ObjectMapper();
@Before
public void setUp() throws Exception {
// 在url中指定core名称:taotao
//"http://solr.taotao.com/#/taotao"--界面操作
String url = "http://solr.taotao.com/taotao";//服务地址
HttpSolrServer httpSolrServer = new HttpSolrServer(url); //定义solr的server
httpSolrServer.setParser(new XMLResponseParser()); // 设置响应解析器
httpSolrServer.setMaxRetries(1); // 设置重试次数,推荐设置为1
httpSolrServer.setConnectionTimeout(500); // 建立连接的最长时间
this.httpSolrServer = httpSolrServer;
}
@Test
public void testData() throws Exception{
String url = "http://manage.taotao.com/rest/item?page={page}&rows=100";
int page = 1;
int pageSize=0;
do {
String u = StringUtils.replace(url, "{page}", ""+page);
String jsonData =doGet(u);
JsonNode jsonNode = MAPPER.readTree(jsonData);
String rowsStr = jsonNode.get("rows").toString();
List<Item> items=MAPPER.readValue(rowsStr,
MAPPER.getTypeFactory().constructCollectionType(List.class, Item.class));
pageSize=items.size();
httpSolrServer.addBeans(items);
this.httpSolrServer.commit();
page++;
} while (pageSize == 100);
}
private String doGet(String url) throws Exception{
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建http GET请求
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
return null;
}
}
1.可以先参考下:http://www.mamicode.com/info-detail-1774772.html
2.这个如果不是公司项目的话,可以发下源码调一下,不然完全不知道怎么走。
如果只是查询导入的数据报错,你可以先改成Integer,然后清空之前导入的数据,重新导入一遍在试试查询.