GithubHelp home page GithubHelp logo

kashenfelter / sparklyrexamples Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 8bit-pixies/sparklyrexamples

0.0 1.0 0.0 8 KB

A fork of sparkhello - for Chapman's personal use

License: MIT License

R 79.62% Scala 20.38%

sparklyrexamples's Introduction

sparkhello: Scala to Spark - Hello World

sparkhello demonstrates how to build a sparklyr extension package that uses custom Scala code which is compiled and deployed to Apache Spark.

For example, suppose that you want to deploy the following Scala code to Spark as part of your extension:

object HelloWorld {
  def hello() : String = {
    "Hello, world! - From Scala"
  }
}

The R wrapper for this Scala code might look like this:

spark_hello <- function(sc) {
  sparklyr::invoke_static(sc, "SparkHello.HelloWorld", "hello")
}

To package and deploy this Scala code as part of your extension, store the Scala code within the inst/scala directory of your package and then compile it using the following command, which will build the JARs required for various versions of Spark under the inst/java directory of your package:

sparklyr::compile_package_jars()

There are a couple of conditions required for sparklyr::compile_package_jars to work correctly:

  1. Your current working directory should be the root directory of your package.

  2. You should download and install the Scala 2.10 and 2.11 compilers to one of the following paths:

    • /opt/scala
    • /opt/local/scala
    • /usr/local/scala
    • ~/scala (Windows-only)

You then need to implement the spark_dependencies function (which tells sparklyr that your JARs are required dependencies) as well as an .onLoad function which registers your extension. For example:

spark_dependencies <- function(spark_version, scala_version, ...) {
  sparklyr::spark_dependency(
    jars = c(
      system.file(
        sprintf("java/sparkhello-%s-%s.jar", spark_version, scala_version),
        package = "sparkhello"
      )
    ),
    packages = c()
  )
}

.onLoad <- function(libname, pkgname) {
  sparklyr::register_extension(pkgname)
}

Assuming the sparkhello package was loaded prior to connecting to Spark, you can now call the spark_hello function which in turn executes the Scala code in your custom JAR:

library(sparklyr)
library(sparkhello)

sc <- spark_connect(master = "local")
spark_hello(sc)
## [1] "Hello, world! - From Scala"
spark_disconnect(sc)

You can learn more about sparklyr extensions at http://spark.rstudio.com/extensions.html.

sparklyrexamples's People

Contributors

8bit-pixies avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.