Livy 安装使用说明

1.  为什么要用Livy
  • Have long running SparkContexts that can be used for multiple Spark jobs, by multiple clients
  • Share cached RDDs or Dataframes across multiple jobs and clients
  • Multiple SparkContexts can be managed simultaneously, and they run on the cluster (YARN/Mesos) instead of the Livy Server for good fault tolerance and concurrency
  • Jobs can be submitted as precompiled jars, snippets of code, or via Java/Scala client API
  • Ensure security via secure authenticated communication
  • Apache License, 100% open source

2.Livy的运行模式(local和Yarn模式)
Then we upload the Spark example jar /usr/lib/spark/lib/spark-examples.jar on HDFS and point to it. If you are using Livy in local mode and not YARN mode, just keep the local path /usr/lib/spark/lib/spark-examples.jar.
如果是Cluster模式的话,Livy会读取HDFS上的文件此时应该把依赖jar上传到HDFS上)
It is strongly recommended to configure Spark to submit applications in YARN cluster mode. That makes sure that user sessions have their resources properly accounted for in the YARN cluster, and that the host running the Livy server doesn't become overloaded when multiple user sessions are running.(当有多个session的时候为了减少Livy server的压力,建议部署成yarn的模式)

3.restful 接口
1.提交一个sparkjob
curl -X POST --data '{"file": "/opt/jars/testLivy.jar", "className": "com.testLivy.TestLivyJob"}' -H "Content-Type: application/json" localhost:8998/batches

2.查看状态( 有not_started starting idle running busy shutting_down error dead success 等状态
localhost:8998/batches/3  结果:"id": 3,  "state": "dead"

4.livy 参数修改

(1) which can be changed with the livy.server.port config option  默认端口为8998,在livy.conf中可修改参数
(2) livy.yarn.jar : this config has been replaced by separate configs listing specific archives for different Livy features. Refer to the default  livy.conf  file shipped with Livy for instructions.

//默认使用hiveContext
livy.repl.enableHiveContext = true
//开启用户代理
livy.impersonation.enabled = true
//设置session空闲过期时间
livy.server.session.timeout = 1h

{"name":"test",
"args":["2016-10-10 22:00:00"],
"proxyUser":"shilong",
"className":"com.test.livyJob",
"file":"/opt/jars/etl-livy.jar",
"jars":["/opt/jars/jar/ficus_2.10-1.0.1.jar","/opt/jars/jar/mysql-connector-java-5.1.39.jar"],
//livy hdfs上面的的依赖jar 问题
"conf":{"driverMemory":"1g","driverCores":1,"executorCores":2,"executorMemory":"3g","numExecutors":2}
}
Livy 提供的关键字参数

(16 known properties: "executorCores", "className", "conf", "driverMemory", "name", "driverCores", "pyFiles", "archives", "queue", "executorMemory", "files", "jars", "proxyUser", "numExecutors", "file" [truncated]]




 
Apache Livy是一个开源项目,它提供了一种RESTful接口,用于在Apache Spark集群上运行交互式和批处理作业。这个接口使得在Java或其他编程语言中使用Spark变得更加简单和方便。 安装 1. 安装Java和Scala Livy需要Java 8或更高版本和Scala 2.11.x或2.12.x。您可以从官方网站下载Java和Scala,也可以使用包管理器进行安装。 2. 下载和编译Livy源代码 您可以从Apache Livy的官方网站https://livy.apache.org/download/下载最新版本的源代码。下载后,解压缩文件并使用以下命令编译: ``` $ mvn clean package -DskipTests ``` 3. 配置Livy 在编译完成后,将生成一个目录livy-0.7.1-incubating-bin。在该目录中,找到conf目录并编辑livy.conf文件。在该文件中,您需要配置以下参数: ``` livy.server.port = 8998 livy.file.local-dir-whitelist = /tmp livy.spark.master = spark://<spark-master>:7077 ``` 其中,livy.server.port是Livy服务器的端口号,livy.file.local-dir-whitelist是本地目录的白名单,livy.spark.master是Spark master的URL。 4. 启动Livy 在配置完成后,使用以下命令启动Livy: ``` $ bin/livy-server ``` 在启动完成后,您可以使用http://localhost:8998/访问Livy的RESTful API。 使用 在Java中使用Livy,您需要使用Livy的Java客户端库。您可以在Maven中添加以下依赖项: ```xml <dependency> <groupId>org.apache.livy</groupId> <artifactId>livy-client-common</artifactId> <version>0.7.1-incubating</version> </dependency> <dependency> <groupId>org.apache.livy</groupId> <artifactId>livy-client-spark_2.11</artifactId> <version>0.7.1-incubating</version> </dependency> ``` 1. 创建LivyClient对象 使用以下代码创建一个LivyClient对象: ```java LivyClient client = new LivyClientBuilder() .setURI(new URI("http://localhost:8998")) .build(); ``` 其中,URI参数是Livy服务器的URL。 2. 提交Spark作业 使用以下代码提交一个Spark作业: ```java JavaSparkContext spark = new JavaSparkContext("spark://<spark-master>:7077", "MyApp"); List<Integer> data = Arrays.asList(1, 2, 3, 4, 5); JavaRDD<Integer> distData = spark.parallelize(data); int result = distData.reduce((a, b) -> a + b); ``` 其中,spark参数是Spark master的URL,data是一个包含整数的列表,distData是一个JavaRDD对象,result是reduce操作的结果。 使用以下代码将作业提交到Livy服务器: ```java JobHandle<Integer> handle = client.submit(new JavaSparkJob<>(MyApp.class.getName(), result)); ``` 其中,JavaSparkJob是一个用于包装Spark作业的类,MyApp是您的Spark作业的类名,result是作业的结果。 3. 获取作业结果 使用以下代码获取作业的结果: ```java int result = handle.get(); ``` 其中,handle是提交作业后返回的JobHandle对象,result是作业的结果。 4. 关闭LivyClient对象 使用以下代码关闭LivyClient对象: ```java client.stop(true); ``` 其中,true参数表示在关闭之前等待所有作业完成。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值