下面列出了com.google.protobuf.TextFormat.ParseException#org.apache.solr.client.solrj.SolrServerException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected MCROAISolrResult solrQuery(Optional<String> cursor) throws SolrServerException, IOException {
SolrQuery query = getBaseQuery(CommonParams.Q);
// set support
if (this.set != null) {
String setId = this.set.getSetId();
MCROAISetConfiguration<SolrQuery, SolrDocument, String> setConfig = getSetManager().getConfig(setId);
setConfig.getHandler().apply(this.set, query);
}
// from & until
if (this.from != null || this.until != null) {
String fromUntilCondition = buildFromUntilCondition(this.from, this.until);
query.add(CommonParams.FQ, fromUntilCondition);
}
// cursor
query.set(CursorMarkParams.CURSOR_MARK_PARAM, cursor.orElse(CursorMarkParams.CURSOR_MARK_START));
query.set(CommonParams.ROWS, String.valueOf(getPartitionSize()));
query.set(CommonParams.SORT, "id asc");
// do the query
SolrClient solrClient = MCRSolrClientFactory.getMainSolrClient();
QueryResponse response = solrClient.query(query);
Collection<MCROAISetResolver<String, SolrDocument>> setResolver = getSetResolver(response.getResults());
return new MCROAISolrResult(response, d -> toHeader(d, setResolver));
}
protected void indexJATEDocuments(Path file, EmbeddedSolrServer solrServer, JATEProperties jateProp, boolean commit) throws JATEException {
if (file == null) {
return;
}
try {
JATEDocument jateDocument = JATEUtil.loadJATEDocument(file);
if (isNotEmpty(jateDocument))
JATEUtil.addNewDoc(solrServer, jateDocument.getId(),
jateDocument.getId(), jateDocument.getContent(), jateProp, commit);
} catch (FileNotFoundException ffe) {
throw new JATEException(ffe.toString());
} catch (IOException ioe) {
throw new JATEException(String.format("failed to index [%s]", file.toString()) + ioe.toString());
} catch (SolrServerException sse) {
throw new JATEException(String.format("failed to index [%s] ", file.toString()) + sse.toString());
}
}
private void initialStream(SolrQuery query, SolrClient connection) {
try {
SolrQuery solrQuery = query.getCopy();
solrQuery.setRows(nrOfTimeSeriesPerBatch);
solrQuery.setStart(currentDocumentCount);
solrStreamingHandler.init(nrOfTimeSeriesPerBatch, currentDocumentCount);
QueryResponse response = connection.queryAndStreamResponse(solrQuery, solrStreamingHandler);
nrOfAvailableTimeSeries = response.getResults().getNumFound();
queryStart = 0;//(long) response.getResponseHeader().get(ChronixSolrStorageConstants.QUERY_START_LONG);
queryEnd = Long.MAX_VALUE;//(long) response.getResponseHeader().get(ChronixSolrStorageConstants.QUERY_END_LONG);
needStream = false;
} catch (SolrServerException | IOException e) {
LOGGER.error("SolrServerException occurred while querying server.", e);
}
}
@Override
protected void setup(Context context) throws IOException, InterruptedException {
verifyPartitionAssignment(context);
SolrRecordWriter.addReducerContext(context);
/*
* Note that ReflectionUtils.newInstance() above also implicitly calls
* resolver.configure(context.getConfiguration()) if the resolver
* implements org.apache.hadoop.conf.Configurable
*/
this.exceptionHandler = new FaultTolerance(
context.getConfiguration().getBoolean(FaultTolerance.IS_PRODUCTION_MODE, false),
context.getConfiguration().getBoolean(FaultTolerance.IS_IGNORING_RECOVERABLE_EXCEPTIONS, false),
context.getConfiguration().get(FaultTolerance.RECOVERABLE_EXCEPTION_CLASSES, SolrServerException.class.getName()));
this.heartBeater = new HeartBeater(context);
}
private SolrInputDocument fetchExistingOrCreateNewSolrDoc(String id) throws SolrServerException, IOException {
Map<String, String> p = new HashMap<String, String>();
p.put("q", PHRASE + ":\"" + ClientUtils.escapeQueryChars(id) + "\"");
SolrParams params = new MapSolrParams(p);
QueryResponse res = solrAC.query(params);
if (res.getResults().size() == 0) {
return new SolrInputDocument();
} else if (res.getResults().size() == 1) {
SolrDocument doc = res.getResults().get(0);
SolrInputDocument tmp = new SolrInputDocument();
for (String fieldName : doc.getFieldNames()) {
tmp.addField(fieldName, doc.getFieldValue(fieldName));
}
return tmp;
} else {
throw new IllegalStateException("Query with params : " + p + " returned more than 1 hit!");
}
}
protected List<Map<String, Object>> getIndexFields(String index)
throws IOException, SolrServerException {
List<Map<String, Object>> indexFields = new ArrayList<>();
// Get all the fields in use, including dynamic fields
LukeRequest lukeRequest = new LukeRequest();
LukeResponse lukeResponse = lukeRequest.process(client, index);
for (Entry<String, LukeResponse.FieldInfo> field : lukeResponse.getFieldInfo().entrySet()) {
Map<String, Object> fieldData = new HashMap<>();
fieldData.put("name", field.getValue().getName());
fieldData.put("type", field.getValue().getType());
indexFields.add(fieldData);
}
// Get all the schema fields
SchemaRepresentation schemaRepresentation = new SchemaRequest().process(client, index)
.getSchemaRepresentation();
indexFields.addAll(schemaRepresentation.getFields());
return indexFields;
}
@Override
public void run() {
while (!finished) {
if (rand.nextBoolean()) {
doc.setField(authField, authFieldValue0);
} else {
doc.setField(authField, authFieldValue1);
}
try {
server.add(doc);
} catch (SolrServerException sse) {
throw new RuntimeException(sse);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
}
private boolean waitForRuleToExpire(String collection, String shard, String splitKey, long finishTime) throws KeeperException, InterruptedException, SolrServerException, IOException {
DocCollection state;
Slice slice;
boolean ruleRemoved = false;
long expiryTime = finishTime + TimeUnit.NANOSECONDS.convert(60, TimeUnit.SECONDS);
while (System.nanoTime() < expiryTime) {
cluster.getSolrClient().getZkStateReader().forceUpdateCollection(collection);
state = getCollectionState(collection);
slice = state.getSlice(shard);
Map<String,RoutingRule> routingRules = slice.getRoutingRules();
if (routingRules == null || routingRules.isEmpty() || !routingRules.containsKey(splitKey)) {
ruleRemoved = true;
break;
}
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", splitKey + random().nextInt());
cluster.getSolrClient().add(collection, doc);
Thread.sleep(1000);
}
return ruleRemoved;
}
public void indexMap(String id, Map<String, List<String>> objectMap)
throws IOException, SolrServerException {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", (trimId ? id.trim() : id));
for (Map.Entry<String, List<String>> entry : objectMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value != null) {
if (!key.endsWith("_sni") && !key.endsWith("_ss"))
key += "_ss";
document.addField(key, value);
}
}
try {
UpdateResponse response = solr.add(document);
} catch (HttpSolrClient.RemoteSolrException ex) {
System.err.printf("document: %s", document);
System.err.printf("Commit exception: %s%n", ex.getMessage());
}
}
@Test
public void testRedirect() throws Exception {
final String clientUrl = jetty.getBaseUrl().toString() + "/redirect/foo";
try (HttpSolrClient client = getHttpSolrClient(clientUrl)) {
SolrQuery q = new SolrQuery("*:*");
// default = false
SolrServerException e = expectThrows(SolrServerException.class, () -> client.query(q));
assertTrue(e.getMessage().contains("redirect"));
client.setFollowRedirects(true);
client.query(q);
//And back again:
client.setFollowRedirects(false);
e = expectThrows(SolrServerException.class, () -> client.query(q));
assertTrue(e.getMessage().contains("redirect"));
}
}
/**
* Has the same behavior as {@link SolrClient#deleteByQuery(String)}.
*
* @param deleteQuery delete query to be executed
*/
@Override
public void deleteByQuery(String deleteQuery) throws SolrServerException, IOException {
try {
solrServer.deleteByQuery(deleteQuery);
} catch (SolrException e) {
if (isDocumentIssue(e)) {
documentDeleteErrorMeter.mark(1);
} else {
solrDeleteErrorMeter.mark(1);
throw e;
}
} catch (SolrServerException sse) {
solrDeleteErrorMeter.mark(1);
throw sse;
}
}
@Test
public void validate_indexing() {
ModifiableSolrParams params = new ModifiableSolrParams();
params.set("q", "*:*");
try {
QueryResponse qResp = server.query(params);
SolrDocumentList docList = qResp.getResults();
assert (docList.getNumFound() == 2000);
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
@Override
public OntologyEntryBean findOntologyEntryByUri(String uri) throws SearchEngineException {
OntologyEntryBean retVal = null;
try {
SolrQuery query = new SolrQuery(uri);
query.setRequestHandler(config.getOntologyNodeRequestHandler());
QueryResponse response = server.query(query);
List<OntologyEntryBean> annotations = response.getBeans(OntologyEntryBean.class);
if (annotations.size() > 0) {
retVal = annotations.get(0);
}
} catch (SolrServerException e) {
throw new SearchEngineException(e);
}
return retVal;
}
@Before
public void init() throws IOException, SolrServerException {
MockitoAnnotations.initMocks(this);
when(solrClient.ping()).thenReturn(solrPingResponse);
when(solrPingResponse.getStatus()).thenReturn(0);
when(solrPingResponse.getQTime()).thenReturn(10);
when(solrClient.query(any(), any(SolrRequest.METHOD.class))).thenReturn(response);
when(response.getResults()).thenReturn(new SolrDocumentList());
when(response.getResponse()).thenReturn(responseObject);
when(responseObject.get("responseHeader")).thenReturn(responseObject);
when(responseObject.get("params")).thenReturn(responseObject);
when(responseObject.get("suggestion.field")).thenReturn("category");
when(solrClient.add(org.mockito.Matchers.<Collection<SolrInputDocument>>any())).thenReturn(iResponse);
when(solrClient.add(any(SolrInputDocument.class))).thenReturn(iResponse);
when(iResponse.getQTime()).thenReturn(10);
when(iResponse.getElapsedTime()).thenReturn(15l);
//we use the protected constructor to avoid schema checking
server = new SolrSearchServerTestClass(solrClient);
}
public void doTestAssertTagStreaming(BiFunction<ModifiableSolrParams,String,QueryRequest> newQueryRequest) throws IOException, SolrServerException {
ModifiableSolrParams params = params();
String input = "foo boston bar";//just one tag;
QueryRequest req = newQueryRequest.apply(params, input);
req.setPath("/tag");
final AtomicReference<SolrDocument> refDoc = new AtomicReference<>();
req.setStreamingResponseCallback(new StreamingResponseCallback() {
@Override
public void streamSolrDocument(SolrDocument doc) {
refDoc.set(doc);
}
@Override
public void streamDocListInfo(long numFound, long start, Float maxScore) {
}
});
QueryResponse rsp = req.process(solrServer);
assertNotNull(rsp.getResponse().get("tags"));
assertNotNull(refDoc.get());
assertEquals("Boston", ((Field)refDoc.get().getFieldValue("name")).stringValue());
}
public void createDocumentShrinkers() throws SolrServerException, InterruptedException, ExecutionException {
ExecutorService service = Executors.newFixedThreadPool(threadPoolSize);
CompletionService<String> completionService = new ExecutorCompletionService<String>(service);
HttpSolrServer thothServer = new HttpSolrServer(thothIndexUrl + realTimeCore);
HttpSolrServer thothShrankServer = new HttpSolrServer(thothIndexUrl + shrankCore);
ArrayList<ServerDetail> listOfServers = new ThothServers().getList(thothServer);
for (ServerDetail serverDetail: listOfServers){
LOG.info("Shrinking docs for server("+serverDetail.getName()+"):("+serverDetail.getPort()+") ");
completionService.submit(new DocumentShrinker(serverDetail, nowMinusTimeToShrink, thothServer, thothShrankServer));
}
// Wait for all the executors to finish
for(int i = 0; i < listOfServers.size(); i++){
completionService.take().get();
}
LOG.info("Done Shrinking.");
}
/**
* Write a record. This method accumulates records in to a batch, and when
* {@link #batchSize} items are present flushes it to the indexer. The writes
* can take a substantial amount of time, depending on {@link #batchSize}. If
* there is heavy disk contention the writes may take more than the 600 second
* default timeout.
*/
@Override
public void write(K key, V value) throws IOException {
heartBeater.needHeartBeat();
try {
try {
SolrInputDocumentWritable sidw = (SolrInputDocumentWritable) value;
batch.add(sidw.getSolrInputDocument());
if (batch.size() >= batchSize) {
batchWriter.queueBatch(batch);
numDocsWritten += batch.size();
if (System.nanoTime() >= nextLogTime) {
LOG.info("docsWritten: {}", numDocsWritten);
nextLogTime += TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
}
batch.clear();
}
} catch (SolrServerException e) {
throw new IOException(e);
}
} finally {
heartBeater.cancelHeartBeat();
}
}
/**
* Indexes the document in both the client, and a selected shard
*/
protected static void indexDoc(SolrClient client, int shardId, SolrInputDocument doc)
throws IOException, SolrServerException
{
client.add(doc);
SolrClient clientShard = clientShards.get(shardId);
clientShard.add(doc);
}
@RequestMapping("/addUser")
public void addUser() throws IOException, SolrServerException {
// 构造一篇文档
//往doc中添加字段,在客户端这边添加的字段必须在服务端的配置文件中有定义
User user = new User();
user.setId("8888888");
user.setName("龙知然");
solrClient.addBean(user);
solrClient.commit();
}
@Override
public Iterable<Document> getAllLatest(List<GetRequest> getRequests) throws IOException {
Map<String, Collection<String>> collectionIdMap = new HashMap<>();
for (GetRequest getRequest : getRequests) {
Optional<String> index = SolrUtilities
.getIndex(config.getIndexSupplier(), getRequest.getSensorType(), getRequest.getIndex());
if (index.isPresent()) {
Collection<String> ids = collectionIdMap.getOrDefault(index.get(), new HashSet<>());
ids.add(getRequest.getGuid());
collectionIdMap.put(index.get(), ids);
} else {
LOG.debug("Unable to find index for sensorType {}", getRequest.getSensorType());
}
}
try {
List<Document> documents = new ArrayList<>();
for (String collection : collectionIdMap.keySet()) {
SolrDocumentList solrDocumentList = client.getById(collectionIdMap.get(collection),
new SolrQuery().set("collection", collection));
documents.addAll(
solrDocumentList.stream().map(SolrUtilities::toDocument).collect(Collectors.toList()));
}
return documents;
} catch (SolrServerException e) {
throw new IOException(e);
}
}
@Test
public void testTag() throws SolrServerException, IOException {
ModifiableSolrParams params = params();
String input = "foo boston bar";//just one tag;
QueryRequest req = new SolrTaggerRequest(params, input);
req.setPath("/tag");
QueryResponse rsp = req.process(solrServer);
SolrDocumentList results= (SolrDocumentList) rsp.getResponse().get("response");
assertNotNull(rsp.getResponse().get("tags"));
assertNotNull(results.get(0));
}
private void errorBackup(CloudSolrClient solrClient)
throws SolrServerException, IOException {
CollectionAdminRequest.Backup backup = CollectionAdminRequest.backupCollection(getCollectionName(), "poisionedbackup")
.setLocation(getBackupLocation());
if (random().nextBoolean()) {
backup.setRepositoryName(poisioned);
} // otherwise we hit default
try {
backup.process(solrClient);
fail("This request should have failed since omitting repo, picks up default poisioned.");
} catch (SolrException ex) {
assertEquals(ErrorCode.SERVER_ERROR.code, ex.code());
}
}
@Override
public SolrDocumentList search( String queryString ) throws SolrServerException
{
SolrServer server = solr.solrServer();
NamedList<Object> list = new NamedList<>();
list.add( "q", queryString );
QueryResponse query = server.query( SolrParams.toSolrParams( list ) );
return query.getResults();
}
public static void setupIndexes(boolean routeByKey) throws IOException, SolrServerException {
clearCollection("products");
clearCollection("parts");
buildIndexes(routeByKey);
assertResultCount("products", "*:*", NUM_PRODUCTS, true);
assertResultCount("parts", "*:*", NUM_PRODUCTS * 10 / 4, true);
}
public static void postFile(SolrClient client, ByteBuffer buffer, String name, String sig)
throws SolrServerException, IOException {
String resource = "/cluster/files" + name;
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("sig", sig);
V2Response rsp = new V2Request.Builder(resource)
.withMethod(SolrRequest.METHOD.PUT)
.withPayload(buffer)
.forceV2(true)
.withMimeType("application/octet-stream")
.withParams(params)
.build()
.process(client);
assertEquals(name, rsp.getResponse().get(CommonParams.FILE));
}
private void testParallelCollectionAPICalls() throws IOException, SolrServerException {
try (SolrClient client = createNewSolrClient("", getBaseUrl((HttpSolrClient) clients.get(0)))) {
for(int i = 1 ; i <= NUM_COLLECTIONS ; i++) {
CollectionAdminRequest.createCollection("ocptest" + i,"conf1",3,1).processAsync(String.valueOf(i), client);
}
boolean pass = false;
int counter = 0;
while(true) {
int numRunningTasks = 0;
for (int i = 1; i <= NUM_COLLECTIONS; i++)
if (getRequestState(i + "", client) == RequestStatusState.RUNNING) {
numRunningTasks++;
}
if (numRunningTasks > 1) {
pass = true;
break;
} else if (counter++ > 100) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
assertTrue("More than one tasks were supposed to be running in parallel but they weren't.", pass);
for (int i = 1; i <= NUM_COLLECTIONS; i++) {
final RequestStatusState state = getRequestStateAfterCompletion(i + "", REQUEST_STATUS_TIMEOUT, client);
assertSame("Task " + i + " did not complete, final state: " + state, RequestStatusState.COMPLETED, state);
}
}
}
public void commit() throws StageException {
try {
this.solrClient.commit(waitFlush, waitSearcher, softCommit);
} catch (SolrServerException | IOException ex) {
throw new StageException(Errors.SOLR_05, ex.toString(), ex);
}
}
@Test
public void testLessThanBatchSizeShouldProduceOneFlowFile() throws IOException, SolrServerException {
final org.apache.nifi.processors.solr.TestGetSolr.TestableProcessor proc = new org.apache.nifi.processors.solr.TestGetSolr.TestableProcessor(solrClient);
TestRunner runner = createDefaultTestRunner(proc);
runner.setProperty(GetSolr.BATCH_SIZE, "20");
runner.run();
runner.assertAllFlowFilesTransferred(GetSolr.REL_SUCCESS, 1);
}
private long indexSize() throws IOException, SolrServerException {
final var q = new SolrQuery("*:*");
q.setRows(0); // don't actually request any data
final var theResponse = solrClient.query(q);
if (theResponse.getResults() != null) {
return theResponse.getResults().getNumFound();
}
return 0;
}
/**
* Paginated search for system operations.
* @param query Solr query `q` parameter.
* @return an iterator of found products.
*/
public Iterator<Product> search(String query)
{
try
{
final Iterator<SolrDocument> it = solrDao.scroll(new SolrQuery(query));
return new Iterator<Product>()
{
@Override
public boolean hasNext()
{
return it.hasNext();
}
@Override
public Product next()
{
return productService.getProduct((Long) it.next().get("id"));
}
@Override
public void remove()
{
productService.deleteProduct((Long) it.next().get("id"));
}
};
}
catch (IOException|SolrServerException ex)
{
LOGGER.error("An exception occured while searching", ex);
}
return Collections.EMPTY_LIST.iterator();
}