Batch Extraction Examples
These examples are derived directly from alef’s cross-language fixture corpus and are kept in sync with the generated snippet tree automatically — no per-language import list to maintain.
Batch-extract in-memory bytes
Section titled “Batch-extract in-memory bytes”Extract multiple in-memory documents in one batch.
import asynciofrom pathlib import Pathfrom xberg import extract_batch
async def main() -> None: inputs = [{"bytes": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], "kind": "bytes", "mime_type": "text/plain"}, {"bytes": "html/html.html", "kind": "bytes", "mime_type": "text/html"}] result = await extract_batch(inputs) print(result.results)
asyncio.run(main())Extract multiple in-memory documents in one batch.
import { ExtractInput, ExtractInputKind, extractBatch } from "@xberg-io/xberg";async function main() { const result = await extractBatch([{ bytes: Uint8Array.from([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]), kind: ExtractInputKind.Bytes, mimeType: "text/plain" } as ExtractInput, { bytes: await (await import("node:fs/promises")).readFile("html/html.html"), kind: ExtractInputKind.Bytes, mimeType: "text/html" } as ExtractInput]); console.log(result.results);}
void main();Extract multiple in-memory documents in one batch.
import { WasmExtractInput, WasmExtractInputKind, extractBatch } from "@xberg-io/xberg-wasm";async function main() { const result = await extractBatch([await (async () => { const _u0 = WasmExtractInput.default(); _u0.bytes = Uint8Array.from([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]); _u0.kind = WasmExtractInputKind.Bytes; _u0.mimeType = "text/plain"; return _u0; })(), await (async () => { const _u0 = WasmExtractInput.default(); _u0.bytes = await (await import("node:fs/promises")).readFile("html/html.html"); _u0.kind = WasmExtractInputKind.Bytes; _u0.mimeType = "text/html"; return _u0; })()], undefined); console.log(result.results);}
void main();Extract multiple in-memory documents in one batch.
use xberg::extract_batch;use xberg::ExtractInput;
#[tokio::main]async fn main() { let mut inputs_json: serde_json::Value = serde_json::from_str(r#"[{"bytes":[72,101,108,108,111,44,32,119,111,114,108,100,33],"kind":"bytes","mime_type":"text/plain"},{"bytes":"html/html.html","kind":"bytes","mime_type":"text/html"}]"#).unwrap(); let inputs_file_0 = std::fs::read(r#"html/html.html"#).expect("file read failed"); *inputs_json.pointer_mut(r#"/1/bytes"#).expect("docs file field missing") = serde_json::json!(inputs_file_0); let inputs = serde_json::from_value::<Vec<ExtractInput>>(inputs_json).unwrap(); let config = Default::default(); let result = extract_batch(inputs, &config).await.expect("call failed"); println!("{:?}", result.results);}Extract multiple in-memory documents in one batch.
package main
import ( "encoding/json" "fmt" xberg "github.com/xberg-io/xberg/packages/go")
func main() { var inputs []xberg.ExtractInput if err := json.Unmarshal([]byte(`[{"bytes":"SGVsbG8sIHdvcmxkIQ==","kind":"bytes","mime_type":"text/plain"},{"bytes":"html/html.html","kind":"bytes","mime_type":"text/html"}]`), &inputs); err != nil { panic(fmt.Sprintf("config parse failed: %v", err)) } config := xberg.ExtractionConfig{} result, err := xberg.ExtractBatch(inputs, config) if err != nil { panic(err) } fmt.Printf("%+v\n", result.Results)}Extract multiple in-memory documents in one batch.
import io.xberg.*;
public final class Example { public static void main(String[] args) throws Exception { var result = Xberg.extractBatch(java.util.Arrays.asList(JsonUtil.fromJson("{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"}", ExtractInput.class), JsonUtil.fromJson("{\"bytes\":\"html/html.html\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}", ExtractInput.class)), ExtractionConfig.builder().build()); System.out.println(result.results()); }}Extract multiple in-memory documents in one batch.
import io.xberg.*import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
fun main() = kotlinx.coroutines.runBlocking { val mapper = jacksonObjectMapper().setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE) val configDefault = mapper.readValue("{\"url\":{\"crawl\":{\"ssrf\":{}}}}", ExtractionConfig::class.java) val result = Xberg.extractBatch(listOf(mapper.readValue("{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"}", ExtractInput::class.java), mapper.readValue("{\"bytes\":\"html/html.html\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}", ExtractInput::class.java)), configDefault) println(result.results)}Extract multiple in-memory documents in one batch.
using System;using System.Text.Json;using Xberg;
var ConfigOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };var result = await XbergConverter.ExtractBatchAsync(new List<ExtractInput>() { JsonSerializer.Deserialize<ExtractInput>("{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"}", ConfigOptions)!, JsonSerializer.Deserialize<ExtractInput>("{\"bytes\":\"html/html.html\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}", ConfigOptions)! }, new ExtractionConfig());Console.WriteLine(result.Results);Extract multiple in-memory documents in one batch.
import Xberg
let _item_inputsArray_0 = try Xberg.extractInputFromJson("{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"}")let _item_inputsArray_1 = try Xberg.extractInputFromJson("{\"bytes\":\"html/html.html\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}")let inputsArray = [_item_inputsArray_0, _item_inputsArray_1]let configObj = try Xberg.extractionConfigFromJson("{}")let result = try await Xberg.extractBatch(inputs: inputsArray, config: configObj)debugPrint(result.results())Extract multiple in-memory documents in one batch.
require "xberg"result = Xberg.extract_batch([{ 'bytes' => [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], 'kind' => 'bytes', 'mime_type' => 'text/plain' }, { 'bytes' => 'html/html.html', 'kind' => 'bytes', 'mime_type' => 'text/html' }])puts result.results.inspectExtract multiple in-memory documents in one batch.
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Xberg\Xberg;use Xberg\ExtractInput;use Xberg\ExtractionConfig;$result = Xberg::extractBatch([ExtractInput::from_json('{"bytes":[72,101,108,108,111,44,32,119,111,114,108,100,33],"kind":"bytes","mime_type":"text/plain"}'), ExtractInput::from_json('{"bytes":"html/html.html","kind":"bytes","mime_type":"text/html"}')], \Xberg\ExtractionConfig::from_json('{}'));var_dump($result->getResults());Extract multiple in-memory documents in one batch.
result = Xberg.extract_batch_async([%{"bytes" => [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], "kind" => "bytes", "mime_type" => "text/plain"}, %{"bytes" => "html/html.html", "kind" => "bytes", "mime_type" => "text/html"}])IO.inspect(result.results)Extract multiple in-memory documents in one batch.
import 'dart:convert';import 'dart:io';import 'package:xberg/xberg.dart';import 'package:xberg/src/xberg_bridge_generated/frb_generated.dart' show RustLib;Future<void> main() async { await RustLib.init(); try { final inputs = await Future.wait((jsonDecode(r'[{"bytes":[72,101,108,108,111,44,32,119,111,114,108,100,33],"kind":"bytes","mime_type":"text/plain"},{"bytes":"html/html.html","kind":"bytes","mime_type":"text/html"}]') as List<dynamic>).map((element) => createExtractInputFromJson(json: jsonEncode(element)))); final result = await XbergBridge.extractBatch(inputs); stdout.writeln(result.results); } finally { RustLib.dispose(); }}Extract multiple in-memory documents in one batch.
const std = @import("std");const xberg = @import("xberg");
pub fn main() !void { var gpa: std.heap.DebugAllocator(.{}) = .init; defer _ = gpa.deinit(); const allocator = gpa.allocator();
var inputs_file_0_threaded = std.Io.Threaded.init(allocator, .{});defer inputs_file_0_threaded.deinit();const inputs_file_0_io = inputs_file_0_threaded.io();const inputs_file_0 = try std.Io.Dir.cwd().readFileAlloc(inputs_file_0_io, "html/html.html", allocator, .unlimited);defer allocator.free(inputs_file_0); const inputs_file_0_json = try std.json.Stringify.valueAlloc(allocator, inputs_file_0, .{ .emit_strings_as_arrays = true });defer allocator.free(inputs_file_0_json); const inputs_json_0 = try std.mem.replaceOwned(u8, allocator, "[{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"},{\"bytes\":\"__ALEF_DOC_FILE_0__\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}]", "\"__ALEF_DOC_FILE_0__\"", inputs_file_0_json);defer allocator.free(inputs_json_0); const _result_json = try xberg.extract_batch(inputs_json_0, "{}"); defer std.heap.c_allocator.free(_result_json); std.debug.print("{s}\n", .{_result_json});
}Extract multiple in-memory documents in one batch.
#include <assert.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "xberg.h"
int main(void) { XBERGAlefHandle result = xberg_extract_batch("[{\"bytes\":[72,101,108,108,111,44,32,119,111,114,108,100,33],\"kind\":\"bytes\",\"mime_type\":\"text/plain\"},{\"bytes\":\"html/html.html\",\"kind\":\"bytes\",\"mime_type\":\"text/html\"}]", 0); xberg_extraction_result_free(result); return EXIT_SUCCESS;}Batch-extract with an unsupported MIME type
Section titled “Batch-extract with an unsupported MIME type”extract_batch with unsupported bytes MIME type
import asynciofrom xberg import extract_batch
async def main() -> None: inputs = [{"bytes": [100, 97, 116, 97], "kind": "bytes", "mime_type": "application/x-unknown"}] result = await extract_batch(inputs) print(result)
asyncio.run(main())extract_batch with unsupported bytes MIME type
import { ExtractInput, ExtractInputKind, extractBatch } from "@xberg-io/xberg";async function main() { const result = await extractBatch([{ bytes: Uint8Array.from([100, 97, 116, 97]), kind: ExtractInputKind.Bytes, mimeType: "application/x-unknown" } as ExtractInput]); console.log(result);}
void main();extract_batch with unsupported bytes MIME type
import { WasmExtractInput, WasmExtractInputKind, extractBatch } from "@xberg-io/xberg-wasm";async function main() { const result = await extractBatch([(() => { const _u0 = WasmExtractInput.default(); _u0.bytes = Uint8Array.from([100, 97, 116, 97]); _u0.kind = WasmExtractInputKind.Bytes; _u0.mimeType = "application/x-unknown"; return _u0; })()], undefined); console.log(result);}
void main();extract_batch with unsupported bytes MIME type
use xberg::extract_batch;use xberg::ExtractInput;
#[tokio::main]async fn main() { let inputs_json: serde_json::Value = serde_json::from_str(r#"[{"bytes":[100,97,116,97],"kind":"bytes","mime_type":"application/x-unknown"}]"#).unwrap(); let inputs = serde_json::from_value::<Vec<ExtractInput>>(inputs_json).unwrap(); let config = Default::default(); let result = extract_batch(inputs, &config).await; println!("{:?}", result);}extract_batch with unsupported bytes MIME type
package main
import ( "encoding/json" "fmt" xberg "github.com/xberg-io/xberg/packages/go")
func main() { var inputs []xberg.ExtractInput if err := json.Unmarshal([]byte(`[{"bytes":"ZGF0YQ==","kind":"bytes","mime_type":"application/x-unknown"}]`), &inputs); err != nil { panic(fmt.Sprintf("config parse failed: %v", err)) } config := xberg.ExtractionConfig{} result, err := xberg.ExtractBatch(inputs, config) if err != nil { panic(err) } fmt.Printf("%+v\n", result)}extract_batch with unsupported bytes MIME type
import io.xberg.*;
public final class Example { public static void main(String[] args) throws Exception { var result = Xberg.extractBatch(java.util.Arrays.asList(JsonUtil.fromJson("{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}", ExtractInput.class)), ExtractionConfig.builder().build()); System.out.println(result); }}extract_batch with unsupported bytes MIME type
import io.xberg.*import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
fun main() = kotlinx.coroutines.runBlocking { val mapper = jacksonObjectMapper().setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategies.SNAKE_CASE) val configDefault = mapper.readValue("{\"url\":{\"crawl\":{\"ssrf\":{}}}}", ExtractionConfig::class.java) val result = Xberg.extractBatch(listOf(mapper.readValue("{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}", ExtractInput::class.java)), configDefault) println(result)}extract_batch with unsupported bytes MIME type
using System;using System.Text.Json;using Xberg;
var ConfigOptions = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };var result = await XbergConverter.ExtractBatchAsync(new List<ExtractInput>() { JsonSerializer.Deserialize<ExtractInput>("{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}", ConfigOptions)! }, new ExtractionConfig());Console.WriteLine(result);extract_batch with unsupported bytes MIME type
import Xberg
let _item_inputsArray_0 = try Xberg.extractInputFromJson("{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}")let inputsArray = [_item_inputsArray_0]let configObj = try Xberg.extractionConfigFromJson("{}")let result = try await Xberg.extractBatch(inputs: inputsArray, config: configObj)print(result)extract_batch with unsupported bytes MIME type
require "xberg"result = Xberg.extract_batch([{ 'bytes' => [100, 97, 116, 97], 'kind' => 'bytes', 'mime_type' => 'application/x-unknown' }])puts result.inspectextract_batch with unsupported bytes MIME type
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use Xberg\Xberg;use Xberg\ExtractInput;use Xberg\ExtractionConfig;$result = Xberg::extractBatch([ExtractInput::from_json('{"bytes":[100,97,116,97],"kind":"bytes","mime_type":"application/x-unknown"}')], \Xberg\ExtractionConfig::from_json('{}'));var_dump($result);extract_batch with unsupported bytes MIME type
result = Xberg.extract_batch_async([%{"bytes" => [100, 97, 116, 97], "kind" => "bytes", "mime_type" => "application/x-unknown"}])IO.inspect(result)extract_batch with unsupported bytes MIME type
import 'dart:convert';import 'dart:io';import 'package:xberg/xberg.dart';import 'package:xberg/src/xberg_bridge_generated/frb_generated.dart' show RustLib;Future<void> main() async { await RustLib.init(); try { final inputs = await Future.wait((jsonDecode(r'[{"bytes":[100,97,116,97],"kind":"bytes","mime_type":"application/x-unknown"}]') as List<dynamic>).map((element) => createExtractInputFromJson(json: jsonEncode(element)))); final result = await XbergBridge.extractBatch(inputs); stdout.writeln(result); } finally { RustLib.dispose(); }}extract_batch with unsupported bytes MIME type
const std = @import("std");const xberg = @import("xberg");
pub fn main() !void { const _result_json = try xberg.extract_batch("[{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}]", "{}"); defer std.heap.c_allocator.free(_result_json); std.debug.print("{s}\n", .{_result_json});
}extract_batch with unsupported bytes MIME type
#include <assert.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "xberg.h"
int main(void) { XBERGAlefHandle result = xberg_extract_batch("[{\"bytes\":[100,97,116,97],\"kind\":\"bytes\",\"mime_type\":\"application/x-unknown\"}]", 0); xberg_extraction_result_free(result); return EXIT_SUCCESS;}