spark:仿写案例--31

来源:互联网 发布:怎样注册两个淘宝账号 编辑:程序博客网 时间:2024/04/29 03:05

1.HBaseTest:分布式开源数据库,测试HBase

package llfimport org.apache.spark._import org.apache.spark.{SparkContext, SparkConf}import org.apache.hadoop.hbase.client.HBaseAdminimport org.apache.hadoop.hbase.{HBaseConfiguration, HTableDescriptor, TableName}import org.apache.hadoop.hbase.mapreduce.TableInputFormat/** * Created by sendoh on 2015/4/23. */object HBaseTest {  def main(args: Array[String]): Unit ={    val conf = new SparkConf().setAppName("HBaseTest").setMaster("local[2]")    val sc = new SparkContext(conf)    val Sconf = HBaseConfiguration.create() //HBaseConfiguration是每一个hbase client都会使用到的对象,它代表的是HBase配置信息。它有两种构造方式:                                                //public HBaseConfiguration()                                                //public HBaseConfiguration(final Configuration c)    Sconf.set(TableInputFormat.INPUT_TABLE, args(0)) //TableInputFormat这是专门处理基于HBase的MapReduce的输入数据的格式类。                                                 //继承结构:(1)public class TableInputFormat extends TableInputFormatBase implements Configurable;                                                 //(2)public abstract class TableInputFormatBase extends InputFormat<ImmutableBytesWritable, Result>。                                                 //其中InputFormat是输入格式的基类。  TableInputFormat类主要是构造HTable对象和Scan对象,                                                 //主要在方法setConf(Configuration configuration)构造 // Initialize hBase table if necessary    val admin = new HBaseAdmin(Sconf)            //HBaseAdmin来创建表。HBaseAdmin负责表的META信息处理。                                                 //HBaseAdmin提供了createTable这个方法:public void createTable(HTableDescriptor desc)    if (!admin.isTableAvailable(args(0))) {      val tableDesc = new HTableDescriptor(TableName.valueOf(args(0)))      admin.createTable(tableDesc)    }    val hBaseRDD = sc.newAPIHadoopRDD(Sconf, classOf[TableInputFormat], //新建一个RDD      classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],      classOf[org.apache.hadoop.hbase.client.Result])    hBaseRDD.count()    sc.stop()  }}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2.HdfsTest:测试Hdfs文件系统

package llfimport org.apache.spark.{SparkContext, SparkConf}import org.apache.spark._/** * Created by sendoh on 2015/4/23. */object HdfsTest {  def main(args: Array[String]): Unit ={   if (args.length < 1){     System.err.println("Usage: HdfsTest <file>")     System.exit(1)   }    val conf = new SparkConf().setAppName("HdfsTest")    val sc = new SparkContext(conf)    val file = sc.textFile(args(0))//取文件,文件路径args(0)    for (iter <- 1 to 10){      val start = System.currentTimeMillis()//开始时间      for (x <- mapped) { x + 2 }   //x每次加2      val end = System.currentTimeMillis()//结束时间      println("Iteration " + iter + " took " + (end-start) + " ms")//打印    }    sc.stop()  }}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3.LocalPi:本地计算Pi值

package llfimport scala.math.random/** * Created by sendoh on 2015/4/23. */object LocalPi {  def main(args: Array[String]): Unit ={    var count = 0    for (i <- 1 to 100000) {      val x = random * 2 - 1      val y = random * 2 - 1      if (x*x + y*y < 1) count += 1    }    println("Pi is roughly " + 4 * count / 100000.0)  }}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

初学spark,欢迎共同讨论相关问题,QQ:1217111493

0 0
原创粉丝点击