Icon

kn_example_python_read_orc_file

Read ORC file into KNIME's Python node

Read ORC file into KNIME's Python node
If you want to transfer several files from outside sources into the Python environment and not loose column types ORC is one (local) alternative from within KNIME

KNIME and Python — Setting up and managing Conda environments
https://medium.com/p/2ac217792539

Annotations and comments
  • Apache Spark On Apple Silicon https://medium.com/towards-data-engineering/apache-spark-on-apple-silicon-4ac61c5caf45 brew install apache-spark
  • import knime.scripting.io as knio # This example script creates an output table containing randomly drawn integers using numpy and pandas. import numpy as np import pandas as pd # https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_orc.html v_read_orc_file = knio.flow_variables['v_path_orc_file'] df = pd.read_orc(path=v_read_orc_file) knio.output_tables[0] = knio.Table.from_pandas(df)
  • import knime.scripting.io as knio import numpy as np import pandas as pd # you will have to install Apache Spark on your Machine # this is the version for MacOSX # https://medium.com/beeranddiapers/installing-apache-spark-on-mac-os-ce416007d79f # https://stackoverflow.com/questions/52889647/how-to-read-an-orc-file-stored-locally-in-python-pandas import findspark from pyspark.sql import SparkSession v_read_orc_file = knio.flow_variables['v_path_orc_file'] findspark.init() spark = SparkSession.builder.getOrCreate() df_spark = spark.read.orc(v_read_orc_file) df_pandas = df_spark.toPandas() knio.output_tables[0] = knio.Table.from_pandas(df_pandas)
  • import knime.scripting.io as knio # This example script creates an output table containing randomly drawn integers using numpy and pandas. import numpy as np import pandas as pd import pyarrow.orc as orc v_read_orc_file = knio.flow_variables['v_path_orc_file'] # https://stackoverflow.com/questions/52889647/how-to-read-an-orc-file-stored-locally-in-python-pandas with open(v_read_orc_file) as file: data = orc.ORCFile(file) df = data.read().to_pandas() knio.output_tables[0] = knio.Table.from_pandas(df)
  • https://stackoverflow.com/questions/52889647/how-to-read-an-orc-file-stored-locally-in-python-pandas https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_orc.html https://medium.com/beeranddiapers/installing-apache-spark-on-mac-os-ce416007d79f https://medium.com/towards-data-engineering/apache-spark-on-apple-silicon-4ac61c5caf45
  • context.workflow.parent-path
  • path.separator.system length_parent_path= length(column("context.workflow.parent-path")) substr(column("context.workflow.parent-path"), length_parent_path-1, 1)
  • => paths
  • create /data/ folder if it does not exist
  • context.workflow.absolute-path context.workflow.path context.workflow.name
  • context.workflow.data-path
  • data-path
  • path variables
  • py3_knime_spark
  • dummy data
  • test_file.orc
  • test_file_from_spark.orc (folder)
  • v_path_orc_file_from_spark
  • v_path_orc_file
  • v_path_data_folder_location
  • v_path_big_data_folder_location
  • v_path_upload_big_data_location
  • v_path_data_folder
  • v_path*
  • v_path_orc_file_from_spark read .orc files
  • ORC folder again to Spark
  • df_spark = spark.read.orc(v_read_orc_file) df_pandas = df_spark.toPandas()
  • pd.read_orc(path=v_read_orc_file) since Pandas 1.0.0 bundled KNIME Python version does work with macOS
  • does not work on MacOS or Windows ...
  • start a (Py)Spark session in background
  • pd.read_orc(path=v_read_orc_file) since Pandas 1.0.0
  • v_path_orc_file_from_spark as folder
  • v_path_orc_file_from*
  • test_data_all_types
  • determine path and name of workflow
  • determine file separator
  • var_path_data
  • var_path_parent_folder
  • var_pos_name
  • context*
  • if (equals(column("os.name"), "Mac OS X") ) {"0"} else if (contains(lowerCase(column("os.name")),"mac" )) {"0"} else {"1"}
  • conda.environment java.home
  • conda.environment "py3_knime_spark" Windows
  • conda.environment "py3_knime_spark" MacOSX Apple Scilicon
  • osgi.syspath => find where the KNIME installtion is located on Windows or MacOSX also java.home
  • $$ROWID$$ = "java.home" => TRUE
  • var_environment_validation
  • java.home
  • END
  • v_result_switch START
  • current_timestamp
  • a_upload_folder !!! create path for connection (CONNECTED, local, ../big_data/upload)
  • lives in /big_data/ folder under the local workflow or in the parent folder
  • search for the upload folder on the loacl big data system ../big_data on MacOS and Linux ..\big_data on Windows?
  • delete-value-selection
  • bigdata-folder-location
  • delete files from big_data folder keep files
  • v_result_switch
  • Path
  • filter types
  • Counter
  • new_id
  • original_row_id
  • $new_id$ >= 10 => TRUE TRUE => FALSE remove the first 10 lines

Nodes

Extensions

Links