/**
  * 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
  * <p>
  * http://www.apache.org/licenses/LICENSE-2.0
  * <p>
  * 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 com.aliyun.odps.spark.oss

import org.apache.spark.{SparkConf, SparkContext}

object JindoFsDemo {
  def main(args: Array[String]): Unit = {
    val aliyunUid : String = args(0)
    val role : String = args(1)
    val bucket : String = args(2)
    val ossPath : String = args(3)

    // using ram-role assume
    val conf = new SparkConf()
      .setAppName("jindo-fs-demo")
      .set("spark.hadoop.fs.AbstractFileSystem.oss.impl", "com.aliyun.emr.fs.oss.OSS")
      .set("spark.hadoop.fs.oss.impl", "com.aliyun.emr.fs.oss.JindoOssFileSystem")
      .set("spark.hadoop.fs.oss.endpoint", "oss-cn-hangzhou-internal.aliyuncs.com")
      .set("spark.hadoop.fs.jfs.cache.oss.credentials.provider", "com.aliyun.emr.fs.auth.CustomCredentialsProvider")
      .set("spark.hadoop.aliyun.oss.provider.url", s"http://localhost:10011/sts-token-info?user_id=${aliyunUid}&role=${role}")


    //using access-key-id/access-key-secret
//    val conf = new SparkConf()
//      .setAppName("jindo-fs-demo")
//      .set("spark.hadoop.fs.AbstractFileSystem.oss.impl", "com.aliyun.emr.fs.oss.OSS")
//      .set("spark.hadoop.fs.oss.impl", "com.aliyun.emr.fs.oss.JindoOssFileSystem")
//      .set("spark.hadoop.fs.oss.endpoint", "oss-cn-hangzhou-internal.aliyuncs.com")
//      .set("spark.hadoop.fs.oss.accessKeyId", "xxx")
//      .set("spark.hadoop.fs.oss.accessKeySecret", "xxx")

    val sc = new SparkContext(conf)

    try {
      read_oss_dir(sc, "demo", s"oss://${bucket}/${ossPath}")
    } finally {
      sc.stop()
    }
  }

  /**
    * compute cost time using jindo sdk
    */
  def read_oss_dir(sc: SparkContext, job_des:String, ossPath: String): Unit = {
    val startTime: Long = System.currentTimeMillis()
    val inputData = sc.textFile(ossPath, 20)
    val cnt = inputData.count
    val endTime:Long = System.currentTimeMillis()
    val cost:Long = endTime - startTime
    println(s"job:$job_des, count:$cnt, consume:$cost")
  }
}
