Based on the @mklement0's answer, i want to share the completed and tested solution with returning the JSON from python to Powershell with consideration of correct character encoding.
I tried it already with 100k Rows on one batch - no issues, running flawlessly and superfast :)
#get data from MS SQL
$query = -join@(
'SELECT `Id`, `CaseSubject`, `CaseDescription`,
`AccountCountry`, `CaseLang` '
'FROM `db`.`table_global` '
'ORDER BY `Id` DESC, `Id` ASC '
'LIMIT 10000;'
)
$data = Invoke-DbaQuery -SqlInstance $Connection -Query $Query -As PSObject -QueryTimeout 1800
$arg = @'
import pycld2 as cld2
import simplejson as json
import sys, re, logging
def main():
#toggle the logging level to stderr
# https://stackoverflow.com/a/6579522/14226613 -> https://docs.python.org/3/library/logging.html#logging.debug
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
logging.info('->Encoding Python: ' + str(sys.stdin.encoding))
# consideration of correct character encoding -> https://stackoverflow.com/a/30107752/14226613
# Parse the JSON passed via stdin into a list of dictionaries -> https://stackoverflow.com/a/65051178/14226613
cases = json.load(sys.stdin, 'utf-8')
# Sample processing: print the 'one' entry of each dict.
# https://regex101.com/r/bymIQS/1
regex = re.compile(r'(?=[^\w\s]).|[\r\n]|\'|\"|\\')
# hash table with Country vs Language for 'boosting' the language detection, if pycld2 is not sure
lang_country = {'Albania' : 'ALBANIAN', 'Algeria' : 'ARABIC', 'Argentina' : 'SPANISH', 'Armenia' : 'ARMENIAN', 'Austria' : 'GERMAN', 'Azerbaijan' : 'AZERBAIJANI', 'Bangladesh' : 'BENGALI', 'Belgium' : 'DUTCH', 'Benin' : 'FRENCH', 'Bolivia, Plurinational State of' : 'SPANISH', 'Bosnia and Herzegovina' : 'BOSNIAN', 'Brazil' : 'PORTUGUESE', 'Bulgaria' : 'BULGARIAN', 'Chile' : 'SPANISH', 'China' : 'Chinese', 'Colombia' : 'SPANISH', 'Costa Rica' : 'SPANISH', 'Croatia' : 'CROATIAN', 'Czech Republic' : 'CZECH', 'Denmark' : 'DANISH', 'Ecuador' : 'SPANISH', 'Egypt' : 'ARABIC', 'El Salvador' : 'SPANISH', 'Finland' : 'FINNISH', 'France' : 'FRENCH', 'Germany' : 'GERMAN', 'Greece' : 'GREEK', 'Greenland' : 'GREENLANDIC', 'Hungary' : 'HUNGARIAN', 'Iceland' : 'ICELANDIC', 'India' : 'HINDI', 'Iran' : 'PERSIAN', 'Iraq' : 'ARABIC', 'Ireland' : 'ENGLISH', 'Israel' : 'HEBREW', 'Italy' : 'ITALIAN', 'Japan' : 'Japanese', 'Kosovo' : 'ALBANIAN', 'Kuwait' : 'ARABIC', 'Mexico' : 'SPANISH', 'Monaco' : 'FRENCH', 'Morocco' : 'ARABIC', 'Netherlands' : 'DUTCH', 'New Zealand' : 'ENGLISH', 'Norway' : 'NORWEGIAN', 'Panama' : 'SPANISH', 'Paraguay' : 'SPANISH', 'Peru' : 'SPANISH', 'Poland' : 'POLISH', 'Portugal' : 'PORTUGUESE', 'Qatar' : 'ARABIC', 'Romania' : 'ROMANIAN', 'Russia' : 'RUSSIAN', 'San Marino' : 'ITALIAN', 'Saudi Arabia' : 'ARABIC', 'Serbia' : 'SERBIAN', 'Slovakia' : 'SLOVAK', 'Slovenia' : 'SLOVENIAN', 'South Africa' : 'AFRIKAANS', 'South Korea' : 'Korean', 'Spain' : 'SPANISH', 'Sweden' : 'SWEDISH', 'Switzerland' : 'GERMAN', 'Thailand' : 'THAI', 'Tunisia' : 'ARABIC', 'Turkey' : 'TURKISH', 'Ukraine' : 'UKRAINIAN', 'United Arab Emirates' : 'ARABIC', 'United Kingdom' : 'ENGLISH', 'United States' : 'ENGLISH', 'Uruguay' : 'SPANISH', 'Uzbekistan' : 'UZBEK', 'Venezuela' : 'SPANISH'}
for case in cases:
#concatenate two fiels and clean them a bitfield, so that we not get any faults due line brakes etc.
tCaseDescription = regex.sub('', (case['CaseSubject'] + ' ' + case['CaseDescription']))
tCaseAccCountry = case['AccountCountry']
if tCaseAccCountry in lang_country:
language = lang_country[tCaseAccCountry]
isReliable, textBytesFound, details = cld2.detect(tCaseDescription,
isPlainText = True,
bestEffort = True,
hintLanguage = language)
else:
isReliable, textBytesFound, details = cld2.detect(tCaseDescription,
isPlainText = True,
bestEffort = True)
#Take Value
case['CaseLang'] = details[0][0]
#logging.info('->Python processing CaseID: ' + str(case['Id']) + ' / Detected Language: ' + str(case['CaseLang']))
#encode to JSON
retVal = json.dumps(cases, 'utf-8')
return retVal
if __name__ == '__main__':
retVal = main()
sys.stdout.write(str(retVal))
'@
$dataJson = ConvertTo-Json $data
$data = ($dataJson | python -X utf8 -c $arg) | ConvertFrom-Json
foreach($case in $data) {
$tCaseSubject = $case.CaseSubject -replace "\\", "\\" -replace "'", "\'"
$tCaseDescription = $case.CaseDescription -replace "\\", "\\" -replace "'", "\'"
$tCaseLang = $case.CaseLang.substring(0,1).toupper() + $case.CaseLang.substring(1).tolower()
$tCaseId = $case.Id
$qUpdate = -join @(
"UPDATE db.table_global SET CaseSubject=`'$tCaseSubject`', "
"CaseDescription=`'$tCaseDescription`', "
"CaseLang=`'$tCaseLang`' "
"WHERE Id=$tCaseId;"
)
try{
$result = Invoke-SqlUpdate -ConnectionName 'maria' -Query $qUpdate
} catch {
Write-Host -Foreground Red -Background Black ("result: " + $result + ' / No. ' + $i)
#break
}
}
Close-SqlConnection -ConnectionName 'maria'
Please apologize the unfortunate syntax highlighting; script block contains SQL, Powershell and Python..