如何使用Cassandra数据库(cql)和node.js的简单示例(Simple example of how to use the Cassandra database (cql) and node.js)
我试图查询并插入cassandra DB中的东西。 我知道如何在cql(cqlsh)的命令行中执行该操作,但不确定如何通过node.js与cassandra数据库进行基本交互。
如果有人可以给我非常简单的代码示例如何做到这一点,这将是非常有用的!
没有节俭,只允许node.js。
提前致谢。
I was trying to query and insert things into a cassandra DB. I know how to do that in the command line for cql (cqlsh), but was unsure on how to basically interact with the cassandra database through node.js.
If someone could give me very simple code examples of how to do this, it would be very useful!
Without thrift, only node.js is allowed.
Thanks in advance.
原文:https://stackoverflow.com/questions/22206917
最满意答案
我想,你想要
reorder()
。 通常,reorder(x,y,FUN)
根据将函数FUN
应用于第二变量y
的结果来改变因子x
的级别的顺序。 在这种情况下,您可以使用FUN=length
,并且它对y
使用什么并不重要。设置数据(我选择修改概率以使结果更清晰):
set.seed(101) df <- data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), prob=c(0.1,0.5,0.2,0.05,0.15), 1000, replace=TRUE)))
基本情节(错误的顺序):
library(ggplot2) print(g1 <- ggplot(df, aes(x=factor(1), fill=Var1))+ geom_bar(width=1, colour="black")+ coord_polar(theta="y")+ theme_void())
现在重新排序:
df$Var1 <- reorder(df$Var1,df$Var1,FUN=length) levels(df$Var1) ## [1] "DE" "AB" "EF" "CD" "BC"
检查订单是否正确:
sort(table(df$Var1)) ## DE AB EF CD BC ## 46 105 163 189 497
打印新图(使用
%+%
粘贴新数据并使用guide_legend()
翻转图例的顺序:也可以使用function(x) -length(x)
作为FUN
来更改guide_legend()
的顺序第一名)。print(g1 %+% df + scale_fill_discrete(guide=guide_legend(reverse=TRUE)))
You want
reorder()
, I think. In generalreorder(x,y,FUN)
changes the order of levels of a factorx
according to the results of applying functionFUN
to a second variabley
. In this case, you can useFUN=length
, and it doesn't really matter what you use fory
.Set up data (I chose to modify the probabilities to make the result clearer):
set.seed(101) df <- data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), prob=c(0.1,0.5,0.2,0.05,0.15), 1000, replace=TRUE)))
Basic plot (wrong order):
library(ggplot2) print(g1 <- ggplot(df, aes(x=factor(1), fill=Var1))+ geom_bar(width=1, colour="black")+ coord_polar(theta="y")+ theme_void())
Now reorder:
df$Var1 <- reorder(df$Var1,df$Var1,FUN=length) levels(df$Var1) ## [1] "DE" "AB" "EF" "CD" "BC"
Check that the order is right:
sort(table(df$Var1)) ## DE AB EF CD BC ## 46 105 163 189 497
Print the new plot (stick in the new data with
%+%
and flip the order of the legend withguide_legend()
: you could also usefunction(x) -length(x)
asFUN
to change the order of the levels in the first place).print(g1 %+% df + scale_fill_discrete(guide=guide_legend(reverse=TRUE)))
相关问答
更多-
对R中的绘图进行排序(Sorting values in for plotting in R)[2022-10-29]
啊,我明白了。 这是因为lines按照给定的顺序在点之间绘制线条。 有几种方法可以解决这个问题: 在plot命令中type='l'然后with(dat,lines(...))是不必要的: # can also do the col='black',lty=1 in here. plot(tp ~ fp, data = dat, xlim = c(0,fp_max),ylim = c(0,tp_max), type = "l") 请注意, 根据 fp_max和tp_max 的定义 ,您将包含点(fp_max ... -
使用reshape包中的dcast函数(或者具有dcast函数增强实现的dcast ): library(reshape2) dcast(DF, ID ~ paste(Var_1,Var_2,sep="."), fun.aggregate = length) 这使: ID A.C A.D B.D B.E 1 first 2 0 0 0 2 fourth 0 1 0 2 3 second 1 1 2 0 4 third 1 0 0 ...
-
我们可以在plot使用xaxt = "n" ,然后使用axis更改xaxis刻度标签 plot(Data ~ Months, transform(df, Months = match(row.names(df), month.abb)), las = 2, xaxt = "n", type = "b", col = "blue") axis(1, at = seq_len(nrow(df)), row.names(df)) We can use xaxt = ...
-
我想,你想要reorder() 。 通常, reorder(x,y,FUN)根据将函数FUN应用于第二变量y的结果来改变因子x的级别的顺序。 在这种情况下,您可以使用FUN=length ,并且它对y使用什么并不重要。 设置数据(我选择修改概率以使结果更清晰): set.seed(101) df <- data.frame(Var1=as.factor(sample(c("AB", "BC", "CD", "DE", "EF"), prob=c(0.1 ...
-
使用factor : # oldlocale <- Sys.getlocale("LC_TIME") # Sys.setlocale("LC_TIME", "english") table(factor( weekdays(Sys.Date()+0:7), levels=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") )) # Monday Tuesday Wednesday ...
-
这是另一种方法,使用ggplot的魔力。 因为ggplot会为你计算摘要,我怀疑这意味着你可以跳过做aov的整个步骤。 关键是你的数据应该在单个data.frame ,你可以传递给ggplot 。 请注意,我已创建了新的示例数据以进行演示。 library(ggplot2) df <- data.frame( value = runif(300), yr = rep(1:10, each=3), trt = rep(LETTERS[1:4], each=75), third = rep(c ...
-
当用R中的`as.factor`绘图时,为什么`ann = F`不起作用?(Why does `ann = F` not work when plotting with `as.factor` in R?)[2023-12-10]
它似乎是由plot.formula引起的,因为它可以分别指定x和y : plot(as.factor(y), x, ann=FALSE) 更新: 确认它在graphics:::plot.formula 。 调用plot的行显式设置了ylab和xlab ( funname是"plot"和dots = list(ann=FALSE) ): do.call(funname, c(list(mf[[i]], y, ylab = yl, xlab = xl), dots)) It seems to be caus ... -
python中的频率绘图(Frequency plotting in python)[2023-05-23]
使用matplotlib的栏 import matplotlib.pyplot as plt x = [1,2,3,4,5] y = [10,40,30,10,3] plt.bar(x,y) plt.show() Use matplotlib's bar import matplotlib.pyplot as plt x = [1,2,3,4,5] y = [10,40,30,10,3] plt.bar(x,y) plt.show() -
R中的混合排序(Mixed sorting in R)[2022-01-11]
以下是使用base r的两种方法 dat <- structure(list(Company = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L), .Label = c("ABC Inc", ... -
R绘制频率分布(R plotting frequency distribution)[2023-05-24]
像这样的东西? x <- c(rep(.3,100), rep(.5, 700)) y <- c(seq(0,1, length.out=100), seq(0,1,length.out=700)) plot(x,y) 编辑:(根据OP的评论) 在这种情况下,这样的事情应该有效。 x <- rep(seq(1, 10)/10, seq(100, 1000, by=100)) x.t <- as.matrix(table(x)) y <- unlist(apply(x.t, 1, function(x) s ...