soumilshah1995 commented on issue #10110:
URL: https://github.com/apache/hudi/issues/10110#issuecomment-1814400817

   Here is Full code for references 
   
   
   ```
   
   from pyspark.sql import SparkSession
   from pyspark.sql.types import StructType, StructField, StringType, 
TimestampType, FloatType
   from datetime import datetime
   
   HUDI_VERSION = '1.0.0-beta1'
   SPARK_VERSION = '3.4'
   
   SUBMIT_ARGS = f"--packages 
org.apache.hudi:hudi-spark{SPARK_VERSION}-bundle_2.12:{HUDI_VERSION} 
pyspark-shell"
   os.environ["PYSPARK_SUBMIT_ARGS"] = SUBMIT_ARGS
   os.environ['PYSPARK_PYTHON'] = sys.executable
   
   # Spark session
   spark = SparkSession.builder \
       .config('spark.serializer', 
'org.apache.spark.serializer.KryoSerializer') \
       .config('spark.sql.extensions', 
'org.apache.spark.sql.hudi.HoodieSparkSessionExtension') \
       .config('className', 'org.apache.hudi') \
       .config('spark.sql.hive.convertMetastoreParquet', 'false') \
       .getOrCreate()
   
   data = [
       [datetime.strptime('2023-09-20 03:58:59', '%Y-%m-%d %H:%M:%S'), 
'334e26e9-8355-45cc-97c6-c31daf0df330', 'rider-A', 'driver-K', 19.10, 
'san_francisco'],
       [datetime.strptime('2023-09-19 08:46:34', '%Y-%m-%d %H:%M:%S'), 
'e96c4396-3fad-413a-a942-4cb36106d721', 'rider-C', 'driver-M', 27.70, 
'san_francisco'],
       [datetime.strptime('2023-09-18 17:45:31', '%Y-%m-%d %H:%M:%S'), 
'9909a8b1-2d15-4d3d-8ec9-efc48c536a00', 'rider-D', 'driver-L', 33.90, 
'san_francisco'],
       [datetime.strptime('2023-09-22 13:12:56', '%Y-%m-%d %H:%M:%S'), 
'1dced545-862b-4ceb-8b43-d2a568f6616b', 'rider-E', 'driver-O', 93.50, 
'san_francisco'],
       [datetime.strptime('2023-09-24 06:15:45', '%Y-%m-%d %H:%M:%S'), 
'e3cf430c-889d-4015-bc98-59bdce1e530c', 'rider-F', 'driver-P', 34.15, 
'sao_paulo'],
       [datetime.strptime('2023-09-22 15:21:36', '%Y-%m-%d %H:%M:%S'), 
'7a84095f-737f-40bc-b62f-6b69664712d2', 'rider-G', 'driver-Q', 43.40, 
'sao_paulo'],
       [datetime.strptime('2023-09-20 12:35:45', '%Y-%m-%d %H:%M:%S'), 
'3eeb61f7-c2b0-4636-99bd-5d7a5a1d2c04', 'rider-I', 'driver-S', 41.06, 
'chennai'],
       [datetime.strptime('2023-09-19 05:34:56', '%Y-%m-%d %H:%M:%S'), 
'c8abbe79-8d89-47ea-b4ce-4d224bae5bfa', 'rider-J', 'driver-T', 17.85, 'chennai']
   ]
   
   # Define schema for the DataFrame
   schema = StructType([
       StructField("ts", TimestampType(), True),
       StructField("transaction_id", StringType(), True),
       StructField("rider", StringType(), True),
       StructField("driver", StringType(), True),
       StructField("price", FloatType(), True),
       StructField("location", StringType(), True),
   ])
   
   # Create Spark DataFrame
   df = spark.createDataFrame(data, schema=schema)
   
   # Show the DataFrame
   df.show()
   
   
   db_name = "hudidb"
   table_name = "hudi_table_func_index"
   
   path = f"file:///Users/soumilnitinshah/Downloads/{db_name}/{table_name}"  # 
Updated path
   method = 'upsert'
   table_type = "COPY_ON_WRITE" 
   recordkey = "transaction_id"
   precombine = "ts"
   
   partition_fields = "location"
   
   
   hudi_options = {
       'hoodie.table.name': table_name,
       'hoodie.datasource.write.table.type': table_type,
       'hoodie.datasource.write.table.name': table_name,
       'hoodie.datasource.write.operation': method,
       'hoodie.datasource.write.recordkey.field': recordkey,
       'hoodie.datasource.write.precombine.field': precombine,
       "hoodie.table.metadata.enable": "true",
       "hoodie.datasource.write.partitionpath.field": partition_fields
       
   }
   
   
   df.write.format("hudi"). \
       options(**hudi_options). \
       mode("append"). \
       save(path)
   
   
   # path = 
'file:///Users/soumilnitinshah/Downloads/hudidb/hudi_table_func_index'
   # 
spark.read.format("hudi").load(path).createOrReplaceTempView("hudi_table_func_index")
   
   
   functional_index_sql = """
       CREATE INDEX IF NOT EXISTS ts_hour
       ON  hudi_table_func_index
       USING column_stats(ts)
   """
   
   spark.sql(functional_index_sql)
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to