本文整理汇总了Java中org.elasticsearch.common.io.Streams类的典型用法代码示例。如果您正苦于以下问题:Java Streams类的具体用法?Java Streams怎么用?Java Streams使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Streams类属于org.elasticsearch.common.io包,在下文中一共展示了Streams类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInitialSearchEntity
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
public void testInitialSearchEntity() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.source(new SearchSourceBuilder());
String query = "{\"match_all\":{}}";
HttpEntity entity = initialSearchEntity(searchRequest, new BytesArray(query));
assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
assertEquals("{\"query\":" + query + ",\"_source\":true}",
Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
// Source filtering is included if set up
searchRequest.source().fetchSource(new String[] {"in1", "in2"}, new String[] {"out"});
entity = initialSearchEntity(searchRequest, new BytesArray(query));
assertEquals(ContentType.APPLICATION_JSON.toString(), entity.getContentType().getValue());
assertEquals("{\"query\":" + query + ",\"_source\":{\"includes\":[\"in1\",\"in2\"],\"excludes\":[\"out\"]}}",
Streams.copyToString(new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8)));
// Invalid XContent fails
RuntimeException e = expectThrows(RuntimeException.class,
() -> initialSearchEntity(searchRequest, new BytesArray("{}, \"trailing\": {}")));
assertThat(e.getCause().getMessage(), containsString("Unexpected character (',' (code 44))"));
e = expectThrows(RuntimeException.class, () -> initialSearchEntity(searchRequest, new BytesArray("{")));
assertThat(e.getCause().getMessage(), containsString("Unexpected end-of-input"));
}
示例2: readBlob
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
/**
* Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
*
* @param blobContainer blob container
* @param blobName blob name
*/
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
try (InputStream inputStream = blobContainer.readBlob(blobName)) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(inputStream, out);
final byte[] bytes = out.toByteArray();
final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
try (ByteArrayIndexInput indexInput = new ByteArrayIndexInput(resourceDesc, bytes)) {
CodecUtil.checksumEntireFile(indexInput);
CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
long filePointer = indexInput.getFilePointer();
long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
BytesReference bytesReference = new BytesArray(bytes, (int) filePointer, (int) contentSize);
return read(bytesReference);
} catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
// we trick this into a dedicated exception with the original stacktrace
throw new CorruptStateException(ex);
}
}
}
示例3: handleFavicon
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
void handleFavicon(RestRequest request, RestChannel channel) {
if (request.method() == RestRequest.Method.GET) {
try {
try (InputStream stream = getClass().getResourceAsStream("/config/favicon.ico")) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out);
BytesRestResponse restResponse = new BytesRestResponse(RestStatus.OK, "image/x-icon", out.toByteArray());
channel.sendResponse(restResponse);
}
} catch (IOException e) {
channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}
} else {
channel.sendResponse(new BytesRestResponse(FORBIDDEN, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
}
}
示例4: writeRawField
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
@Override
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
if (mayWriteRawData(contentType) == false) {
// EMPTY is safe here because we never call namedObject when writing raw data
try (XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, content)) {
parser.nextToken();
writeFieldName(name);
copyCurrentStructure(parser);
}
} else {
writeStartRaw(name);
flush();
Streams.copy(content, os);
writeEndRaw();
}
}
示例5: testScriptsWithoutExtensions
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
public void testScriptsWithoutExtensions() throws IOException {
buildScriptService(Settings.EMPTY);
Path testFileNoExt = scriptsFilePath.resolve("test_no_ext");
Path testFileWithExt = scriptsFilePath.resolve("test_script.test");
Streams.copy("test_file_no_ext".getBytes("UTF-8"), Files.newOutputStream(testFileNoExt));
Streams.copy("test_file".getBytes("UTF-8"), Files.newOutputStream(testFileWithExt));
resourceWatcherService.notifyNow();
CompiledScript compiledScript = scriptService.compile(new Script(ScriptType.FILE, "test", "test_script", Collections.emptyMap()),
ScriptContext.Standard.SEARCH);
assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file"));
Files.delete(testFileNoExt);
Files.delete(testFileWithExt);
resourceWatcherService.notifyNow();
try {
scriptService.compile(new Script(ScriptType.FILE, "test", "test_script", Collections.emptyMap()), ScriptContext.Standard.SEARCH);
fail("the script test_script should no longer exist");
} catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString("unable to find file script [test_script] using lang [test]"));
}
}
示例6: testScriptCompiledOnceHiddenFileDetected
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
public void testScriptCompiledOnceHiddenFileDetected() throws IOException {
buildScriptService(Settings.EMPTY);
Path testHiddenFile = scriptsFilePath.resolve(".hidden_file");
Streams.copy("test_hidden_file".getBytes("UTF-8"), Files.newOutputStream(testHiddenFile));
Path testFileScript = scriptsFilePath.resolve("file_script.test");
Streams.copy("test_file_script".getBytes("UTF-8"), Files.newOutputStream(testFileScript));
resourceWatcherService.notifyNow();
CompiledScript compiledScript = scriptService.compile(new Script(ScriptType.FILE, "test", "file_script", Collections.emptyMap()),
ScriptContext.Standard.SEARCH);
assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file_script"));
Files.delete(testHiddenFile);
Files.delete(testFileScript);
resourceWatcherService.notifyNow();
}
示例7: randomCorruption
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
protected void randomCorruption(BlobContainer blobContainer, String blobName) throws IOException {
byte[] buffer = new byte[(int) blobContainer.listBlobsByPrefix(blobName).get(blobName).length()];
long originalChecksum = checksum(buffer);
try (InputStream inputStream = blobContainer.readBlob(blobName)) {
Streams.readFully(inputStream, buffer);
}
do {
int location = randomIntBetween(0, buffer.length - 1);
buffer[location] = (byte) (buffer[location] ^ 42);
} while (originalChecksum == checksum(buffer));
blobContainer.deleteBlob(blobName); // delete original before writing new blob
BytesArray bytesArray = new BytesArray(buffer);
try (StreamInput stream = bytesArray.streamInput()) {
blobContainer.writeBlob(blobName, stream, bytesArray.length());
}
}
示例8: createIndex
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
private void createIndex(final ActionListener<ActionResponse> listener) {
try (final Reader in = new InputStreamReader(IndexingProxyService.class.getClassLoader().getResourceAsStream(FILE_MAPPING_JSON),
StandardCharsets.UTF_8)) {
final String source = Streams.copyToString(in);
final XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()//
.startObject()//
.startObject("index")//
.field("number_of_shards", numberOfShards)//
.field("number_of_replicas", numberOfReplicas)//
.endObject()//
.endObject();
client.admin().indices().prepareCreate(IndexingProxyPlugin.INDEX_NAME).setSettings(settingsBuilder)
.addMapping(IndexingProxyPlugin.TYPE_NAME, source, XContentFactory.xContentType(source))
.execute(wrap(response -> waitForIndex(listener), listener::onFailure));
} catch (final IOException e) {
listener.onFailure(e);
}
}
示例9: addMappings
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
private void addMappings(Map<String, Map<String, Object>> mappings, File mappingsDir) {
File[] mappingsFiles = mappingsDir.listFiles();
for (File mappingFile : mappingsFiles) {
if (mappingFile.isHidden()) {
continue;
}
int lastDotIndex = mappingFile.getName().lastIndexOf('.');
String mappingType = lastDotIndex != -1 ? mappingFile.getName().substring(0, lastDotIndex) : mappingFile.getName();
try {
String mappingSource = Streams.copyToString(new InputStreamReader(new FileInputStream(mappingFile), Charsets.UTF_8));
if (mappings.containsKey(mappingType)) {
XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
} else {
mappings.put(mappingType, parseMapping(mappingSource));
}
} catch (Exception e) {
logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
}
}
}
示例10: addMappings
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(mappingsDir)) {
for (Path mappingFile : stream) {
final String fileName = mappingFile.getFileName().toString();
if (FileSystemUtils.isHidden(mappingFile)) {
continue;
}
int lastDotIndex = fileName.lastIndexOf('.');
String mappingType = lastDotIndex != -1 ? mappingFile.getFileName().toString().substring(0, lastDotIndex) : mappingFile.getFileName().toString();
try (BufferedReader reader = Files.newBufferedReader(mappingFile, Charsets.UTF_8)) {
String mappingSource = Streams.copyToString(reader);
if (mappings.containsKey(mappingType)) {
XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
} else {
mappings.put(mappingType, parseMapping(mappingSource));
}
} catch (Exception e) {
logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
}
}
}
}
示例11: createIndex
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
private void createIndex(final ActionListener<ActionResponse> listener) {
try (final Reader in = new InputStreamReader(ConfigSyncService.class.getClassLoader().getResourceAsStream(FILE_MAPPING_JSON),
StandardCharsets.UTF_8)) {
final String source = Streams.copyToString(in);
final XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()//
.startObject()//
.startObject("index")//
.field("number_of_replicas", 0)//
.endObject()//
.endObject();
client.admin().indices().prepareCreate(index).setSettings(settingsBuilder)
.addMapping(type, source, XContentType.JSON)
.execute(wrap(response -> waitForIndex(listener), listener::onFailure));
} catch (final IOException e) {
listener.onFailure(e);
}
}
示例12: simpleDeployTest
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
@Test
public void simpleDeployTest() throws IOException, InterruptedException {
String name = "demo";
InputStream in = getClass().getResourceAsStream("/example.zip");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(in, out);
BytesReference content = new BytesArray(out.toByteArray());
String contentType = "application/zip";
DeployRequestBuilder deployRequestBuilder = new DeployRequestBuilder(client("1").admin().cluster())
.setName(name)
.setContentType(contentType)
.setContent(content);
final DeployRequest deployRequest = deployRequestBuilder.request();
client("1").admin().cluster().execute(DeployAction.INSTANCE, deployRequest);
Thread.sleep(5000L);
}
示例13: httpPost
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
@Test
public void httpPost() throws IOException {
startCluster();
try {
String httpAddress = findHttpAddress(client());
if (httpAddress == null) {
throw new IllegalArgumentException("no HTTP address found");
}
URL base = new URL(httpAddress);
URL url = new URL(base, "_langdetect");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
Streams.copy(new StringReader("Das ist ein Text"), new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
StringWriter response = new StringWriter();
Streams.copy(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8), response);
assertEquals("{\"languages\":[{\"language\":\"de\",\"probability\":0.9999967609942226}]}", response.toString());
} finally {
stopCluster();
}
}
示例14: httpPostShortProfile
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
@Test
public void httpPostShortProfile() throws IOException {
startCluster();
try {
String httpAddress = findHttpAddress(client());
if (httpAddress == null) {
throw new IllegalArgumentException("no HTTP address found");
}
URL base = new URL(httpAddress);
URL url = new URL(base, "_langdetect?profile=short-text");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
Streams.copy(new StringReader("Das ist ein Text"), new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
StringWriter response = new StringWriter();
Streams.copy(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8), response);
assertEquals("{\"profile\":\"short-text\",\"languages\":[{\"language\":\"de\",\"probability\":0.9999968539079941}]}", response.toString());
} finally {
stopCluster();
}
}
示例15: copyToStringFromClasspath
import org.elasticsearch.common.io.Streams; //导入依赖的package包/类
public static String copyToStringFromClasspath(ClassLoader classLoader, String path) throws IOException {
InputStream is = classLoader.getResourceAsStream(path);
if (is == null) {
throw new FileNotFoundException("Resource [" + path + "] not found in classpath with class loader [" + classLoader + "]");
}
return Streams.copyToString(new InputStreamReader(is, StandardCharsets.UTF_8));
}