我设法在AmazonRedshift中用Python编写一些标量函数,即将一列或几列作为输入并根据某些逻辑或转换返回单个值.
但是有没有办法在UDF中传递数字列(即列表)的所有值并计算这些值的统计数据,例如平均值或标准差?
我有一个宏通过列,并从范围内的所有单元格中删除数字.我想添加一个可选参数,所以我可以调用sub,同时告诉它要运行哪些列.这就是我所拥有的:
Sub GEN_USE_Remove_Numbers_from_Columns(Optional myColumns as String)
这个想法是我可以从另一个sub中调用它,就像这样 GEN_USE_...Columns("A B C")
但是,我无法从VB编辑器中运行它,也无法在宏窗口中看到该宏(单击视图 - >宏时).为什么不?为什么我必须用参数调用它(偶数GEN_USE_...Columns("")
)我不能再调用它GEN_USE_...Columns()
了.
我已经看到你可以添加= Nothing
到最后,如果没有给出一个默认值.我已经尝试了()但它没有做任何事情.
我想我的问题是A)为什么我在宏窗口中看不到具有可选参数的宏?和B)为什么我不能直接从VB编辑器调用带参数的宏?我必须实际创建一个sub,然后我可以调用该sub中的宏.不再只是突出显示一些文字并点击"播放".
我知道这两个问题可能是相关的,所以任何见解都会受到赞赏!
(PS:我知道我们应该发布代码,但我不认为这是非常相关的.当然,如果你想看到它,请告诉我,我会更新).
我正在尝试注册一个简单的 UDF,用于使用 Scala Luna Eclipse IDE 在 spark 中提取日期功能。这是我的代码:
sqlContext.udf.register("extract", (dateUnit: String, date : String) => udf.extract(dateUnit,date ) )
def extract(dateUnit : String, date: String) : String = {
val splitArray : Array[String] = date.split("-")
val result = dateUnit.toUpperCase() match {
case "YEAR" => splitArray(0)
case "MONTH" => splitArray(1)
case "DAY" => splitArray(2)
case whoa => "Unexpected case :" + whoa.toString()
}
return result ;
}
Run Code Online (Sandbox Code Playgroud)
当我通过 Eclipse 控制台执行此功能时 Select * from date_dim WHERE d_dom < extract('YEAR', '2015-05-01') limit 10" …
我试图在我的Mac上安装以下软件包,以便在我的本地环境中测试我的API,但到目前为止我还没有成功.
https://github.com/spachev/mysql_udf_bundle
我尝试了各种各样的东西,比如:
brew install libmysqlclient-dev
Run Code Online (Sandbox Code Playgroud)
这产生了以下错误:
Error: No available formula with the name "libmysqlclient-dev"
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
==> Searching taps...
Error: No formulae found in taps.
Run Code Online (Sandbox Code Playgroud)
我习惯于在CentOS上工作,所以我并不是特别熟悉apt
和brew
... ...有谁能告诉我如何最好地在我的Mac上安装它?
不确定它是否有任何相关性,但我正在运行Mac OSX 10.11.4(El Capitan).
我当时不使用安装MySQL brew install mysql
,而不是,我也跟着指示操作:http://jason.pureconcepts.net/2015/10/install-apache-php-mysql-mac-os-x-el-capitan/
在项目中间我在我的spark sql查询中调用一个函数后出现了波纹管错误
我已经写了一个用户定义函数,它将取两个字符串并连接它们连接后它将占用大多数子串长度为5取决于总字符串长度(sql server的右(字符串,整数)的替代方法)
from pyspark.sql.types import*
def concatstring(xstring, ystring):
newvalstring = xstring+ystring
print newvalstring
if(len(newvalstring)==6):
stringvalue=newvalstring[1:6]
return stringvalue
if(len(newvalstring)==7):
stringvalue1=newvalstring[2:7]
return stringvalue1
else:
return '99999'
spark.udf.register ('rightconcat', lambda x,y:concatstring(x,y), StringType())
Run Code Online (Sandbox Code Playgroud)
它单独工作.现在,当我在我的spark sql查询中传递它作为列时,查询出现此异常
书面查询是
spark.sql("select d.BldgID,d.LeaseID,d.SuiteID,coalesce(BLDG.BLDGNAME,('select EmptyDefault from EmptyDefault')) as LeaseBldgName,coalesce(l.OCCPNAME,('select EmptyDefault from EmptyDefault'))as LeaseOccupantName, coalesce(l.DBA, ('select EmptyDefault from EmptyDefault')) as LeaseDBA, coalesce(l.CONTNAME, ('select EmptyDefault from EmptyDefault')) as LeaseContact,coalesce(l.PHONENO1, '')as LeasePhone1,coalesce(l.PHONENO2, '')as LeasePhone2,coalesce(l.NAME, '') as LeaseName,coalesce(l.ADDRESS, '') as LeaseAddress1,coalesce(l.ADDRESS2,'') as LeaseAddress2,coalesce(l.CITY, '')as LeaseCity, coalesce(l.STATE, ('select EmptyDefault from EmptyDefault'))as LeaseState,coalesce(l.ZIPCODE, '')as …
Run Code Online (Sandbox Code Playgroud) 我需要从hive结构中的所有列中选择*.
Hive创建表脚本如下所示
从表中选择*将每个结构显示为从表中选择*列
我的要求是将结构集合的所有字段显示为配置单元中的列.
用户不必单独编写列名.有没有人有UDF这样做?
想象一下下面的代码:
def myUdf(arg: Int) = udf((vector: MyData) => {
// complex logic that returns a Double
})
Run Code Online (Sandbox Code Playgroud)
如何定义 myUdf 的返回类型,以便查看代码的人立即知道它返回 Double?
我有一些 sql 想要传递到 mysql 存储过程中。我正在使用 mysql-json-udfs-0.4.0-labs-json-udfs-linux-glibc2.5-x86_64 中的 json 函数。我们正在运行 mysql 5.5.4 服务器。可以选择更新到 5.7.x。
当我跑步时
set @mapJSON = '[{"from":12,"to":0},{"from":11,"to":-1},{"from":1,"to":1}]' ;
select json_extract(@mapJSON,'from') `from`,json_extract(@mapJSON,'to') `to` ;
Run Code Online (Sandbox Code Playgroud)
我期待着
from to
12 0
11 -1
1 1
Run Code Online (Sandbox Code Playgroud)
我正进入(状态
from to
{"from":12,"to":0} {"from":12,"to":0}
Run Code Online (Sandbox Code Playgroud)
问题是如何使用 udf json_extract 0.4.0 从 json 数组中提取行?
我暂时通过使用comma_schema和 json解决了这个问题
{
"map": [
{
"from": 12,
"to": 0
},
{
"from": 1,
"to": 10
},
{
"from": 2,
"to": 20
},
{
"from": 3,
"to": 30
},
{
"from": 4,
"to": …
Run Code Online (Sandbox Code Playgroud) 您好,并提前感谢您.
我的程序是用java编写的,我无法移动到scala.
我目前正在使用以下行使用从json文件中提取的spark DataFrame:
DataFrame dff = sqlContext.read().json("filePath.son");
SQLContext和SparkContext被正确初始化并完美运行.
问题是我正在读取的json有嵌套结构,我想清理/验证内部数据,而不更改模式.
其中一个数据帧的列具有"GenericRowWithSchema"类型.
假设我想清理那个名为"data"的列.
我想到的解决方案是定义名为"cleanDataField"的用户定义函数(UDF),然后在"data"列上运行它.这是代码:
UDF1<GenericRowWithSchema,GenericRowWithSchema> cleanDataField = new UDF1<GenericRowWithSchema, GenericRowWithSchema>(){
public GenericRowWithSchema call( GenericRowWithSchema grws){
cleanGenericRowWithSchema(grws);
return grws;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我会在SQLContext中注册该函数:
sqlContext.udf().register("cleanDataField", cleanDataField, DataTypes.StringType);
Run Code Online (Sandbox Code Playgroud)
之后我会打电话
df.selectExpr("cleanDataField(data)").show(10, false);
为了查看带有干净数据的前10行.
最后,问题导致:我可以返回复杂数据(例如自定义类对象)吗?如果有可能,我应该怎么做?我想我必须更改udf注册的第3个参数,因为我没有返回一个字符串,但我应该替换它?
谢谢
我试图将一个struct in spark传递给udf.它正在更改字段名称并重命名为列位置.我如何解决它?
object TestCSV {
def main(args: Array[String]) {
val conf = new SparkConf().setAppName("localTest").setMaster("local")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val inputData = sqlContext.read.format("com.databricks.spark.csv")
.option("delimiter","|")
.option("header", "true")
.load("test.csv")
inputData.printSchema()
inputData.show()
val groupedData = inputData.withColumn("name",struct(inputData("firstname"),inputData("lastname")))
val udfApply = groupedData.withColumn("newName",processName(groupedData("name")))
udfApply.show()
}
def processName = udf((input:Row) =>{
println(input)
println(input.schema)
Map("firstName" -> input.getAs[String]("firstname"), "lastName" -> input.getAs[String]("lastname"))
})
}
Run Code Online (Sandbox Code Playgroud)
输出:
root
|-- id: string (nullable = true)
|-- firstname: string (nullable = true)
|-- lastname: string (nullable = true) …
Run Code Online (Sandbox Code Playgroud)