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
- spark:仿写案例--31
- spark:仿写案例--30
- spark:仿写案例--32
- spark:仿写案例--34
- spark:学习杂记+仿写案例--28
- spark:学习杂记+仿写案例--35
- spark:仿写sparkstreaming例子--15
- Spark SQL+Spark Streaming案例
- Spark典型案例
- Spark日志分析案例
- Spark基础案例
- spark使用案例
- Spark之SparkStreaming案例
- spark streaming案例整理
- spark整合kafka案例
- spark之实用案例
- Spark 之 经典案例
- Spark transformation算子案例
- Linux下socket编程与实例
- pycharm写python程序时"line 1"报错
- 08-外连接(左连接、右连接)、内连接、交叉连接
- 第一次配置windows服务器
- Junit的使用入门,判断控制台输出是否与预期相同
- spark:仿写案例--31
- 使用git上传代码到github<一>
- html5
- 解题报告 之 SOJ3353 Total Flow
- 软件测试-黑盒篇
- 主题模型TopicModel:LDA的缺陷和改进
- java程序连接数据库
- 史上最全设计模式导学目录(完整版)
- 虚拟机下安装linux