Looking for databricks python/pyspark code to copy azure blob from one container to another container older than 30 days
Asked
Active
Viewed 259 times
-1
-
Welcome to Stack Overflow! Please check [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) "_Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it._" – user2314737 Apr 25 '22 at 07:44
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 03 '22 at 14:38
1 Answers
0
The copy code is simple as follows.
dbutils.fs.cp("/mnt/xxx/file_A", "/mnt/yyy/file_A", True)
The difficult part is checking blob modification time. According to the doc, the modification time will only get returned by using
dbutils.fs.ls
command on Databricks Runtime 10.2 or above. You may check the Runtime version using the command below.spark.conf.get("spark.databricks.clusterUsageTags.sparkVersion")
The returned value will be Databricks Runtime followed by Scala versions.
If you get lucky with the version, you can can do something like:import time ts_now = time.time() for file in dbutils.fs.ls('/mnt/xxx'): if ts_now - file.modificationTime > 30 * 86400: dbutils.fs.cp(f'/mnt/xxx/{file.name}', f'/mnt/yyy/{file.name}', True)

Phuri Chalermkiatsakul
- 551
- 4
- 10
-
Here time.time is giving time in seconds. Is there any method to give timeframe in yymmdd. – Sankar Azad Apr 25 '22 at 12:14
-
With reference to this [thread](https://stackoverflow.com/questions/32490629/getting-todays-date-in-yyyy-mm-dd-in-python), you may convert timestamp to any format you want. First `dt = datetime.fromtimestamp(ts)` then `dt.strftime('%Y-%m-%d %H:%M:%S')` – Phuri Chalermkiatsakul Apr 25 '22 at 13:15
-
Hey Could you please help me with how do I navigate for the log file maintained by azure for the operation in azure lifecycle management policy. – Sankar Azad Apr 26 '22 at 05:25
-
It would be better to create a new question for that and see if someone can help. – Phuri Chalermkiatsakul Apr 26 '22 at 06:26