content: /*
   * 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 samples;
  
  import com.aliyun.oss.event.ProgressEvent;
  import com.aliyun.oss.event.ProgressListener;
  import com.aliyun.oss.model.*;
  import com.aliyun.oss.OSS;
  import com.aliyun.oss.OSSClientBuilder;
  
  import java.io.BufferedOutputStream;
  import java.io.ByteArrayInputStream;
  import java.io.FileOutputStream;
  import java.io.InputStream;
  
  /**
   * Examples of create select object metadata and select object.
   *
   */
  public class SelectObjectSample {
      private static String endpoint = "<endpoint, http://oss-cn-hangzhou.aliyuncs.com>";
      private static String accessKeyId = "<accessKeyId>";
      private static String accessKeySecret = "<accessKeySecret>";
      private static String bucketName = "<bucketName>";
  
      public static void main(String[] args) throws Exception {
          OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
          selectCsvSample("test.csv", client);
          selectJsonSample("test.json", client);
          client.shutdown();
      }
  
      private static void selectCsvSample(String key, OSS client) throws Exception {
          String content = "name,school,company,age\r\n" +
                  "Lora Francis,School A,Staples Inc,27\r\n" +
                  "Eleanor Little,School B,\"Conectiv, Inc\",43\r\n" +
                  "Rosie Hughes,School C,Western Gas Resources Inc,44\r\n" +
                  "Lawrence Ross,School D,MetLife Inc.,24";
  
          client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
  
          SelectObjectMetadata selectObjectMetadata = client.createSelectObjectMetadata(
                  new CreateSelectObjectMetadataRequest(bucketName, key)
                          .withInputSerialization(
                                  new InputSerialization().withCsvInputFormat(
                                          new CSVFormat().withHeaderInfo(CSVFormat.Header.Use).withRecordDelimiter("\r\n"))));
          System.out.println(selectObjectMetadata.getCsvObjectMetadata().getTotalLines());
          System.out.println(selectObjectMetadata.getCsvObjectMetadata().getSplits());
  
          SelectObjectRequest selectObjectRequest =
                  new SelectObjectRequest(bucketName, key)
                          .withInputSerialization(
                                  new InputSerialization().withCsvInputFormat(
                                          new CSVFormat().withHeaderInfo(CSVFormat.Header.Use).withRecordDelimiter("\r\n")))
                          .withOutputSerialization(new OutputSerialization().withCsvOutputFormat(new CSVFormat()));
          selectObjectRequest.setExpression("select * from ossobject where _4 > 40");
          OSSObject ossObject = client.selectObject(selectObjectRequest);
          // read object content from ossObject
          writeToFile(ossObject.getObjectContent(), "result.csv");
      }
  
      private static void selectJsonSample(String key, OSS client) throws Exception {
          final String content = "{\n" +
                  "\t\"name\": \"Lora Francis\",\n" +
                  "\t\"age\": 27,\n" +
                  "\t\"company\": \"Staples Inc\"\n" +
                  "}\n" +
                  "{\n" +
                  "\t\"name\": \"Eleanor Little\",\n" +
                  "\t\"age\": 43,\n" +
                  "\t\"company\": \"Conectiv, Inc\"\n" +
                  "}\n" +
                  "{\n" +
                  "\t\"name\": \"Rosie Hughes\",\n" +
                  "\t\"age\": 44,\n" +
                  "\t\"company\": \"Western Gas Resources Inc\"\n" +
                  "}\n" +
                  "{\n" +
                  "\t\"name\": \"Lawrence Ross\",\n" +
                  "\t\"age\": 24,\n" +
                  "\t\"company\": \"MetLife Inc.\"\n" +
                  "}";
          client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
          SelectObjectRequest selectObjectRequest =
                  new SelectObjectRequest(bucketName, key)
                          .withInputSerialization(new InputSerialization()
                                  .withCompressionType(CompressionType.NONE)
                                  .withJsonInputFormat(new JsonFormat().withJsonType(JsonType.LINES)))
                          .withOutputSerialization(new OutputSerialization()
                                  .withCrcEnabled(true)
                                  .withJsonOutputFormat(new JsonFormat()))
                          .withExpression("select * from ossobject as s where s.age > 40");
  
          OSSObject ossObject = client.selectObject(selectObjectRequest);
          writeToFile(ossObject.getObjectContent(), "result.json");
      }
  
      private static void writeToFile(InputStream in, String file) throws Exception {
          BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
          byte[] buffer = new byte[1024];
          int bytesRead;
          while ((bytesRead = in.read(buffer)) != -1) {
              outputStream.write(buffer, 0, bytesRead);
          }
          outputStream.close();
      }
  }
  