This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select * from cast_ex.table_1 where val_int = 1; | |
select * from cast_ex.table_1 where val_int = '1'; | |
select * from cast_ex.table_1 where val_int_char = 1; | |
select * from cast_ex.table_1 where val_int_char = '1'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create database if not exists cast_ex; | |
create table if not exists cast_ex.table_1 ( | |
val_int int(10) not null, | |
val_int_char varchar(10) not null, | |
val_str varchar(10) not null | |
); | |
insert into cast_ex.table_1 | |
with recursive seq | |
as ( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select a, b, a = b | |
from ( | |
select 13 a, '13dfgs' b union all | |
select 13 a, '130dfgs' b union all | |
select 13 a, '13' b union all | |
select 13 a, '0013dfgs' b union all | |
select 13 a, ' 13dfgs' b union all | |
select 13 a, 'dfgs13' | |
) as t; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with dates | |
as | |
( | |
select '2019-12-12' as _date union all | |
select '2019/12/12' as _date union all | |
select '2019.12.12' as _date union all | |
select '2019_12_12' as _date union all | |
select '2019\12\12' as _date union all | |
select '2012#12#12' as _date union all | |
select '2019$12$12' as _date union all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alter table cast_ex.table_1 | |
add index idx_val_int (val_int), | |
add index idx_val_int_char(val_int_char); | |
explain select * from cast_ex.table_1 where val_int = '1'; | |
explain select * from cast_ex.table_1 where val_int_char = 1; | |
explain select * from cast_ex.table_1 force index (idx_val_int_char) where val_int_char = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select null is null null_is_null, | |
null is not null null_is_not_null, | |
1 = 0 1_0, | |
0 is null 0_is_null, | |
1 is null 1_is_null, | |
0 = 1 is null 0_1_is_null, | |
null = null null_equals_null, | |
null <> null null_not_equals_null, | |
null != null null_not_equals_null_2, | |
null < null null_less_than_null, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT t1.*, | |
(SELECT t2.field_name | |
FROM TABLE_2 AS t2 | |
WHERE t1.id = t2.id | |
LIMIT 1 | |
) | |
FROM TABLE_1 AS t1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT city, | |
FROM TABLE_1 AS t1 | |
LEFT JOIN TABLE_2 as t2 | |
ON t1.id = t2.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyspark | |
from pyspark import SparkContext | |
from pyspark.sql import SparkSession | |
from pyspark.sql import SQLContext | |
# create a SparkSession instance with the name moviedb with Hive support enabled | |
# https://spark.apache.org/docs/latest/sql-data-sources-hive-tables.html | |
spark = SparkSession.builder.appName("moviedb").enableHiveSupport().getOrCreate() | |
# create a SparkContext instance which allows the Spark Application to access |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# set the file_path variable in the beginning of the file | |
# or if your Spark application interacts with other applications, parameterize it | |
file_path = '/Users/kovid-r/datasets/moviedb/movies_metadata.csv' | |
# method 1 for reading a CSV file | |
df = spark.read.csv(file_path, header=True) | |
# method 2 for reading a CSV file | |
df = spark.read.format(csv_plugin).options(header='true', inferSchema='true').load(file_path) |