5959@ Tag ("functional" )
6060public class TestFileSystemViewCommand extends CLIFunctionalTestHarness {
6161
62+ private String unpartitionedTablePath ;
63+ private String partitionedTablePath ;
6264 private String partitionPath ;
63- private SyncableFileSystemView fsView ;
65+ private SyncableFileSystemView unpartitionedFsView ;
66+ private SyncableFileSystemView partitionedFsView ;
6467
6568 @ BeforeEach
6669 public void init () throws IOException {
70+ createUnpartitionedTable ();
71+ createPartitionedTable ();
72+ }
73+
74+ private void createUnpartitionedTable () throws IOException {
6775 HoodieCLI .conf = hadoopConf ();
6876
6977 // Create table and connect
70- String tableName = tableName ();
71- String tablePath = tablePath (tableName );
78+ String unpartitionedTableName = "unpartitioned_" + tableName ();
79+ unpartitionedTablePath = tablePath (unpartitionedTableName );
7280 new TableCommand ().createTable (
73- tablePath , tableName ,
74- "COPY_ON_WRITE" , "" , 1 , "org.apache.hudi.common.model.HoodieAvroPayload" );
81+ unpartitionedTablePath , unpartitionedTableName ,
82+ "COPY_ON_WRITE" , "" , 1 , "org.apache.hudi.common.model.HoodieAvroPayload" );
83+
84+ HoodieTableMetaClient metaClient = HoodieCLI .getTableMetaClient ();
85+
86+ Files .createDirectories (Paths .get (unpartitionedTablePath ));
87+
88+ // Generate 2 commits
89+ String commitTime1 = "3" ;
90+ String commitTime2 = "4" ;
91+
92+ String fileId1 = UUID .randomUUID ().toString ();
93+
94+ // Write date files and log file
95+ String testWriteToken = "2-0-2" ;
96+ Files .createFile (Paths .get (unpartitionedTablePath , FSUtils
97+ .makeDataFileName (commitTime1 , testWriteToken , fileId1 )));
98+ Files .createFile (Paths .get (unpartitionedTablePath , FSUtils
99+ .makeLogFileName (fileId1 , HoodieLogFile .DELTA_EXTENSION , commitTime1 , 0 , testWriteToken )));
100+ Files .createFile (Paths .get (unpartitionedTablePath , FSUtils
101+ .makeDataFileName (commitTime2 , testWriteToken , fileId1 )));
102+ Files .createFile (Paths .get (unpartitionedTablePath , FSUtils
103+ .makeLogFileName (fileId1 , HoodieLogFile .DELTA_EXTENSION , commitTime2 , 0 , testWriteToken )));
104+
105+ // Write commit files
106+ Files .createFile (Paths .get (unpartitionedTablePath , ".hoodie" , commitTime1 + ".commit" ));
107+ Files .createFile (Paths .get (unpartitionedTablePath , ".hoodie" , commitTime2 + ".commit" ));
108+
109+ // Reload meta client and create fsView
110+ metaClient = HoodieTableMetaClient .reload (metaClient );
111+
112+ unpartitionedFsView = new HoodieTableFileSystemView (metaClient , metaClient .getActiveTimeline (), true );
113+ }
114+
115+ private void createPartitionedTable () throws IOException {
116+ HoodieCLI .conf = hadoopConf ();
117+
118+ // Create table and connect
119+ String partitionedTableName = "partitioned_" + tableName ();
120+ partitionedTablePath = tablePath (partitionedTableName );
121+ new TableCommand ().createTable (
122+ partitionedTablePath , partitionedTableName ,
123+ "COPY_ON_WRITE" , "" , 1 , "org.apache.hudi.common.model.HoodieAvroPayload" );
75124
76125 HoodieTableMetaClient metaClient = HoodieCLI .getTableMetaClient ();
77126
78127 partitionPath = HoodieTestCommitMetadataGenerator .DEFAULT_FIRST_PARTITION_PATH ;
79- String fullPartitionPath = Paths .get (tablePath , partitionPath ).toString ();
128+ String fullPartitionPath = Paths .get (partitionedTablePath , partitionPath ).toString ();
80129 Files .createDirectories (Paths .get (fullPartitionPath ));
81130
82131 // Generate 2 commits
@@ -97,13 +146,13 @@ public void init() throws IOException {
97146 .makeLogFileName (fileId1 , HoodieLogFile .DELTA_EXTENSION , commitTime2 , 0 , testWriteToken )));
98147
99148 // Write commit files
100- Files .createFile (Paths .get (tablePath , ".hoodie" , commitTime1 + ".commit" ));
101- Files .createFile (Paths .get (tablePath , ".hoodie" , commitTime2 + ".commit" ));
149+ Files .createFile (Paths .get (partitionedTablePath , ".hoodie" , commitTime1 + ".commit" ));
150+ Files .createFile (Paths .get (partitionedTablePath , ".hoodie" , commitTime2 + ".commit" ));
102151
103152 // Reload meta client and create fsView
104153 metaClient = HoodieTableMetaClient .reload (metaClient );
105154
106- fsView = new HoodieTableFileSystemView (metaClient , metaClient .getActiveTimeline (), true );
155+ partitionedFsView = new HoodieTableFileSystemView (metaClient , metaClient .getActiveTimeline (), true );
107156 }
108157
109158 /**
@@ -116,7 +165,7 @@ public void testShowCommits() {
116165 assertTrue (cr .isSuccess ());
117166
118167 // Get all file groups
119- Stream <HoodieFileGroup > fileGroups = fsView .getAllFileGroups (partitionPath );
168+ Stream <HoodieFileGroup > fileGroups = partitionedFsView .getAllFileGroups (partitionPath );
120169
121170 List <Comparable []> rows = new ArrayList <>();
122171 fileGroups .forEach (fg -> fg .getAllFileSlices ().forEach (fs -> {
@@ -164,7 +213,7 @@ public void testShowCommitsWithSpecifiedValues() {
164213 assertTrue (cr .isSuccess ());
165214
166215 List <Comparable []> rows = new ArrayList <>();
167- Stream <HoodieFileGroup > fileGroups = fsView .getAllFileGroups (partitionPath );
216+ Stream <HoodieFileGroup > fileGroups = partitionedFsView .getAllFileGroups (partitionPath );
168217
169218 // Only get instant 1, since maxInstant was specified 2
170219 fileGroups .forEach (fg -> fg .getAllFileSlices ().filter (fs -> fs .getBaseInstantTime ().equals ("1" )).forEach (fs -> {
@@ -197,17 +246,7 @@ public void testShowCommitsWithSpecifiedValues() {
197246 assertEquals (expected , got );
198247 }
199248
200- /**
201- * Test case for command 'show fsview latest'.
202- */
203- @ Test
204- public void testShowLatestFileSlices () {
205- // Test show with partition path '2016/03/15'
206- CommandResult cr = shell ().executeCommand ("show fsview latest --partitionPath " + partitionPath );
207- assertTrue (cr .isSuccess ());
208-
209- Stream <FileSlice > fileSlice = fsView .getLatestFileSlices (partitionPath );
210-
249+ private List <Comparable []> fileSlicesToCRList (Stream <FileSlice > fileSlice , String partitionPath ) {
211250 List <Comparable []> rows = new ArrayList <>();
212251 fileSlice .forEach (fs -> {
213252 int idx = 0 ;
@@ -245,7 +284,14 @@ public void testShowLatestFileSlices() {
245284 .collect (Collectors .toList ()).toString ();
246285 rows .add (row );
247286 });
287+ return rows ;
288+ }
248289
290+ /**(
291+ * Test case for command 'show fsview latest'.
292+ */
293+ @ Test
294+ public void testShowLatestFileSlices () throws IOException {
249295 Function <Object , String > converterFunction =
250296 entry -> NumericUtils .humanReadableByteCount ((Double .parseDouble (entry .toString ())));
251297 Map <String , Function <Object , String >> fieldNameToConverterMap = new HashMap <>();
@@ -267,9 +313,32 @@ public void testShowLatestFileSlices() {
267313 .addTableHeaderField (HoodieTableHeaderFields .HEADER_DELTA_BASE_UNSCHEDULED )
268314 .addTableHeaderField (HoodieTableHeaderFields .HEADER_DELTA_FILES_SCHEDULED )
269315 .addTableHeaderField (HoodieTableHeaderFields .HEADER_DELTA_FILES_UNSCHEDULED );
270- String expected = HoodiePrintHelper .print (header , fieldNameToConverterMap , "" , false , -1 , false , rows );
271- expected = removeNonWordAndStripSpace (expected );
272- String got = removeNonWordAndStripSpace (cr .getResult ().toString ());
273- assertEquals (expected , got );
316+
317+ // Test show with partition path '2016/03/15'
318+ new TableCommand ().connect (partitionedTablePath , null , false , 0 , 0 , 0 );
319+ CommandResult partitionedTableCR = shell ().executeCommand ("show fsview latest --partitionPath " + partitionPath );
320+ assertTrue (partitionedTableCR .isSuccess ());
321+
322+ Stream <FileSlice > partitionedFileSlice = partitionedFsView .getLatestFileSlices (partitionPath );
323+
324+ List <Comparable []> partitionedRows = fileSlicesToCRList (partitionedFileSlice , partitionPath );
325+ String partitionedExpected = HoodiePrintHelper .print (header , fieldNameToConverterMap , "" , false , -1 , false , partitionedRows );
326+ partitionedExpected = removeNonWordAndStripSpace (partitionedExpected );
327+ String partitionedResults = removeNonWordAndStripSpace (partitionedTableCR .getResult ().toString ());
328+ assertEquals (partitionedExpected , partitionedResults );
329+
330+ // Test show for unpartitioned table
331+ new TableCommand ().connect (unpartitionedTablePath , null , false , 0 , 0 , 0 );
332+ CommandResult unpartitionedTableCR = shell ().executeCommand ("show fsview latest" );
333+ assertTrue (unpartitionedTableCR .isSuccess ());
334+
335+ Stream <FileSlice > unpartitionedFileSlice = unpartitionedFsView .getLatestFileSlices ("" );
336+
337+ List <Comparable []> unpartitionedRows = fileSlicesToCRList (unpartitionedFileSlice , "" );
338+
339+ String unpartitionedExpected = HoodiePrintHelper .print (header , fieldNameToConverterMap , "" , false , -1 , false , unpartitionedRows );
340+ unpartitionedExpected = removeNonWordAndStripSpace (unpartitionedExpected );
341+ String unpartitionedResults = removeNonWordAndStripSpace (unpartitionedTableCR .getResult ().toString ());
342+ assertEquals (unpartitionedExpected , unpartitionedResults );
274343 }
275344}
0 commit comments