You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hive.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2020/07/01 12:55:00 UTC

[jira] [Work logged] (HIVE-20447) Add JSON Outputformat support

     [ https://issues.apache.org/jira/browse/HIVE-20447?focusedWorklogId=453404&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-453404 ]

ASF GitHub Bot logged work on HIVE-20447:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Jul/20 12:54
            Start Date: 01/Jul/20 12:54
    Worklog Time Spent: 10m 
      Work Description: belugabehr commented on a change in pull request #1169:
URL: https://github.com/apache/hive/pull/1169#discussion_r448341358



##########
File path: pom.xml
##########
@@ -1486,6 +1486,7 @@
       <exclude>**/patchprocess/**</exclude>
       <exclude>**/metastore_db/**</exclude>
       <exclude>**/test/resources/**/*.ldif</exclude>
+      <exclude>.vscode/**</exclude>

Review comment:
       Since this has nothing to do with JSON formatting in beeline, save it for another JIRA.

##########
File path: beeline/src/test/org/apache/hive/beeline/TestJSONOutputFormat.java
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.hive.beeline;
+
+import static org.mockito.ArgumentMatchers.anyInt;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.io.PrintStream;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.ArrayList;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class TestJSONOutputFormat {
+
+  private final Object[][] mockRowData = {
+      {"aaa", true, null, Double.valueOf(3.14), "\\/\b\f\n\r\t"}
+  };
+  private TestJSONOutputFormat.BeelineMock mockBeeline;
+  private ResultSet mockResultSet;
+  private MockRow mockRow;
+
+  @Before
+  public void setupMockData() throws SQLException {
+    mockBeeline = new TestJSONOutputFormat.BeelineMock();
+    mockResultSet = mock(ResultSet.class);
+
+    ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
+    when(mockResultSetMetaData.getColumnCount()).thenReturn(5);
+    when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("string");
+    when(mockResultSetMetaData.getColumnLabel(2)).thenReturn("boolean");
+    when(mockResultSetMetaData.getColumnLabel(3)).thenReturn("null");
+    when(mockResultSetMetaData.getColumnLabel(4)).thenReturn("double");
+    when(mockResultSetMetaData.getColumnLabel(5)).thenReturn("special symbols");
+    when(mockResultSetMetaData.getColumnType(1)).thenReturn(Types.VARCHAR);
+    when(mockResultSetMetaData.getColumnType(2)).thenReturn(Types.BOOLEAN);
+    when(mockResultSetMetaData.getColumnType(3)).thenReturn(Types.NULL);
+    when(mockResultSetMetaData.getColumnType(4)).thenReturn(Types.DOUBLE);
+    when(mockResultSetMetaData.getColumnType(5)).thenReturn(Types.VARCHAR);
+    when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
+
+    mockRow = new MockRow();
+    // returns true as long as there is more data in mockResultData array
+    when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {
+      private int mockRowDataIndex = 0;
+
+      @Override
+      public Boolean answer(final InvocationOnMock invocation) {
+        if (mockRowDataIndex < mockRowData.length) {
+          mockRow.setCurrentRowData(mockRowData[mockRowDataIndex]);
+          mockRowDataIndex++;
+          return true;
+        } else {
+          return false;
+        }
+      }
+    });
+
+    when(mockResultSet.getObject(anyInt())).thenAnswer(new Answer<Object>() {
+      @Override
+      public Object answer(final InvocationOnMock invocation) {
+        Object[] args = invocation.getArguments();
+        int index = ((Integer) args[0]);
+        return mockRow.getColumn(index);
+      }
+    });
+  }
+
+  /**
+   * Test printing output data with JsonOutputFormat
+   */
+  @Test
+  public final void testPrint() throws SQLException {
+    setupMockData();
+    BufferedRows bfRows = new BufferedRows(mockBeeline, mockResultSet);
+    JSONOutputFormat instance = new JSONOutputFormat(mockBeeline);
+    instance.print(bfRows);
+    ArrayList<String> actualOutput = mockBeeline.getLines();
+    ArrayList<String> expectedOutput = new ArrayList<>(6);
+    expectedOutput.add("{\"resultset\":[");
+    expectedOutput.add("{\"string\":\"aaa\"," + "\"boolean\":true," + "\"null\":null," + "\"double\":3.14,"
+        + "\"special symbols\":\"\\\\\\/\\b\\f\\n\\r\\t\"}");
+    expectedOutput.add("]}");
+    assertArrayEquals(expectedOutput.toArray(), actualOutput.toArray());
+  }
+
+  public class BeelineMock extends BeeLine {
+
+    private ArrayList<String> lines = new ArrayList<>();
+
+    @Override
+    final void output(final ColorBuffer msg, boolean newline, PrintStream out) {
+      lines.add(msg.getMono());
+      super.output(msg, newline, out);
+    }
+
+    private ArrayList<String> getLines() {
+      return lines;
+    }
+  }
+
+  static class MockRow {
+    Object[] rowData;
+
+    public void setCurrentRowData(Object[] rowData) {
+      this.rowData = rowData;
+    }
+
+    public Object getColumn(int idx) {
+      return rowData[idx - 1];
+    }
+  }
+}

Review comment:
       Add a new line to end of file

##########
File path: beeline/src/java/org/apache/hive/beeline/JSONOutputFormat.java
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.
+ */
+
+/*
+ * This source file is based on code taken from SQLLine 1.9
+ * See SQLLine notice in LICENSE
+ */
+package org.apache.hive.beeline;
+
+import java.sql.SQLException;
+import java.sql.Types;
+
+import com.fasterxml.jackson.core.io.JsonStringEncoder;
+
+/**
+ * OutputFormat for standard JSON format.
+ *
+ */ 
+public class JSONOutputFormat extends AbstractOutputFormat {
+  private final BeeLine beeLine;
+  private int[] columnTypes;
+  private JsonStringEncoder jse = JsonStringEncoder.getInstance();

Review comment:
       Take a look at this:
   
   https://fasterxml.github.io/jackson-core/javadoc/2.8/com/fasterxml/jackson/core/JsonGenerator.html
   
   Please use the Jackson JSON generator to... generate JSON. :)

##########
File path: .gitignore
##########
@@ -38,3 +38,4 @@ standalone-metastore/metastore-server/src/gen/version
 launch.json
 settings.json
 kafka-handler/src/test/gen
+.vscode/

Review comment:
       Add this in a separate JIRA




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 453404)
    Time Spent: 1h 20m  (was: 1h 10m)

> Add JSON Outputformat support
> -----------------------------
>
>                 Key: HIVE-20447
>                 URL: https://issues.apache.org/jira/browse/HIVE-20447
>             Project: Hive
>          Issue Type: Task
>          Components: Beeline
>            Reporter: Max Efremov
>            Assignee: Hunter Logan
>            Priority: Major
>              Labels: pull-request-available
>         Attachments: HIVE-20447.01.patch
>
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> This function is present in SQLLine. We need add it to beeline too.
> https://github.com/julianhyde/sqlline/pull/84



--
This message was sent by Atlassian Jira
(v8.3.4#803005)