exceptionfactory commented on code in PR #10013: URL: https://github.com/apache/nifi/pull/10013#discussion_r2193936669
########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.parquet.web.controller; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.nifi.authorization.AccessDeniedException; +import org.apache.nifi.web.ContentAccess; +import org.apache.nifi.web.ContentRequestContext; +import org.apache.nifi.web.DownloadableContent; +import org.apache.nifi.web.HttpServletContentRequestContext; +import org.apache.nifi.web.ResourceNotFoundException; +import org.apache.parquet.avro.AvroParquetReader; +import org.apache.parquet.hadoop.ParquetReader; +import org.apache.parquet.io.InputFile; +import org.apache.parquet.web.utils.NifiParquetContentViewerInputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class ParquetContentViewerController extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(ParquetContentViewerController.class); + + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { + final ContentRequestContext requestContext = new HttpServletContentRequestContext(request); + + final ServletContext servletContext = request.getServletContext(); + final ContentAccess contentAccess = (ContentAccess) servletContext.getAttribute("nifi-content-access"); + + // get the content + final DownloadableContent downloadableContent; + try { + downloadableContent = contentAccess.getContent(requestContext); + } catch (final ResourceNotFoundException e) { + logger.warn("Content not found", e); + response.sendError(HttpURLConnection.HTTP_NOT_FOUND, "Content not found"); + return; + } catch (final AccessDeniedException e) { + logger.warn("Content access denied", e); + response.sendError(HttpURLConnection.HTTP_FORBIDDEN, "Content access denied"); + return; + } catch (final Exception e) { + logger.warn("Content retrieval failed", e); + response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, "Content retrieval failed"); + return; + } + + response.setStatus(HttpServletResponse.SC_OK); + + final boolean formatted = Boolean.parseBoolean(request.getParameter("formatted")); + if (!formatted) { + final InputStream contentStream = downloadableContent.getContent(); + contentStream.transferTo(response.getOutputStream()); + return; + } + + // allow the user to drive the data type but fall back to the content type if necessary + String displayName = request.getParameter("mimeTypeDisplayName"); + if (displayName == null) { + displayName = downloadableContent.getType(); + } + + if (displayName == null || !(displayName.equals("parquet") || displayName.equals("application/parquet"))) { + response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unknown content type"); + return; + } + + try { + //Output will be in a fixed length table format. + List<String> fieldNames = new ArrayList<>(); + List<ArrayList<String>> columns = new ArrayList<>(); + + //Convert InputStream to a seekable InputStream + byte[] data = IOUtils.toByteArray(downloadableContent.getContent()); + Configuration conf = new Configuration(); + conf.setBoolean("parquet.avro.readInt96AsFixed", true); + + final InputFile inputFile = new NifiParquetContentViewerInputFile(new ByteArrayInputStream(data), data.length); + final ParquetReader<GenericRecord> reader = AvroParquetReader.<GenericRecord>builder(inputFile) + .withConf(conf) + .build(); + + //Get each column per record and save it to corresponding column list + GenericRecord record; + boolean needHeader = true; + while ((record = reader.read()) != null) { + //Get the field names from the schema + if (needHeader) { + record.getSchema().getFields().forEach(f -> { + fieldNames.add(f.name()); + ArrayList<String> column = new ArrayList<>(); Review Comment: The `List` type should always be used for the declared type instead of `ArrayList`: ```suggestion List<String> column = new ArrayList<>(); ``` ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.parquet.web.controller; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.nifi.authorization.AccessDeniedException; +import org.apache.nifi.web.ContentAccess; +import org.apache.nifi.web.ContentRequestContext; +import org.apache.nifi.web.DownloadableContent; +import org.apache.nifi.web.HttpServletContentRequestContext; +import org.apache.nifi.web.ResourceNotFoundException; +import org.apache.parquet.avro.AvroParquetReader; +import org.apache.parquet.hadoop.ParquetReader; +import org.apache.parquet.io.InputFile; +import org.apache.parquet.web.utils.NifiParquetContentViewerInputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class ParquetContentViewerController extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(ParquetContentViewerController.class); + + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { + final ContentRequestContext requestContext = new HttpServletContentRequestContext(request); + + final ServletContext servletContext = request.getServletContext(); + final ContentAccess contentAccess = (ContentAccess) servletContext.getAttribute("nifi-content-access"); + + // get the content + final DownloadableContent downloadableContent; + try { + downloadableContent = contentAccess.getContent(requestContext); + } catch (final ResourceNotFoundException e) { + logger.warn("Content not found", e); + response.sendError(HttpURLConnection.HTTP_NOT_FOUND, "Content not found"); + return; + } catch (final AccessDeniedException e) { + logger.warn("Content access denied", e); + response.sendError(HttpURLConnection.HTTP_FORBIDDEN, "Content access denied"); + return; + } catch (final Exception e) { + logger.warn("Content retrieval failed", e); + response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, "Content retrieval failed"); + return; + } + + response.setStatus(HttpServletResponse.SC_OK); + + final boolean formatted = Boolean.parseBoolean(request.getParameter("formatted")); + if (!formatted) { + final InputStream contentStream = downloadableContent.getContent(); + contentStream.transferTo(response.getOutputStream()); + return; + } + + // allow the user to drive the data type but fall back to the content type if necessary + String displayName = request.getParameter("mimeTypeDisplayName"); + if (displayName == null) { + displayName = downloadableContent.getType(); + } + + if (displayName == null || !(displayName.equals("parquet") || displayName.equals("application/parquet"))) { + response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unknown content type"); + return; + } + + try { + //Output will be in a fixed length table format. + List<String> fieldNames = new ArrayList<>(); + List<ArrayList<String>> columns = new ArrayList<>(); + + //Convert InputStream to a seekable InputStream + byte[] data = IOUtils.toByteArray(downloadableContent.getContent()); + Configuration conf = new Configuration(); + conf.setBoolean("parquet.avro.readInt96AsFixed", true); + + final InputFile inputFile = new NifiParquetContentViewerInputFile(new ByteArrayInputStream(data), data.length); + final ParquetReader<GenericRecord> reader = AvroParquetReader.<GenericRecord>builder(inputFile) + .withConf(conf) + .build(); + + //Get each column per record and save it to corresponding column list + GenericRecord record; + boolean needHeader = true; + while ((record = reader.read()) != null) { + //Get the field names from the schema + if (needHeader) { + record.getSchema().getFields().forEach(f -> { + fieldNames.add(f.name()); + ArrayList<String> column = new ArrayList<>(); + column.add(f.name()); + columns.add(column); + }); + needHeader = false; + } + + for (int i = 0; i < fieldNames.size(); i++) { + Object value = record.get(fieldNames.get(i)); + if (value != null) { + columns.get(i).add(value.toString()); + } else { + columns.get(i).add(""); + } + } + } + + //Get the maximum length for each column including the header. + int[] maxLengths = new int[fieldNames.size()]; + for (int i = 0; i < columns.size(); i++) { + maxLengths[i] = columns.get(i).stream() + .max(Comparator.comparingInt(String::length)) + .orElse("") + .length(); + } + + //Print out each field with a fixed length + for (int i = 0; i < columns.getFirst().size(); i++) { + StringBuilder row = new StringBuilder(); Review Comment: Building the row in memory does not seem necessary, it should be possible to write to the response OutputStream. ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.parquet.web.controller; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.nifi.authorization.AccessDeniedException; +import org.apache.nifi.web.ContentAccess; +import org.apache.nifi.web.ContentRequestContext; +import org.apache.nifi.web.DownloadableContent; +import org.apache.nifi.web.HttpServletContentRequestContext; +import org.apache.nifi.web.ResourceNotFoundException; +import org.apache.parquet.avro.AvroParquetReader; +import org.apache.parquet.hadoop.ParquetReader; +import org.apache.parquet.io.InputFile; +import org.apache.parquet.web.utils.NifiParquetContentViewerInputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class ParquetContentViewerController extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(ParquetContentViewerController.class); + + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { + final ContentRequestContext requestContext = new HttpServletContentRequestContext(request); + + final ServletContext servletContext = request.getServletContext(); + final ContentAccess contentAccess = (ContentAccess) servletContext.getAttribute("nifi-content-access"); + + // get the content + final DownloadableContent downloadableContent; + try { + downloadableContent = contentAccess.getContent(requestContext); + } catch (final ResourceNotFoundException e) { + logger.warn("Content not found", e); + response.sendError(HttpURLConnection.HTTP_NOT_FOUND, "Content not found"); + return; + } catch (final AccessDeniedException e) { + logger.warn("Content access denied", e); + response.sendError(HttpURLConnection.HTTP_FORBIDDEN, "Content access denied"); + return; + } catch (final Exception e) { + logger.warn("Content retrieval failed", e); + response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, "Content retrieval failed"); + return; + } + + response.setStatus(HttpServletResponse.SC_OK); + + final boolean formatted = Boolean.parseBoolean(request.getParameter("formatted")); + if (!formatted) { + final InputStream contentStream = downloadableContent.getContent(); + contentStream.transferTo(response.getOutputStream()); + return; + } + + // allow the user to drive the data type but fall back to the content type if necessary + String displayName = request.getParameter("mimeTypeDisplayName"); + if (displayName == null) { + displayName = downloadableContent.getType(); + } + + if (displayName == null || !(displayName.equals("parquet") || displayName.equals("application/parquet"))) { + response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unknown content type"); + return; + } + + try { + //Output will be in a fixed length table format. + List<String> fieldNames = new ArrayList<>(); + List<ArrayList<String>> columns = new ArrayList<>(); + + //Convert InputStream to a seekable InputStream + byte[] data = IOUtils.toByteArray(downloadableContent.getContent()); Review Comment: This could easily use up available memory if the file is large. Is there a particular reason for reading the entire content as opposed to passing the content stream to the InputFile wrapper? ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/pom.xml: ########## @@ -0,0 +1,184 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-parquet-bundle</artifactId> + <version>2.5.0-SNAPSHOT</version> + </parent> + <artifactId>nifi-parquet-content-viewer</artifactId> + <packaging>war</packaging> + <properties> + <source.skip>true</source.skip> + <standard-content-viewer.ui.working.dir>${project.build.directory}/standard-content-viewer-ui-working-directory</standard-content-viewer.ui.working.dir> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.hadoop</groupId> + <artifactId>hadoop-mapreduce-client-core</artifactId> + <version>3.4.1</version> + <scope>compile</scope> + <exclusions> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-reload4j</artifactId> + </exclusion> + <exclusion> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-log4j12</artifactId> + </exclusion> + <exclusion> + <groupId>log4j</groupId> + <artifactId>log4j</artifactId> + </exclusion> + <exclusion> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + </exclusion> + <exclusion> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + </exclusion> + <!-- Exclude Jetty 9.4 --> + <exclusion> + <groupId>org.eclipse.jetty.websocket</groupId> + <artifactId>websocket-client</artifactId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-framework-api</artifactId> + <version>2.5.0-SNAPSHOT</version> + <scope>provided</scope> <!-- expected to be provided by parent classloader --> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-content-viewer-utils</artifactId> + <version>2.5.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-web-servlet-shared</artifactId> + <version>2.5.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-frontend</artifactId> + <version>2.5.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.glassfish.jersey.core</groupId> + <artifactId>jersey-common</artifactId> + </dependency> + <dependency> + <groupId>org.apache.parquet</groupId> + <artifactId>parquet-common</artifactId> + <version>1.15.2</version> Review Comment: The Parquet version should be moved to a property in this value and reused to keep the numbers aligned. ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.parquet.web.controller; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.nifi.authorization.AccessDeniedException; +import org.apache.nifi.web.ContentAccess; +import org.apache.nifi.web.ContentRequestContext; +import org.apache.nifi.web.DownloadableContent; +import org.apache.nifi.web.HttpServletContentRequestContext; +import org.apache.nifi.web.ResourceNotFoundException; +import org.apache.parquet.avro.AvroParquetReader; +import org.apache.parquet.hadoop.ParquetReader; +import org.apache.parquet.io.InputFile; +import org.apache.parquet.web.utils.NifiParquetContentViewerInputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class ParquetContentViewerController extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(ParquetContentViewerController.class); + + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { + final ContentRequestContext requestContext = new HttpServletContentRequestContext(request); + + final ServletContext servletContext = request.getServletContext(); + final ContentAccess contentAccess = (ContentAccess) servletContext.getAttribute("nifi-content-access"); + + // get the content + final DownloadableContent downloadableContent; + try { + downloadableContent = contentAccess.getContent(requestContext); + } catch (final ResourceNotFoundException e) { + logger.warn("Content not found", e); + response.sendError(HttpURLConnection.HTTP_NOT_FOUND, "Content not found"); + return; + } catch (final AccessDeniedException e) { + logger.warn("Content access denied", e); + response.sendError(HttpURLConnection.HTTP_FORBIDDEN, "Content access denied"); + return; + } catch (final Exception e) { + logger.warn("Content retrieval failed", e); + response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, "Content retrieval failed"); + return; + } + + response.setStatus(HttpServletResponse.SC_OK); + + final boolean formatted = Boolean.parseBoolean(request.getParameter("formatted")); + if (!formatted) { + final InputStream contentStream = downloadableContent.getContent(); + contentStream.transferTo(response.getOutputStream()); + return; + } + + // allow the user to drive the data type but fall back to the content type if necessary + String displayName = request.getParameter("mimeTypeDisplayName"); + if (displayName == null) { + displayName = downloadableContent.getType(); + } + + if (displayName == null || !(displayName.equals("parquet") || displayName.equals("application/parquet"))) { Review Comment: See note on official Media Type naming. ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java: ########## @@ -0,0 +1,157 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.parquet.web.controller; + +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.apache.avro.generic.GenericRecord; +import org.apache.commons.io.IOUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.nifi.authorization.AccessDeniedException; +import org.apache.nifi.web.ContentAccess; +import org.apache.nifi.web.ContentRequestContext; +import org.apache.nifi.web.DownloadableContent; +import org.apache.nifi.web.HttpServletContentRequestContext; +import org.apache.nifi.web.ResourceNotFoundException; +import org.apache.parquet.avro.AvroParquetReader; +import org.apache.parquet.hadoop.ParquetReader; +import org.apache.parquet.io.InputFile; +import org.apache.parquet.web.utils.NifiParquetContentViewerInputFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class ParquetContentViewerController extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(ParquetContentViewerController.class); + + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { + final ContentRequestContext requestContext = new HttpServletContentRequestContext(request); + + final ServletContext servletContext = request.getServletContext(); + final ContentAccess contentAccess = (ContentAccess) servletContext.getAttribute("nifi-content-access"); + + // get the content + final DownloadableContent downloadableContent; + try { + downloadableContent = contentAccess.getContent(requestContext); + } catch (final ResourceNotFoundException e) { + logger.warn("Content not found", e); + response.sendError(HttpURLConnection.HTTP_NOT_FOUND, "Content not found"); + return; + } catch (final AccessDeniedException e) { + logger.warn("Content access denied", e); + response.sendError(HttpURLConnection.HTTP_FORBIDDEN, "Content access denied"); + return; + } catch (final Exception e) { + logger.warn("Content retrieval failed", e); + response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR, "Content retrieval failed"); + return; + } + + response.setStatus(HttpServletResponse.SC_OK); + + final boolean formatted = Boolean.parseBoolean(request.getParameter("formatted")); + if (!formatted) { + final InputStream contentStream = downloadableContent.getContent(); + contentStream.transferTo(response.getOutputStream()); + return; + } + + // allow the user to drive the data type but fall back to the content type if necessary + String displayName = request.getParameter("mimeTypeDisplayName"); + if (displayName == null) { + displayName = downloadableContent.getType(); + } + + if (displayName == null || !(displayName.equals("parquet") || displayName.equals("application/parquet"))) { + response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unknown content type"); + return; + } + + try { + //Output will be in a fixed length table format. + List<String> fieldNames = new ArrayList<>(); + List<ArrayList<String>> columns = new ArrayList<>(); Review Comment: ```suggestion List<List<String>> columns = new ArrayList<>(); ``` ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/pom.xml: ########## @@ -0,0 +1,184 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.nifi</groupId> + <artifactId>nifi-parquet-bundle</artifactId> + <version>2.5.0-SNAPSHOT</version> + </parent> + <artifactId>nifi-parquet-content-viewer</artifactId> + <packaging>war</packaging> + <properties> + <source.skip>true</source.skip> + <standard-content-viewer.ui.working.dir>${project.build.directory}/standard-content-viewer-ui-working-directory</standard-content-viewer.ui.working.dir> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.hadoop</groupId> + <artifactId>hadoop-mapreduce-client-core</artifactId> + <version>3.4.1</version> Review Comment: This should use the Hadoop version variable instead of the specific version number. ########## nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/webapp/META-INF/nifi-content-viewer: ########## @@ -0,0 +1,15 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +parquet=application/parquet Review Comment: The standard MIME Type appears to be [application/vnd.apache.parquet](https://www.iana.org/assignments/media-types/application/vnd.apache.parquet) so that seems the the appropriate value for this setting. ########## nifi-extension-bundles/nifi-parquet-bundle/pom.xml: ########## @@ -25,8 +25,10 @@ <packaging>pom</packaging> <modules> + <module>nifi-parquet-content-viewer</module> Review Comment: This should be moved directly before the `nifi-parquet-content-viewer-nar` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
