首页 文章 精选 留言 我的

精选列表

搜索[Go语言],共10000篇文章
优秀的个人博客,低调大师

sparklyr-R语言访问Spark的另外一种方法

Connect toSparkfrom R. The sparklyr package provides a completedplyrbackend. Filter and aggregate Spark datasets then bring them into R for analysis and visualization. Use Spark<u+2019>s distributedmachine learninglibrary from R. Createextensionsthat call the full Spark API and provide interfaces to Spark packages. Installation You can install thesparklyrpackage from CRAN as follows: install.packages("sparklyr") You should also install a local version of Spark for development purposes: library(sparklyr) spark_install(version = "1.6.2") To upgrade to the latest version of sparklyr, run the following command and restart your r session: devtools::install_github("rstudio/sparklyr") If you use the RStudio IDE, you should also download the latestpreview releaseof the IDE which includes several enhancements for interacting with Spark (see theRStudio IDEsection below for more details). Connecting to Spark You can connect to both local instances of Spark as well as remote Spark clusters. Here we<u+2019>ll connect to a local instance of Spark via thespark_connectfunction: library(sparklyr) sc <- spark_connect(master = "local") The returned Spark connection (sc) provides a remote dplyr data source to the Spark cluster. For more information on connecting to remote Spark clusters see theDeploymentsection of the sparklyr website. Using dplyr We can new use all of the available dplyr verbs against the tables within the cluster. We<u+2019>ll start by copying some datasets from R into the Spark cluster (note that you may need to install the nycflights13 and Lahman packages in order to execute this code): install.packages(c("nycflights13", "Lahman")) library(dplyr) iris_tbl <- copy_to(sc, iris) flights_tbl <- copy_to(sc, nycflights13::flights, "flights") batting_tbl <- copy_to(sc, Lahman::Batting, "batting") src_tbls(sc) ## [1] "batting" "flights" "iris" To start with here<u+2019>s a simple filtering example: ) ## Source: query [6,233 x 19] ## Database: spark connection master=local[8] app=sparklyr local=TRUE ## ## year month day dep_time sched_dep_time dep_delay arr_time ## <int> <int> <int> <int> <int> <dbl> <int> ## 1 2013 1 1 517 515 2 830 ## 2 2013 1 1 542 540 2 923 ## 3 2013 1 1 702 700 2 1058 ## 4 2013 1 1 715 713 2 911 ## 5 2013 1 1 752 750 2 1025 ## 6 2013 1 1 917 915 2 1206 ## 7 2013 1 1 932 930 2 1219 ## 8 2013 1 1 1028 1026 2 1350 ## 9 2013 1 1 1042 1040 2 1325 ## 10 2013 1 1 1231 1229 2 1523 ## # ... with 6,223 more rows, and 12 more variables: sched_arr_time <int>, ## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>, ## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, ## # minute <dbl>, time_hour <dbl> Introduction to dplyrprovides additional dplyr examples you can try. For example, consider the last example from the tutorial which plots data on flight delays: delay <- flights_tbl %>% group_by(tailnum) %>% summarise(count = n(), dist = mean(distance), delay = mean(arr_delay)) %>% filter(count > 20, dist < 2000, !is.na(delay)) %>% collect # plot delays library(ggplot2) ggplot(delay, aes(dist, delay)) + geom_point(aes(size = count), alpha = 1/2) + geom_smooth() + scale_size_area(max_size = 2) ## `geom_smooth()` using method = 'gam' Window Functions dplyrwindow functionsare also supported, for example: batting_tbl %>% select(playerID, yearID, teamID, G, AB:H) %>% arrange(playerID, yearID, teamID) %>% group_by(playerID) %>% filter(min_rank(desc(H)) <= 2 & H > 0) ## Source: query [2.562e+04 x 7] ## Database: spark connection master=local[8] app=sparklyr local=TRUE ## Groups: playerID ## ## playerID yearID teamID G AB R H ## <chr> <int> <chr> <int> <int> <int> <int> ## 1 abbotpa01 2000 SEA 35 5 1 2 ## 2 abbotpa01 2004 PHI 10 11 1 2 ## 3 abnersh01 1992 CHA 97 208 21 58 ## 4 abnersh01 1990 SDN 91 184 17 45 ## 5 abreujo02 2015 CHA 154 613 88 178 ## 6 abreujo02 2014 CHA 145 556 80 176 ## 7 acevejo01 2001 CIN 18 34 1 4 ## 8 acevejo01 2004 CIN 39 43 0 2 ## 9 adamsbe01 1919 PHI 78 232 14 54 ## 10 adamsbe01 1918 PHI 84 227 10 40 ## # ... with 2.561e+04 more rows For additional documentation on using dplyr with Spark see thedplyrsection of the sparklyr website. Using SQL It<u+2019>s also possible to execute SQL queries directly against tables within a Spark cluster. Thespark_connectionobject implements aDBIinterface for Spark, so you can usedbGetQueryto execute SQL and return the result as an R data frame: library(DBI) iris_preview <- dbGetQuery(sc, "SELECT * FROM iris LIMIT 10") iris_preview ## Sepal_Length Sepal_Width Petal_Length Petal_Width Species ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ## 7 4.6 3.4 1.4 0.3 setosa ## 8 5.0 3.4 1.5 0.2 setosa ## 9 4.4 2.9 1.4 0.2 setosa ## 10 4.9 3.1 1.5 0.1 setosa Machine Learning You can orchestrate machine learning algorithms in a Spark cluster via themachine learningfunctions withinsparklyr. These functions connect to a set of high-level APIs built on top of DataFrames that help you create and tune machine learning workflows. Here<u+2019>s an example where we useml_linear_regressionto fit a linear regression model. We<u+2019>ll use the built-inmtcarsdataset, and see if we can predict a car<u+2019>s fuel consumption (mpg) based on its weight (wt), and the number of cylinders the engine contains (cyl). We<u+2019>ll assume in each case that the relationship betweenmpgand each of our features is linear. ) ## * No rows dropped by 'na.omit' call fit ## Call: ml_linear_regression(., response = "mpg", features = c("wt", "cyl")) ## ## Coefficients: ## (Intercept) wt cyl ## 37.066699 -2.309504 -1.639546 For linear regression models produced by Spark, we can usesummary()to learn a bit more about the quality of our fit, and the statistical significance of each of our predictors. summary(fit) ## Call: ml_linear_regression(., response = "mpg", features = c("wt", "cyl")) ## ## Deviance Residuals:: ## Min 1Q Median 3Q Max ## -2.6881 -1.0507 -0.4420 0.4757 3.3858 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 37.06670 2.76494 13.4059 2.981e-07 *** ## wt -2.30950 0.84748 -2.7252 0.02341 * ## cyl -1.63955 0.58635 -2.7962 0.02084 * ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## R-Squared: 0.8665 ## Root Mean Squared Error: 1.799 Spark machine learning supports a wide array of algorithms and feature transformations and as illustrated above it<u+2019>s easy to chain these functions together with dplyr pipelines. To learn more see themachine learningsection. Reading and Writing Data You can read and write data in CSV, JSON, and Parquet formats. Data can be stored in HDFS, S3, or on the local filesystem of cluster nodes. temp_csv <- tempfile(fileext = ".csv") temp_parquet <- tempfile(fileext = ".parquet") temp_json <- tempfile(fileext = ".json") spark_write_csv(iris_tbl, temp_csv) iris_csv_tbl <- spark_read_csv(sc, "iris_csv", temp_csv) spark_write_parquet(iris_tbl, temp_parquet) iris_parquet_tbl <- spark_read_parquet(sc, "iris_parquet", temp_parquet) spark_write_json(iris_tbl, temp_json) iris_json_tbl <- spark_read_json(sc, "iris_json", temp_json) src_tbls(sc) ## [1] "batting" "flights" "iris" "iris_csv" ## [5] "iris_json" "iris_parquet" "mtcars" Extensions The facilities used internally by sparklyr for its dplyr and machine learning interfaces are available to extension packages. Since Spark is a general purpose cluster computing system there are many potential applications for extensions (e.g.<u+00a0>interfaces to custom machine learning pipelines, interfaces to 3rd party Spark packages, etc.). Here<u+2019>s a simple example that wraps a Spark text file line counting function with an R function: ) ## [1] 336777 To learn more about creating extensions see theExtensionssection of the sparklyr website. Table Utilities You can cache a table into memory with: tbl_cache(sc, "batting") and unload from memory using: tbl_uncache(sc, "batting") Connection Utilities You can view the Spark web console using thespark_webfunction: spark_web(sc) You can show the log using thespark_logfunction: spark_log(sc, n = 10) ## 17/02/03 15:34:17 INFO DAGScheduler: Submitting 1 missing tasks from ResultStage 91 (/var/folders/fz/v6wfsg2x1fb1rw4f6r0x4jwm0000gn/T//RtmpZqMbDE/file8f2b280ac4e5.csv MapPartitionsRDD[363] at textFile at NativeMethodAccessorImpl.java:-2) ## 17/02/03 15:34:17 INFO TaskSchedulerImpl: Adding task set 91.0 with 1 tasks ## 17/02/03 15:34:17 INFO TaskSetManager: Starting task 0.0 in stage 91.0 (TID 177, localhost, partition 0,PROCESS_LOCAL, 2430 bytes) ## 17/02/03 15:34:17 INFO Executor: Running task 0.0 in stage 91.0 (TID 177) ## 17/02/03 15:34:17 INFO HadoopRDD: Input split: file:/var/folders/fz/v6wfsg2x1fb1rw4f6r0x4jwm0000gn/T/RtmpZqMbDE/file8f2b280ac4e5.csv:0+33313106 ## 17/02/03 15:34:17 INFO Executor: Finished task 0.0 in stage 91.0 (TID 177). 2082 bytes result sent to driver ## 17/02/03 15:34:17 INFO TaskSetManager: Finished task 0.0 in stage 91.0 (TID 177) in 116 ms on localhost (1/1) ## 17/02/03 15:34:17 INFO DAGScheduler: ResultStage 91 (count at NativeMethodAccessorImpl.java:-2) finished in 0.117 s ## 17/02/03 15:34:17 INFO TaskSchedulerImpl: Removed TaskSet 91.0, whose tasks have all completed, from pool ## 17/02/03 15:34:17 INFO DAGScheduler: Job 61 finished: count at NativeMethodAccessorImpl.java:-2, took 0.119612 s Finally, we disconnect from Spark: spark_disconnect(sc) RStudio IDE The latest RStudioPreview Releaseof the RStudio IDE includes integrated support for Spark and the sparklyr package, including tools for: Creating and managing Spark connections Browsing the tables and columns of Spark DataFrames Previewing the first 1,000 rows of Spark DataFrames Once you<u+2019>ve installed the sparklyr package, you should find a newSparkpane within the IDE. This pane includes aNew Connectiondialog which can be used to make connections to local or remote Spark instances: Once you<u+2019>ve connected to Spark you<u+2019>ll be able to browse the tables contained within the Spark cluster: The Spark DataFrame preview uses the standard RStudio data viewer: The RStudio IDE features for sparklyr are available now as part of theRStudio Preview Release. Using H2O rsparklingis a CRAN package fromH2Othat extendssparklyrto provide an interface intoSparkling Water. For instance, the following example installs, configures and runsh2o.glm: options(rsparkling.sparklingwater.version = "1.6.8") library(rsparkling) library(sparklyr) library(dplyr) library(h2o) sc <- spark_connect(master = "local", version = "1.6.2") mtcars_tbl <- copy_to(sc, mtcars, "mtcars") mtcars_h2o <- as_h2o_frame(sc, mtcars_tbl, strict_version_check = FALSE) mtcars_glm <- h2o.glm(x = c("wt", "cyl"), y = "mpg", training_frame = mtcars_h2o, lambda_search = TRUE) mtcars_glm ## Model Details: ## ============== ## ## H2ORegressionModel: glm ## Model ID: GLM_model_R_1486164877174_1 ## GLM Model: summary ## family link regularization ## 1 gaussian identity Elastic Net (alpha = 0.5, lambda = 0.1013 ) ## lambda_search ## 1 nlambda = 100, lambda.max = 10.132, lambda.min = 0.1013, lambda.1se = -1.0 ## number_of_predictors_total number_of_active_predictors ## 1 2 2 ## number_of_iterations training_frame ## 1 0 frame_rdd_33 ## ## Coefficients: glm coefficients ## names coefficients standardized_coefficients ## 1 Intercept 38.941654 20.090625 ## 2 cyl -1.468783 -2.623132 ## 3 wt -3.034558 -2.969186 ## ## H2ORegressionMetrics: glm ## ** Reported on training data. ** ## ## MSE: 6.017684 ## RMSE: 2.453097 ## MAE: 1.940985 ## RMSLE: 0.1114801 ## Mean Residual Deviance : 6.017684 ## R^2 : 0.8289895 ## Null Deviance :1126.047 ## Null D.o.F. :31 ## Residual Deviance :192.5659 ## Residual D.o.F. :29 ## AIC :156.2425 spark_disconnect(sc) Connecting through Livy Livyenables remote connections to Apache Spark clusters. Connecting to Spark clusters through Livy isunder experimental developmentinsparklyr. Please post any feedback or questions as a GitHub issue as needed. Before connecting to Livy, you will need the connection information to an existing service running Livy. Otherwise, to testlivyin your local environment, you can install it and run it locally as follows: livy_install() livy_service_start() To connect, use the Livy service address asmasterandmethod = "livy"inspark_connect. Once connection completes, usesparklyras usual, for instance: sc <- spark_connect(master = "http://localhost:8998", method = "livy") copy_to(sc, iris) ## Source: query [150 x 5] ## Database: spark connection master=http://localhost:8998 app= local=FALSE ## ## Sepal_Length Sepal_Width Petal_Length Petal_Width Species ## <dbl> <dbl> <dbl> <dbl> <chr> ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3.0 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5.0 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ## 7 4.6 3.4 1.4 0.3 setosa ## 8 5.0 3.4 1.5 0.2 setosa ## 9 4.4 2.9 1.4 0.2 setosa ## 10 4.9 3.1 1.5 0.1 setosa ## # ... with 140 more rows spark_disconnect(sc) Once you are done usinglivylocally, you should stop this service with: livy_service_stop() To connect to remotelivyclusters that support basic authentication connect as: config <- livy_config_auth("<username>", "<password">) sc <- spark_connect(master = "<address>", method = "livy", config = config) spark_disconnect(sc) Links Download from CRAN athttps://<u+200b>cran.r-project.org/<u+200b>package=sparklyr Report a bug athttps://<u+200b>github.com/<u+200b>rstudio/<u+200b>sparklyr/<u+200b>issues License Apache License 2.0 | fileLICENSE Developers Javier LuraschiAuthor, maintainer Kevin UsheyAuthor JJ AllaireAuthor The Apache Software FoundationAuthor, copyright<u+00a0>holder All authors... Dev status Developed by Javier Luraschi, Kevin Ushey, JJ Allaire, The Apache Software Foundation. Site built withpkgdown. 参考http://spark.rstudio.com/ http://alitrack.com/2016/11/01/sparklyr-r%E8%AF%AD%E8%A8%80%E8%AE%BF%E9%97%AEspark%E7%9A%84%E5%8F%A6%E5%A4%96%E4%B8%80%E7%A7%8D%E6%96%B9%E6%B3%95/ 本文转自茄子_2008博客园博客,原文链接:http://www.cnblogs.com/xd502djj/p/7116076.html,如需转载请自行联系原作者。

优秀的个人博客,低调大师

K&R《C语言程序设计》代码慢录一

昨天,在一品漫城打第三届4VS4室内篮球赛。 我带的队,打三场,输三场,成功淘汰出局~~~:) 哎,到最后,好像打球氛围不太好,我们老了,只为练练身体,友谊第一。 但,好像有些队,只认输赢一样, 一个哥们居然去威胁裁判。。。 晚上回来吃得少,半夜饿啦,小孩子又跟我们睡觉, 我就起来找食吃,顺便看会K&R的书,尽管在DOUBAN上我给了它差评, 但一路PYTHON看下来,我觉得和这书对照,应该理解得更深入些。 今早突发想学布置JAVA和HADOOP的事。到时再写BLOG。 先上刚才写的书上的样例代码和运行效果吧。 代码: 1 #include <stdio.h> 2 main() 3 { 4 /* list transfer program. 5 print list*/ 6 float fahr, celsius; 7 int lower, upper, step; 8 9 lower = 0; 10 upper = 300; 11 step = 20; 12 13 fahr = lower; 14 while (fahr <= upper) { 15 celsius = (5.0/9.0) * (fahr-32); 16 printf("%3.0f %6.1f\n", fahr, celsius); 17 fahr = fahr + step; 18 } 19 } 运行截图:

优秀的个人博客,低调大师

风控开发指南:Go集成家政风险报告实现精准合规审查

每年春节前的“大扫除”与年前搬家高峰期,城市即时家政平台的日均运力缺口往往达到平日的 4 至 6 倍。大量灵活就业人员通过线上招募通道临时注册成为保洁员、收纳师或搬家师傅,平台需要在极短时间内完成数万名新增零工的入户安全核查。由于家政服务涉及直接进入雇主家庭私人空间,对服务人员的真实身份、治安背景及司法履约状况进行严格合规审查是平台不可逾越的底线。传统模式下,运营团队依赖人工核对身份证照片、线下收取无犯罪记录纸质证明,单人审核耗时动辄数小时,不仅在春节峰值期间造成大量运力积压,也难以实时察觉身份信息不匹配资质或近期新增的司法执行与失信记录。

优秀的个人博客,低调大师

风控开发指南:Go集成企业税收违法核查实现精准合规审查

在大宗商品(有色金属、黑色建材、能源化工、农产品)B2B 现货贸易与电子商业承兑汇票(ECDS 商票)质押贴现流转业务中,动辄数千万乃至数亿元的单笔合同金额与微薄的行业利润率,对交易平台的实时履约风控提出了极其严苛的要求。在供应链金融与现货交割流程中,核心交易主体或票据承兑企业的税务合规状态,直接决定了货款交割安全性与商业承兑汇票的最终兑付能力。依据《中华人民共和国税收征收管理法》及最高人民法院关于审理票据纠纷案件的相关司法解释,企业一旦触发重大税收违法失信案件(如虚开增值税专用发票、偷税漏税、骗取出口退税或逃避追缴欠税),将立即面临税务机关行政处罚、纳税信用等级降级、发票申领受限以及银行基本结算账户被司法机关冻结等致命风险,进而迅速演变为大宗现货无法交割、商业承兑汇票到期拒付、资金链断裂等严重履约风险。

优秀的个人博客,低调大师

风控开发指南:Go集成手机消费区间验证实现精准合规审查

在消费电子年度旗舰新品首发、平台电商“双十一”及“618”等超级大促活动中,各大品牌方与电商平台通常会联合金融科技机构、数码分期租赁服务商,向预约申请用户定向投放诸如“0元享新机信用租赁”、“万元消费额度免息立减”、“高端数码硬件私享延保礼包”等高价值专属权益。依据《中华人民共和国民法典》《互联网用户账号信息管理规定》以及电商平台合规风控合规指引,平台必须在权益核发与额度授予环节,对申领用户的资信履约能力、日常消费活跃度以及账号真实性进行审慎评估。在峰值秒杀和准入审批场景下,网关每秒面临数万级瞬时突发并发进件,风控中台必须在 30 毫秒以内的极窄时间窗口内,快速甄别出具备稳定日常消费能力的真实优质用户,既要保障高潜高价值客户享受丝滑无感的秒级核准与自动发券,又要稳妥识别使用低频非活跃账号或非真实日常账号批量套取营销补贴等履约隐患,筑牢营销预算与资产安全的合规防线。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码