1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| Sys.setenv(SPARK_HOME = "/usr/local/bigdata/spark")
library(sparklyr) library(dplyr)
library(pipeR) library(stringr)
spark_settings <- readLines(paste0(Sys.getenv("SPARK_HOME"), "/conf/spark-defaults.conf")) %>>% `[`(!str_detect(., "^#")) %>>% `[`(nchar(.) > 0) %>>% str_split("\\s")
file.copy(system.file(file.path("conf", "config-template.yml"), package = "sparklyr"), "config.yml", TRUE)
sink("config.yml", TRUE) sapply(spark_settings %>>% sapply(paste, collapse = ": "), function(x) cat(" ", x, "\n")) %>>% invisible sink()
spark_master <- "mesos://zk://192.168.0.121:2181,192.168.0.122:2181,192.168.0.123:2181/mesos" sc <- spark_connect(master = spark_master, config = spark_config("config.yml", FALSE))
sparkSession <- invoke_static(sc2, "org.apache.spark.sql.SparkSession", "builder") %>>% invoke("config", "spark.cassandra.connection.host", "192.168.0.121,192.168.0.122,192.168.0.123") %>>% invoke("getOrCreate")
cass_df <- invoke(sparkSession, "read") %>>% invoke("format", "org.apache.spark.sql.cassandra") %>>% invoke("option", "keyspace", "test") %>>% invoke("option", "table", "kv2") %>>% invoke("load") %>>% (~ print(cass_df))
cass_df_tbl <- sparklyr:::spark_partition_register_df(sc2, cass_df, "test_kv2", 0, TRUE) print(cass_df_tbl)
cass_df_tbl %>>% filter(var2 > 5)
cass_df_tbl %>>% filter(var2 > 5) %>>% as.data.frame
cass_df_tbl_agg <- cass_df_tbl %>>% mutate(var2_map = map(var1, var2)) %>>% group_by(var0) %>>% summarise(var2 = collect_list(var2_map)) %>>% as.data.frame
print(cass_df_tbl_agg)
str(cass_df_tbl_agg) 'data.frame': 2 obs. of 2 variables: $ var0: chr "T" "U" $ var2:List of 2 ..$ :List of 3 .. ..$ :List of 1 .. .. ..$ A: num 23.1 .. ..$ :List of 1 .. .. ..$ B: num 17.5 .. ..$ :List of 1 .. .. ..$ C: num 17 ..$ :List of 3 .. ..$ :List of 1 .. .. ..$ A: num 25.3 .. ..$ :List of 1 .. .. ..$ B: num 11.9 .. ..$ :List of 1 .. .. ..$ C: num 5
cass_df_2 <- invoke(sparkSession, "read") %>>% invoke("format", "org.apache.spark.sql.cassandra") %>>% invoke("option", "keyspace", "test") %>>% invoke("option", "table", "kv2_trans") %>>% invoke("load") %>>% (~print(.))
cass_df_tbl_2 <- sparklyr:::spark_partition_register_df(sc2, cass_df_2, "test_kv2_trans", 0, TRUE)
print(cass_df_tbl_2)
as.data.frame(cass_df_tbl_2) %>>% str
cass_df_tbl_2_r <- as.data.frame(cass_df_tbl_2) %>>% data.table %>>% `[`( , `:=`(var2_v = lapply(var2, unlist), var2_k = lapply(var2, names))) %>>% `[`( , .(var2_value = unlist(var2_v), var2_key = unlist(var2_k)), by = "var0") %>>% (~ print(.))
|