88import org .apache .logging .log4j .LogManager ;
99import org .apache .logging .log4j .Logger ;
1010
11+ import fr .insee .arc .core .service .p1reception .provider .DirectoriesReception ;
12+ import fr .insee .arc .core .service .p2chargement .bo .NormeRules ;
13+ import fr .insee .arc .core .util .BDParameters ;
1114import fr .insee .arc .utils .consumer .ThrowingConsumer ;
1215import fr .insee .arc .utils .dao .DuckdbDao ;
1316import fr .insee .arc .utils .dao .GenericPreparedStatementBuilder ;
@@ -23,6 +26,10 @@ public class ParquetDao {
2326
2427 private static final Logger LOGGER = LogManager .getLogger (ParquetDao .class );
2528
29+ // Ratio MAX_PARALLEL_WORKERS parameters
30+ // used not to over throttle input database or s3
31+ private static final float RATIO_OF_CPU_USED_TO_EXPORT_PARQUET = 0.5F ;
32+
2633 // parquet file format as "file.parquet"
2734 private static final String PARQUET_FILE_EXTENSION = ".parquet" ;
2835
@@ -69,6 +76,8 @@ public void exportToParquet(List<TableToRetrieve> tables, String outputDirectory
6976 {
7077 addParquetEncryptionKeyInDuckDb (connection );
7178
79+ defineNumberOfThreadThread (connection );
80+
7281 // create output directory
7382 FileUtilsArc .createDirIfNotexist (outputDirectory );
7483
@@ -79,10 +88,26 @@ public void exportToParquet(List<TableToRetrieve> tables, String outputDirectory
7988 }
8089 };
8190
91+
8292 duckdbDao .executeOnDuckdb (exportToParquetOperation );
8393
8494 }
8595
96+ private void defineNumberOfThreadThread (Connection connection ) throws ArcException {
97+
98+ BDParameters bdParameters = new BDParameters (ArcDatabase .COORDINATOR );
99+
100+ int maxParallelWorkers = (int ) (bdParameters .getInt (null ,"ApiService.MAX_PARALLEL_WORKERS" , 4 ) * RATIO_OF_CPU_USED_TO_EXPORT_PARQUET ) ;
101+ maxParallelWorkers = (maxParallelWorkers < 1 )? 1 : maxParallelWorkers ;
102+
103+ GenericPreparedStatementBuilder query = new GenericPreparedStatementBuilder ();
104+ query .append ("SET threads=" ).append (query .quoteInt (maxParallelWorkers )).append (";" );
105+
106+ duckdbDao .executeQuery (connection , query );
107+
108+ }
109+
110+
86111 private void addParquetEncryptionKeyInDuckDb (Connection connection ) throws ArcException {
87112
88113 if (encryptionKey == null ) {
0 commit comments