Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Median-udf in mysql : Step By Step Installation And Problem Fixes For Installing My Sql Udf
1. My step by step installation and problem fixes of MySQL
Statistical UDF – Median.
Wikipedia: “In probability theory and statistics, a median is described as the numeric
value separating the higher half of a sample, a population, or a probability
distribution, from the lower half. The median of a finite list of numbers can be found
by arranging all the observations from lowest value to highest value and picking the
middle one. If there is an even number of observations, then there is no single
middle value, so one often takes the mean of the two middle values.”
Approach for obtaining Median, the middle most value, earlier was as follows:
Create a temporary table to obtain middle most value(s) and then take average of
them.
1. CREATE TEMPORARY TABLE medians SELECT x.val FROM data x, data y GROUP BY
x.val HAVING ((COUNT(*)-(SUM(SIGN(1-SIGN(y.val-x.val))))) <= floor((COUNT(*)
+1)/2)) and ((COUNT(*)-(SUM(SIGN(1+SIGN(y.val-x.val))))) <= floor((COUNT(*)
+1)/2));
2. SELECT AVG(val) AS median FROM medians;
[As suggested by Kreaseck.]
But Median-UDF has brought us to ease in getting median value easily.
Following is a kind of snap shot / steps I executed on my linux- ubuntu box to get
the Median UDF working on MySQL 5.0.
I faced approx 3 minor errors which I got resolved; they’re also included in steps.
root@krex:~/mysqludf# wget
http://www.mooreds.com/files/mysql-udf-patched.tar.gz
root@krex:~/mysqludf# ls
mysql-udf-patched.tar.gz
root@krex:~/mysqludf# tar -xzf mysql-udf-patched.tar.gz
root@krex:~/mysqludf# cd mysql-udf/
root@krex:~/mysqludf/mysql-udf# ls
udf_colwidth.cc udf_correlation.cc udf_intercept.cc
udf_median.cc udf_slope.cc udf_weightedavg.cc
udf_confidence_higher.cc udf_faculty.cc udf_kurtosis.cc
udf_noverm.cc udf_stdnorm_density.cc
udf_confidence_lower.cc udf_geomean.cc udf_longest.cc
udf_skewness.cc udf_stdnorm_dist.cc
2. root@krex:~/mysqludf/mysql-udf# gcc -shared -lstdc++ -I
/usr/include -I /usr/local/include -I /usr/local/mysql/include/ -o
udf_median.so udf_median.cc
udf_median.cc:31:23: error: my_global.h: No such file or directory
udf_median.cc:32:20: error: my_sys.h: No such file or directory
udf_median.cc:34:19: error: mysql.h: No such file or directory
udf_median.cc:35:21: error: m_ctype.h: No such file or directory
udf_median.cc:36:24: error: m_string.h: No such file or directory
root@krex:~/mysqludf/mysql-udf# locate my_global.h
/opt/lampp/include/mysql/my_global.h
root@krex:~/mysqludf/mysql-udf# gcc -shared -lstdc++ -I
/usr/include -I /usr/local/include -I /opt/lampp/include/ -I
/opt/lampp/include/mysql/ -o udf_median.so udf_median.cc
In file included from udf_median.cc:31:
/opt/lampp/include/mysql/my_global.h:353:24: error: asm/atomic.h:
No such file or directory
root@krex:~/mysqludf/mysql-udf# locate atomic.h
/opt/lampp/include/atomic.h
root@krex:~/mysqludf/mysql-udf# mkdir /opt/lampp/include/asm
root@krex:~/mysqludf/mysql-udf# cp /opt/lampp/include/atomic.h
/opt/lampp/include/asm/
root@krex:~/mysqludf/mysql-udf# gcc -shared -lstdc++ -I
/usr/include -I /usr/local/include -I /opt/lampp/include/ -I
/opt/lampp/include/mysql/ -o udf_median.so udf_median.cc
/usr/bin/ld: /tmp/ccimPFiX.o: relocation R_X86_64_32 against
`compare_doubles(void const*, void const*)' can not be used when
making a shared object; recompile with -fPIC
/tmp/ccimPFiX.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
root@krex:~/mysqludf/mysql-udf# uname -a
Linux xeon-01 2.6.20-15-generic #2 SMP Sun Apr 15 06:17:24 UTC
2007 x86_64 GNU/Linux
root@krex:~/mysqludf/mysql-udf# gcc -shared -fPIC -lstdc++ -I
/usr/include -I /usr/local/include -I /opt/lampp/include/ -I
/opt/lampp/include/mysql/ -o udf_median.so udf_median.cc
3. root@krex:~/mysqludf/mysql-udf# ls
udf_colwidth.cc udf_correlation.cc udf_intercept.cc
udf_median.cc udf_skewness.cc udf_stdnorm_dist.cc
udf_confidence_higher.cc udf_faculty.cc udf_kurtosis.cc
udf_median.so udf_slope.cc udf_weightedavg.cc
udf_confidence_lower.cc udf_geomean.cc udf_longest.cc
udf_noverm.cc udf_stdnorm_density.cc
root@krex:~/mysqludf/mysql-udf# cp udf_median.so /usr/lib/
root@krex:~/mysqludf/mysql-udf# mysql -uroot –p test
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 84928
Server version: 5.0.38-Ubuntu_0ubuntu1-log Ubuntu 7.04 distribution
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql> create aggregate function median returns real soname
'udf_median.so';
Query OK, 0 rows affected (0.04 sec)
mysql> select median(SalePrice) from DB.Table_Name;
+-------------------+
| median(SalePrice) |
+-------------------+
| 262000.00 |
+-------------------+
1 row in set (2.49 sec)
Voila…. Installation finished!!! Now it’s easy enough to obtain a median!
http://kedar.nitty-witty.com/blog/