Spark:0.9版本
spark-env.sh
export JAVA_HOME=
export SPARK_MASTER_IP=
export SPARK_WORKER_CORES=
export SPARK_WORKER_INSTANCES=
export SPARK_WORKER_MEMORY= // Q1:这里的memory与SPARK_MEM有什么区别呢: 这里是说一个worker node上可以用多少内存,下面那个是说启动的Application可以用多少内存
export SPARK_MASTER_PORT=
export SPARK_JAVA_OPTS="-verbose:gc -XX:-PrintGCDetails -XX:+PrintGCTimeStamps” //最后的参数在新版本中已经修正删除
slaves
xx.xx.xx.2
xx.xx.xx.3
xx.xx.xx.4
xx.xx.xx.5
集群启动
.../sbin/start-all.sh
如果使用HDFS的话需要启动DFS即可
/xx/hadoop-xx.yy/bin/start-dfs.sh
附上几条dfs的命令
hadoop fs -tail /xxx/xx
hadoop fs -ls /xxx/xxx
• MASTER=local[4] ADD_JARS=code.jar ./spark-shell //如果是集群运行最好Master的书写完整,如果是local运行,可以省略,则默认是本地一个线程执行。需要依赖的外部jar包,如果没有可以不写ADD_JARS
• MASTER=spark://host:port
• 指定executor内存:export SPARK_MEM=25g //这句话可以加在./spark-shell 这个文件中执行,就可以省略这一步了,根据源码显示,如果这里不指定的话,默认是512M
//控制台或者代码中指定,为第一优先级,其次是配置文件中的指定,最后就是默认的512M了
第一部分使用来自sogou lab的数据集 http://www.sogou.com/labs/dl/q.html
数据格式为
访问时间\t用户ID\t[查询词]\t该URL在返回结果中的排名\t用户点击的顺序号\t用户点击的URL
其中,用户ID是根据用户使用浏览器访问搜索引擎时的Cookie信息自动赋值,即同一次使用浏览器输入的不同查询对应同一个用户ID。
20111230000005 57375476989eea12893c0c3811607bcf 奇艺高清 1 1 http://www.qiyi.com/
20111230000005 66c5bb7774e31d0a22278249b26bc83a 凡人修仙传 3 1 http://www.booksky.org/BookDetail.aspx?BookID=1050804&Level=1
20111230000007 b97920521c78de70ac38e3713f524b50 本本联盟 1 1 http://www.bblianmeng.com/
20111230000008 6961d0c97fe93701fc9c0d861d096cd9 华南师范大学图书馆 1 1 http://lib.scnu.edu.cn/
20111230000008 f2f5a21c764aebde1e8afcc2871e086f 在线代理 2 1 http://proxyie.cn/
20111230000009 96994a0480e7e1edcaef67b20d8816b7 伟大导演 1 1 http://movie.douban.com/review/1128960/
val data = sc.textFile("hdfs://xxxxxxx")
data.cache //这句话要在下次action的时候才会执行
data.count //计算有多少行数据
data.map(_.split('\t')(0)).filter(_<'20111230000009').count // (0)是访问数组的语法
data.map(_.split('\t')(3)).filter(_.toInt == 1).count
data.map(_.split('\t')).filter(_(0)<'20111230000009').filter(_(4).toInt == 1).count // (0)是访问数组的语法
XML中主要配置spark core包的mvn 依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>chinahadoop</groupId> <artifactId>chinahadoop</artifactId> <version>1.0-SNAPSHOT</version> <repositories> <repository> <id>Akka repository</id> <url>http://repo.akka.io/releases</url> </repository> </repositories> <build> <sourceDirectory>src/main/scala/</sourceDirectory> <testSourceDirectory>src/test/scala/</testSourceDirectory> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <configuration> <scalaVersion>2.10.3</scalaVersion> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.2</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>reference.conf</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>cn.chinahadoop.???</Main-Class> </manifestEntries> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>0.9.0-incubating</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-streaming_2.10</artifactId> <version>0.9.0-incubating</version> </dependency> </dependencies> </project>
package cn.chinahadoop.spark import org.apache.spark.{SparkContext, SparkConf} import scala.collection.mutable.ListBuffer import org.apache.spark.SparkContext._ /** * Created by chenchao on 14-3-1. */ class Analysis { } object Analysis{ def main(args : Array[String]){ if(args.length != 3){ println("Usage : java -jar code.jar dependency_jars file_location save_location") System.exit(0) } val jars = ListBuffer[String]() args(0).split(',').map(jars += _) val conf = new SparkConf() conf.setMaster("spark://server1:8888") .setSparkHome("/data/software/spark-0.9.0-incubating-bin-hadoop1") .setAppName("analysis") .setJars(jars) .set("spark.executor.memory","25g") val sc = new SparkContext(conf) val data = sc.textFile(args(1)) data.cache println(data.count) data.filter(_.split(' ').length == 3).map(_.split(' ')(1)).map((_,1)).reduceByKey(_+_) .map(x => (x._2, x._1)).sortByKey(false).map( x => (x._2, x._1)).saveAsTextFile(args(2)) } }