alanblueshift commented on a change in pull request #1018: Scan command now can 
produce results as csv and json. Related to #984
URL: https://github.com/apache/fluo/pull/1018#discussion_r173677616
 
 

 ##########
 File path: modules/core/src/main/java/org/apache/fluo/core/util/ScanUtil.java
 ##########
 @@ -89,68 +115,158 @@ public static Span getSpan(ScanOpts options) {
     return columns;
   }
 
-  public static void scanFluo(ScanOpts options, FluoConfiguration sConfig) {
+  public static void scanFluo(ScanOpts options, FluoConfiguration sConfig, 
PrintStream out)
+      throws IOException {
 
     try (FluoClient client = FluoFactory.newClient(sConfig)) {
       try (Snapshot s = client.newSnapshot()) {
 
-        Span span = null;
-        Collection<Column> columns = null;
-        try {
-          span = getSpan(options);
-          columns = getColumns(options);
-        } catch (IllegalArgumentException e) {
-          System.err.println(e.getMessage());
-          System.exit(-1);
+        Span span = getSpan(options);
+        Collection<Column> columns = getColumns(options);
+
+        // Retrieve "fluo.scan.*" from config properties.
+        SimpleConfiguration scanProperties = sConfig.getScanConfiguration();
+        if (options.exportAsJson) {
+          generateJson(options, scanProperties, span, columns, s, out);
+        } else { // TSV or CSV format
+          generateTsvCsv(options, scanProperties, span, columns, s, out);
         }
 
-        CellScanner cellScanner = 
s.scanner().over(span).fetch(columns).build();
-
-        StringBuilder sb = new StringBuilder();
-        for (RowColumnValue rcv : cellScanner) {
-          if (options.hexEncNonAscii) {
-            sb.setLength(0);
-            Hex.encNonAscii(sb, rcv.getRow());
-            sb.append(" ");
-            Hex.encNonAscii(sb, rcv.getColumn(), " ");
-            sb.append("\t");
-            Hex.encNonAscii(sb, rcv.getValue());
-            System.out.println(sb.toString());
-          } else {
-            sb.setLength(0);
-            sb.append(rcv.getsRow());
-            sb.append(" ");
-            sb.append(rcv.getColumn());
-            sb.append("\t");
-            sb.append(rcv.getsValue());
-            System.out.println(sb.toString());
-          }
+      } catch (FluoException e) {
+        throw e;
+      }
+    }
+  }
 
-          if (System.out.checkError()) {
-            break;
-          }
+  /**
+   * Generate TSV or CSV format as result of the scan.
+   * 
+   * @since 1.2
+   */
+  private static void generateTsvCsv(ScanOpts options, SimpleConfiguration 
scan, Span span,
+      Collection<Column> columns, final Snapshot snapshot, PrintStream out) 
throws IOException {
+    // Default TAB separator and NO quotes if possible.
+    CSVFormat csvFormat = CSVFormat.DEFAULT;
+    csvFormat = csvFormat.withDelimiter(CSVFormat.TDF.getDelimiter());
+    csvFormat = csvFormat.withQuoteMode(QuoteMode.MINIMAL);
+
+    // when "--csv" parameter is passed the "fluo.scan.csv" is analised
+    if (options.exportAsCsv) {
 
 Review comment:
   You were right! I fixed it!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to