@@ -70,9 +70,23 @@ public static GenericTable<GenericRow, String> createTable(
7070 Path tempDir ,
7171 Configuration hadoopConf ,
7272 boolean additionalColumns ) {
73+
74+ Schema schema = buildGenericSchema (partitionField , additionalColumns );
75+ return createTable (
76+ tableName , partitionField , tempDir , hadoopConf , additionalColumns , schema );
77+ }
78+
79+ public static GenericTable <GenericRow , String > createTable (
80+ String tableName ,
81+ String partitionField ,
82+ Path tempDir ,
83+ Configuration hadoopConf ,
84+ boolean additionalColumns ,
85+ Schema schema ) {
7386 String basePath = initBasePath (tempDir , tableName );
7487 Catalog catalog = createFilesystemCatalog (basePath , hadoopConf );
75- FileStoreTable paimonTable = createTable (catalog , partitionField , additionalColumns );
88+ FileStoreTable paimonTable =
89+ createTable (catalog , tableName , schema );
7690
7791 System .out .println (
7892 "Initialized Paimon test table at base path: "
@@ -91,19 +105,20 @@ public static Catalog createFilesystemCatalog(String basePath, Configuration had
91105 }
92106
93107 public static FileStoreTable createTable (
94- Catalog catalog , String partitionField , boolean additionalColumns ) {
108+ Catalog catalog ,
109+ String tableName ,
110+ Schema schema ) {
95111 try {
96112 catalog .createDatabase ("test_db" , true );
97- Identifier identifier = Identifier .create ("test_db" , "test_table" );
98- Schema schema = buildSchema (partitionField , additionalColumns );
113+ Identifier identifier = Identifier .create ("test_db" , tableName );
99114 catalog .createTable (identifier , schema , true );
100115 return (FileStoreTable ) catalog .getTable (identifier );
101116 } catch (Exception e ) {
102117 throw new RuntimeException (e );
103118 }
104119 }
105120
106- private static Schema buildSchema (String partitionField , boolean additionalColumns ) {
121+ private static Schema buildGenericSchema (String partitionField , boolean additionalColumns ) {
107122 Schema .Builder builder =
108123 Schema .newBuilder ()
109124 .primaryKey ("id" )
@@ -179,20 +194,12 @@ public List<GenericRow> insertRecordsForSpecialPartition(int numRows) {
179194 }
180195
181196 private List <GenericRow > insertRecordsToPartition (int numRows , String partitionValue ) {
182- BatchWriteBuilder batchWriteBuilder = paimonTable .newBatchWriteBuilder ();
183- try (BatchTableWrite writer = batchWriteBuilder .newWrite ()) {
184- List <GenericRow > rows = new ArrayList <>(numRows );
185- for (int i = 0 ; i < numRows ; i ++) {
186- GenericRow row = buildGenericRow (i , paimonTable .schema (), partitionValue );
187- writer .write (row );
188- rows .add (row );
189- }
190- commitWrites (batchWriteBuilder , writer );
191- compactTable ();
192- return rows ;
193- } catch (Exception e ) {
194- throw new RuntimeException ("Failed to insert rows into Paimon table" , e );
197+ List <GenericRow > rows = new ArrayList <>(numRows );
198+ for (int i = 0 ; i < numRows ; i ++) {
199+ rows .add (buildGenericRow (i , paimonTable .schema (), partitionValue ));
195200 }
201+ writeRows (paimonTable , rows );
202+ return rows ;
196203 }
197204
198205 @ Override
@@ -225,8 +232,12 @@ public void deleteRows(List<GenericRow> rows) {
225232 }
226233
227234 private void compactTable () {
228- BatchWriteBuilder batchWriteBuilder = paimonTable .newBatchWriteBuilder ();
229- SnapshotReader snapshotReader = paimonTable .newSnapshotReader ();
235+ compactTable (paimonTable );
236+ }
237+
238+ public static void compactTable (FileStoreTable table ) {
239+ BatchWriteBuilder batchWriteBuilder = table .newBatchWriteBuilder ();
240+ SnapshotReader snapshotReader = table .newSnapshotReader ();
230241 try (BatchTableWrite writer = batchWriteBuilder .newWrite ()) {
231242 for (BucketEntry bucketEntry : snapshotReader .bucketEntries ()) {
232243 writer .compact (bucketEntry .partition (), bucketEntry .bucket (), true );
@@ -237,6 +248,19 @@ private void compactTable() {
237248 }
238249 }
239250
251+ public static void writeRows (FileStoreTable table , List <GenericRow > rows ) {
252+ BatchWriteBuilder batchWriteBuilder = table .newBatchWriteBuilder ();
253+ try (BatchTableWrite writer = batchWriteBuilder .newWrite ()) {
254+ for (GenericRow row : rows ) {
255+ writer .write (row );
256+ }
257+ commitWrites (batchWriteBuilder , writer );
258+ compactTable (table );
259+ } catch (Exception e ) {
260+ throw new RuntimeException ("Failed to write rows into Paimon table" , e );
261+ }
262+ }
263+
240264 private static void commitWrites (BatchWriteBuilder batchWriteBuilder , BatchTableWrite writer )
241265 throws Exception {
242266 BatchTableCommit commit = batchWriteBuilder .newCommit ();
0 commit comments