<jupyter_start><jupyter_text><jupyter_code>!apt-get update
!apt-get install openjdk-8-jdk-headless -qq > /dev/null
!wget -q https://dlcdn.apache.org/spark/spark-3.0.3/spark-3.0.3-bin-hadoop3.2.tgz
!tar xf spark-3.0.3-bin-hadoop3.2.tgz
!pip install -q findspark
import os
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-11-openjdk-amd64"
os.environ["SPARK_HOME"] = "/content/spark-3.0.3-bin-hadoop3.2"
os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages io.delta:delta-core_2.12:0.7.0 --conf spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension --conf spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog pyspark-shell'
import findspark
findspark.init()
from pyspark.sql import SparkSession
from pyspark.sql.functions import *
spark = SparkSession.builder.appName('delta_test').getOrCreate()
history = spark.range(1,11)
history = history.withColumn("type", lit("history"))
history.show()
history.write.format("delta").save("delta_sample2")

spark.sql("describe detail 'delta_sample2'").show(truncate=False)
# spark.sql("select id, type from delta_sample2").show()
history.write.format("delta").saveAsTable("delta_sample2_table")
spark.sql("select * from delta_sample2_table").show()<jupyter_output>+---+-------+
| id|   type|
+---+-------+
|  1|history|
|  2|history|
|  3|history|
|  4|history|
|  5|history|
|  6|history|
|  7|history|
|  8|history|
|  9|history|
| 10|history|
+---+-------+<jupyter_text>New Section<jupyter_code>!ls -l delta_sample2
updates = spark.range(6,11)
updates = updates.withColumn("type", lit("updates"))
updates.show()
from delta.tables import *
deltaTable = DeltaTable.forPath(spark, "delta_sample2")
deltaTable.alias("history").merge(
    updates.alias("updates"),
    "history.id = updates.id").whenMatchedUpdateAll().whenNotMatchedInsertAll().execute()
deltaTable.history().show(truncate=False)
spark.sql("describe detail 'delta_sample2'").show(truncate=False)
spark.read.format("delta").load("delta_sample2").groupBy("type").count().show()
spark.sql("SELECT * FROM delta_sample2 VERSION AS OF 0").show()
spark.sql("SELECT COUNT(*) FROM delta_sample2 VERSION AS OF 0").show()
df = spark.read.csv("/content/test.csv")
df.show()
# spark.sql("create table california_housing_train(longitude string,latitude string, housing_median_age double, total_rooms double, population double,population double,households string,median_income string  )  /content/sample_data/california_housing_train.csv")
spark.sql("create table test1 using DELTA location '/content/test.csv'")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17469643556600079, 'fraction_numerical': 0.023501762632197415, 'mean_word_length': 5.997260273972603, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '370010', 'license': 'Apache-2.0', 'n_tokens_mistral': 944, 'n_tokens_neox': 905, 'n_words': 139, 'path': 'delta_lake_upsert.ipynb', 'repo_name': 'nikhilmeghnani/AmazonSageMakerCourse', 'size': '2553'}