kotlin 使用Apache Spark和Kotlin计算字数

使用Apache Spark和Kotlin计算字数

spark.kt
package org.cakesolutions.spark

import org.apache.spark.SparkConf
import org.apache.spark.api.java.JavaSparkContext
import scala.Tuple2


fun main(args: Array<String>) {
    val inputFile = args[0]
    val outputFile = args[1]

    val conf = SparkConf().setAppName("wordCount")
    val sc = JavaSparkContext(conf)

    val input = sc.textFile(inputFile)
    val words = input.flatMap { x -> x.splitBy(" ") }

    val counts = words.mapToPair { x -> Tuple2(x, 1) }.reduceByKey { x, y -> x + y }

    counts.saveAsTextFile(outputFile)
}

kotlin Android数据库游标扩展,简化了对类型值的检索

Android数据库游标扩展,简化了对类型值的检索

CursorExtenstions.kt
import android.database.Cursor

/**
 * CursorExtentions
 *
 * Android database cursor extensions that simplify retrieval of typed values
 * @author alexfu
 */
fun Cursor.getInt(colName: String): Int {
  return this.getInt(this.getColumnIndex(colName))
}

fun Cursor.getString(colName: String): String {
  return this.getString(this.getColumnIndex(colName))
}

fun Cursor.getDouble(colName: String): Double {
  return this.getDouble(this.getColumnIndex(colName))
}

fun Cursor.getFloat(colName: String): Float {
  return this.getFloat(this.getColumnIndex(colName))
}

fun Cursor.getBlob(colName: String): ByteArray? {
  return this.getBlob(this.getColumnIndex(colName))
}

fun Cursor.getLong(colName: String): Long {
  return this.getLong(this.getColumnIndex(colName))
}

fun Cursor.getShort(colName: String): Short {
  return this.getShort(this.getColumnIndex(colName))
}