Process predictions for many queries using efficient parallelization through Spark. Useful for mass auditing of predictions and for generating predictions to push into other systems.

Batch predict reads and writes multi-object JSON files similar to the batch import format. JSON objects are separated by newlines and cannot themselves contain unencoded newlines.


pio batchpredict loads the engine and processes queries exactly like pio deploy. There is only one additional requirement for engines to utilize batch predict:

All algorithm classes used in the engine must be serializable. This is already true for PredictionIO's base algorithm classes, but may be broken by including non-serializable fields in their constructor. Using the @transient annotation may help in these cases.

This requirement is due to processing the input queries as a Spark RDD which enables high-performance parallelization, even on a single machine.


pio batchpredict

Command to process bulk predictions. Takes the same options as pio deploy plus:

--input <value>

Path to file containing queries; a multi-object JSON file with one query object per line. Accepts any valid Hadoop file URL.

Default: batchpredict-input.json

--output <value>

Path to file to receive results; a multi-object JSON file with one object per line, the prediction + original query. Accepts any valid Hadoop file URL. Actual output will be written as Hadoop partition files in a directory with the output name.

Default: batchpredict-output.json

--query-partitions <value>

Configure the concurrency of predictions by setting the number of partitions used internally for the RDD of queries. This will directly effect the number of resulting part-* output files. While setting to 1 may seem appealing to get a single output file, this will remove parallelization for the batch process, reducing performance and possibly exhausting memory.

Default: number created by Spark context's textFile (probably the number of cores available on the local machine)

--engine-instance-id <value>

Identifier for the trained instance to use for batch predict.

Default: the latest trained instance.



A multi-object JSON file of queries as they would be sent to the engine's HTTP Queries API.

Read via SparkContext's textFile and so may be a single file or any supported Hadoop format.

File: batchpredict-input.json



pio batchpredict \
  --input batchpredict-input.json \
  --output batchpredict-output.json

This command will run to completion, aborting if any errors are encountered.


A multi-object JSON file of predictions + original queries. The predictions are JSON objects as they would be returned from the engine's HTTP Queries API.

Results are written via Spark RDD's saveAsTextFile so each partition will be written to its own part-* file. See post-processing results.

File 1: batchpredict-output.json/part-00000


File 2: batchpredict-output.json/part-00001


Post-processing Results

After the process exits successfully, the parts may be concatenated into a single output file using a command like:

cat batchpredict-output.json/part-* > batchpredict-output-all.json