Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Replace single quote with two single quotes in a string

avatar
New Contributor

I am trying to consume data from kafka and insert into sql server. The kafka value has single quotes in it eg: "O'Keefe". PutSQL processor is failing to insert the string value into SQL server varchar column. I would like to replace the single quote with two single quotes in nifi so that I can insert the kafka value with single quote into sql server. Can you please give an example of replacetext processor how to achieve this.

Input: O'Keefe

Output: O''Keefe

Thanks

1 ACCEPTED SOLUTION

avatar
Master Guru
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login
4 REPLIES 4

avatar
New Contributor

@Shu

This is the current Nifi flow

ConsumeKafka -> UpdateAttribute -> ReplaceText -> PutSQL

*In UpdateAttribute I am capturing kafka.topic, kafka.offset,kafka.partition, kafka.key and assigning to variables.

topic=${kakfa.topic}

partition=${kakfa.partition}

offset=${kakfa.offset}

key=${kakfa.key}

*In ReplaceText processor I am replacing the flowfile content with insert statement.

searchvalue = (?s)(^.*$)

replacementvalue = Insert into test_schema.table1(topic, partition, offset, key, value)

values ('${topic}','${partition}','${offset}','${key}','$1')

But the nifi workflow is failing since the kafka value has single quotes in it as mentioned in initial post and sql server wont allow single quotes into the varchar column unless we double the single quote. I want to add one more ReplaceText processor and search the kafka value string and replace all the single quote occurrences with two single quotes. Please let me know what should be the search value and replacement value in the Replacetext processor. Please let me know if there is a better way to handle this scenario.

Thanks

avatar
Master Guru
hide-solution

This problem has been solved!

Want to get a detailed solution you have to login/registered on the community

Register/Login

avatar
New Contributor

Thank you Shu, it worked 🙂

avatar

NO! if you are trying to escape input to generate a sql query you should never roll your own sanitization unless you fully trust the input.

THIS IS VULNERABLE TO SQL INJECTION!

You should be using the '?' parameter substitution in your putsql stage.