Skip to content

C# and F# language binding and extensions to Apache Spark

License

Notifications You must be signed in to change notification settings

microsoft/Mobius

Repository files navigation

Mobius development is deprecated and has been superseded by a more recent version '.NET for Apache Spark' from Microsoft (Website | GitHub) that runs on Azure HDInsight Spark, Amazon EMR Spark, Azure & AWS Databricks.

Mobius logo

Mobius: C# API for Spark

Mobius provides C# language binding to Apache Spark enabling the implementation of Spark driver program and data processing operations in the languages supported in the .NET framework like C# or F#.

For example, the word count sample in Apache Spark can be implemented in C# as follows :

varlines=sparkContext.TextFile(@"hdfs://path/to/input.txt");varwords=lines.FlatMap(s =>s.Split(' '));varwordCounts=words.Map(w =>newTuple<string,int>(w.Trim(),1)).ReduceByKey((x,y)=>x+y);varwordCountCollection=wordCounts.Collect();wordCounts.SaveAsTextFile(@"hdfs://path/to/wordcount.txt");

A simple DataFrame application using TempTable may look like the following:

varreqDataFrame=sqlContext.TextFile(@"hdfs://path/to/requests.csv");varmetricDataFrame=sqlContext.TextFile(@"hdfs://path/to/metrics.csv");reqDataFrame.RegisterTempTable("requests");metricDataFrame.RegisterTempTable("metrics");// C0 - guid in requests DataFrame, C3 - guid in metrics DataFrame varjoinDataFrame=GetSqlContext().Sql("SELECT joinedtable.datacenter"+", MAX(joinedtable.latency) maxlatency"+", AVG(joinedtable.latency) avglatency "+"FROM ("+"SELECT a.C1 as datacenter, b.C6 as latency "+"FROM requests a JOIN metrics b ON a.C0 = b.C3) joinedtable "+"GROUP BY datacenter");joinDataFrame.ShowSchema();joinDataFrame.Show();

A simple DataFrame application using DataFrame DSL may look like the following:

// C0 - guid, C1 - datacentervarreqDataFrame=sqlContext.TextFile(@"hdfs://path/to/requests.csv").Select("C0","C1");// C3 - guid, C6 - latency varmetricDataFrame=sqlContext.TextFile(@"hdfs://path/to/metrics.csv",",",false,true).Select("C3","C6");//override delimiter, hasHeader & inferSchemavarjoinDataFrame=reqDataFrame.Join(metricDataFrame,reqDataFrame["C0"]==metricDataFrame["C3"]).GroupBy("C1");varmaxLatencyByDcDataFrame=joinDataFrame.Agg(newDictionary<string,string>{{"C6","max"}});maxLatencyByDcDataFrame.ShowSchema();maxLatencyByDcDataFrame.Show();

A simple Spark Streaming application that processes messages from Kafka using C# may be implemented using the following code:

StreamingContextsparkStreamingContext=StreamingContext.GetOrCreate(checkpointPath,()=>{varssc=newStreamingContext(sparkContext,slideDurationInMillis);ssc.Checkpoint(checkpointPath);varstream=KafkaUtils.CreateDirectStream(ssc,topicList,kafkaParams,perTopicPartitionKafkaOffsets);//message format: [timestamp],[loglevel],[logmessage]varcountByLogLevelAndTime=stream.Map(kvp =>Encoding.UTF8.GetString(kvp.Value)).Filter(line =>line.Contains(",")).Map(line =>line.Split(',')).Map(columns =>newTuple<string,int>(string.Format("{0},{1}",columns[0],columns[1]),1)).ReduceByKeyAndWindow((x,y)=>x+y,(x,y)=>x-y,windowDurationInSecs,slideDurationInSecs,3).Map(logLevelCountPair =>string.Format("{0},{1}",logLevelCountPair.Key,logLevelCountPair.Value));countByLogLevelAndTime.ForeachRDD(countByLogLevel =>{foreach(varlogCountincountByLogLevel.Collect())Console.WriteLine(logCount);});returnssc;});sparkStreamingContext.Start();sparkStreamingContext.AwaitTermination();

For more code samples, refer to Mobius\examples directory or Mobius\csharp\Samples directory.

API Documentation

Refer to Mobius C# API documentation for the list of Spark's data processing operations supported in Mobius.

API Usage

Mobius API usage samples are available at:

  • Examples folder which contains standalone C# and F# projects that can be used as templates to start developing Mobius applications

  • Samples project which uses a comprehensive set of Mobius APIs to implement samples that are also used for functional validation of APIs

  • Mobius performance test scenarios implemented in C# and Scala for side by side comparison of Spark driver code

Documents

Refer to the docs folder for design overview and other info on Mobius

Build Status

Ubuntu 14.04.3 LTSWindowsUnit test coverage
Build statusBuild statuscodecov.io

Getting Started

WindowsLinux
Build & run unit testsBuild in WindowsBuild in Linux
Run samples (functional tests) in local modeSamples in WindowsSamples in Linux
Run examples in local modeExamples in WindowsExamples in Linux
Run Mobius app
Run Mobius ShellNot supported yet

Useful Links

Supported Spark Versions

Mobius is built and tested with Apache Spark 1.4.1, 1.5.2, 1.6.* and 2.0.

Releases

Mobius releases are available at https://github.com/Microsoft/Mobius/releases. References needed to build C# Spark driver applicaiton using Mobius are also available in NuGet

NuGet Badge

Refer to mobius-release-info.md for the details on versioning policy and the contents of the release.

License

License

Mobius is licensed under the MIT license. See LICENSE file for full license information.

Community

Issue StatsIssue StatsJoin the chat at https://gitter.im/Microsoft/MobiusTwitter

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.