Freedom9339 commented on code in PR #10013:
URL: https://github.com/apache/nifi/pull/10013#discussion_r2225972269


##########
nifi-extension-bundles/nifi-parquet-bundle/nifi-parquet-content-viewer/src/main/java/org/apache/parquet/web/controller/ParquetContentViewerController.java:
##########
@@ -0,0 +1,167 @@
+/*
+ * 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 com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParser;
+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.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.nifi.parquet.shared.NifiParquetInputFile;
+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.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/vnd.apache.parquet"))) {
+            response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Unknown 
content type");
+            return;
+        }
+
+        try {
+            //Convert InputStream to a seekable InputStream
+            long maxBytes = 1024 * 1024 * 2; //10MB
+            byte[] data = 
getInputStreamBytes(downloadableContent.getContent(), maxBytes);
+
+            if (data.length < 1) {
+                response.getOutputStream().write("Content size is too large to 
display.".getBytes());
+                return;
+            }
+
+            Configuration conf = new Configuration();
+            conf.setBoolean("parquet.avro.readInt96AsFixed", true);
+
+            final InputFile inputFile = new NifiParquetInputFile(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;
+            Gson gson = new GsonBuilder().setPrettyPrinting().create();
+            boolean firstRecord = true;
+
+            while ((record = reader.read()) != null) {
+                if (firstRecord) {
+                    firstRecord = false;
+                } else {
+                    response.getOutputStream().write(",\n".getBytes());
+                }
+
+                
response.getOutputStream().write(gson.toJson(JsonParser.parseString(record.toString())).getBytes());

Review Comment:
   I refactored to use Jackson, But I'm not sure how to make it to where record 
variable is not needed.



-- 
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]

Reply via email to