src/utils/cassandra_helper.py
def push_tsv_to_database(self, file, table_name):
        """
        [summary]:This functions accepts tsv file and table_name and creates table in cassandra with given table_name
        and inserts values from the file given into created table
        Args:
            file ([type]): [file name to upload]
            table_name ([type]): [table name to create ]
        """
        try:
            session = self.cluster.connect(self.keyspace)
            create_table_query = f"create table {table_name}(column_count int, "
            insert_into_table_query = f"insert into {table_name}(column_count , "
            columns = ["_".join(i.split()).replace(" ", "") for i in str(pd.read_csv(file).dtypes).split("\n")]
            columns.pop()

            for col in columns:
                create_table_query += f"{col} text, "
                insert_into_table_query += f'{col}, '

            query = create_table_query.strip() + f" primary key(column_count));"
            session.execute(query)
            print(query)
            print("table created")
            query = insert_into_table_query.strip(", '") + ")" + " values(" + str((len(columns) + 1) * ('?,')).strip(", ") + ");"
            print(query)
            prepared_query = session.prepare(query)
            count = 0

            with open(file, "r") as csvfile:
                next(csvfile)
                for line in csvfile:
                    data = (line.strip("\n").replace(",,", ",NaN,").replace('"', '').replace("'", "").split(","))
                    count += 1;
                    data.insert(0, count)
                    try:
                        session.execute(prepared_query, data)
                    except Exception as e:
                        print(e)
        except Exception as e:
            print(e)

        else:
            if not session.is_shutdown:
                session.shutdown()
                print('Cassand session closed!')

        [summary]:This functions accepts tsv file and table_name and creates table in cassandra with given table_name
        and inserts values from the file given into created table
        Args:
            file ([type]): [file name to upload]
            table_name ([type]): [table name to create ]
        
[summary]:This functions accepts tsv file and table_name and creates table in cassandra with given table_name
and inserts values from the file given into created table
[ summary ] : This functions accepts tsv file and table_name and creates table in cassandra with given table_name and inserts values from the file given into created table
def push_tsv_to_database(self, file, table_name):
        try:
            session = self.cluster.connect(self.keyspace)
            create_table_query = f"create table {table_name}(column_count int, "
            insert_into_table_query = f"insert into {table_name}(column_count , "
            columns = ["_".join(i.split()).replace(" ", "") for i in str(pd.read_csv(file).dtypes).split("\n")]
            columns.pop()
            for col in columns:
                create_table_query += f"{col} text, "
                insert_into_table_query += f'{col}, '
            query = create_table_query.strip() + f" primary key(column_count));"
            session.execute(query)
            print(query)
            print("table created")
            query = insert_into_table_query.strip(", '") + ")" + " values(" + str((len(columns) + 1) * ('?,')).strip(", ") + ");"
            print(query)
            prepared_query = session.prepare(query)
            count = 0
            with open(file, "r") as csvfile:
                next(csvfile)
                for line in csvfile:
                    data = (line.strip("\n").replace(",,", ",NaN,").replace('"', '').replace("'", "").split(","))
                    count += 1;
                    data.insert(0, count)
                    try:
                        session.execute(prepared_query, data)
                    except Exception as e:
                        print(e)
        except Exception as e:
            print(e)
        else:
            if not session.is_shutdown:
                session.shutdown()
                print('Cassand session closed!')
def push_tsv_to_database ( self , file , table_name ) : try : session = self . cluster . connect ( self . keyspace ) create_table_query = f"create table {table_name}(column_count int, " insert_into_table_query = f"insert into {table_name}(column_count , " columns = [ "_" . join ( i . split ( ) ) . replace ( " " , "" ) for i in str ( pd . read_csv ( file ) . dtypes ) . split ( "\n" ) ] columns . pop ( ) for col in columns : create_table_query += f"{col} text, " insert_into_table_query += f'{col}, ' query = create_table_query . strip ( ) + f" primary key(column_count));" session . execute ( query ) print ( query ) print ( "table created" ) query = insert_into_table_query . strip ( ", '" ) + ")" + " values(" + str ( ( len ( columns ) + 1 ) * ( '?,' ) ) . strip ( ", " ) + ");" print ( query ) prepared_query = session . prepare ( query ) count = 0 with open ( file , "r" ) as csvfile : next ( csvfile ) for line in csvfile : data = ( line . strip ( "\n" ) . replace ( ",," , ",NaN," ) . replace ( '"' , '' ) . replace ( "'" , "" ) . split ( "," ) ) count += 1 ; data . insert ( 0 , count ) try : session . execute ( prepared_query , data ) except Exception as e : print ( e ) except Exception as e : print ( e ) else : if not session . is_shutdown : session . shutdown ( ) print ( 'Cassand session closed!' )
[summary]:This functions accepts tsv file and table_name and creates table in cassandra with given table_name
and inserts values from the file given into created table
[ summary ] : This functions accepts tsv file and table_name and creates table in cassandra with given table_name and inserts values from the file given into created table
"""
        [summary]:This functions accepts tsv file and table_name and creates table in cassandra with given table_name
        and inserts values from the file given into created table
        Args:
            file ([type]): [file name to upload]
            table_name ([type]): [table name to create ]
        """
self
file
[file name to upload]
[ file name to upload ]
table_name
[table name to create ]
[ table name to create ]