我正在遍历一个包含 2 个 json 文件的文件夹,从“名称”键中提取值并将它们存储在一个变量中。
然后我尝试使用这些变量执行一些操作(例如:与 curl 请求的结果进行比较)并打印控制台发生的事情。
我遇到的问题是脚本的回显输出似乎混淆了一些变量和字符串文本。它似乎只发生在第一个循环中,第二个循环看起来不错。
我不确定为什么会这样。查看下面的脚本、脚本输出和源文件,任何人都可以看出为什么我的输出在第一个循环中会以这种方式混乱吗?也许是我使用动态变量?
下面的脚本、脚本输出和文件+内容...
脚本
###############################
# SETTINGS
###############################
connectorsDirectory="sources"
connectHost=localhost
###############################
# INIT
###############################
# Get a list of all currently running connectors
connectorListResponse=$(curl -s $connectHost:8083/connectors)
runningConnectorList=$(echo $connectorListResponse | sed -e 's/\[\(.*\)\]/\1/')
echo $runningConnectorList
echo "==="
# Convert runningConnectorList into something parseable by removing square brackets and double-quotes
parseableRunningConnectorList=`echo $runningConnectorList | tr -d '[]"'`
echo $parseableRunningConnectorList
echo "===="
connectorName=""
genericConnectorName=""
###############################
# JOB
###############################
# Loop through JSON files in provided connectors directory
for filename in $connectorsDirectory/*.json; do
[ -e "$filename" ] || continue
echo "Reading $filename ..."
# Find connector name specified in file
#connectorName=$(cat $filename | sed -n 's|.*"name":"\([^"]*\)".*|\1|p')
connectorName=$(sed -nE '/name/{s/.*:\s*"(.*)",/\1/p;q}' $filename)
#connectorNameSED='/name/{s/.*:\s*"(.*)",/\1/p;q}'
echo "connectorName="$connectorName
# Find "generic" connector name (sans .v### at END of string)
genericConnectorName=$(echo "$connectorName" | sed 's/\.v[0-9]*$//')
echo "genericConnectorName="$genericConnectorName
if [ -z "$connectorName" ]
then
echo "No connector found in this file."
else
echo "Found connector in file named: "$filename" (generic name = "$genericConnectorName")"
fi
echo "---"
# Loop through running connectors and find those that start with our "generic" connector name
# If there are any connectors running AND we also found a connector in the current file...
if [ ! -z "$parseableRunningConnectorList" ] && [ ! -z "$connectorName" ]
then
echo "Turning off the following connectors..."
# Loop through running connectors and delete them (turn them off)
for runningConnectorName in $(echo $parseableRunningConnectorList | sed "s/,/ /g")
do
connectorToTurnOff=$(echo "$runningConnectorName" | grep ^$genericConnectorName)
if [ ! -z $connectorToTurnOff ]
then
echo $connectorToTurnOff
echo "TEST MODE: curl -X DELETE $connectHost:8083/connectors/$connectorToTurnOff"
#curl -X DELETE $connectHost:8083/connectors/$connectorToTurnOff
#add some logic here to concat found connectors
fi
done
else
# No running connectors, so nothing to turn off ahead of publishing new connector (ie: do nothing)
echo "No connector in this file or no running connectors needing to be stopped."
fi
# Publish the connector found in the current file
if [ -z $connectorName ]
then
echo "No connector job to publish in the current file."
else
echo "Publishing connect job: \"$connectorName\"..."
echo "TEST MODE: curl -X POST -H \"Content-Type: application/json\" --data @$filename http://$connectHost:8083/connectors"
echo "TEST MODE: Connect job \"$connectorName\" is now active."
#curl -X POST -H "Content-Type: application/json" --data @$filename http://$connectHost:8083/connectors
#echo "Connect job \"$connectorName\" is now active."
fi
#
echo ""
echo "#############################################################################"
echo ""
done
脚本输出
[centos@localhost test]$ ./publish_connectors.sh
"some.sink.connector.running","some.source.connector.running","cars.sales.test_v2","some.other.source.connector.v001"
===
some.sink.connector.running,some.source.connector.running,cars.sales.test_v2,some.other.source.connector.v001
====
Reading sources/stanbridge.connect.source.test.v01.json ...
connectorName=stanbridge.connect.source.test.v001
genericConnectorName=stanbridge.connect.source.test.v001
)ound connector in file named: sources/stanbridge.connect.source.test.v01.json (generic name = stanbridge.connect.source.test.v001
---
Turning off the following connectors...
"...ishing connect job: "stanbridge.connect.source.test.v001
TEST MODE: curl -X POST -H "Content-Type: application/json" --data @sources/stanbridge.connect.source.test.v01.json http://localhost:8083/connectors
" is now active.ct job "stanbridge.connect.source.test.v001
#############################################################################
Reading sources/cars.sales.test.json ...
connectorName=cars.sales.test.v001
genericConnectorName=cars.sales.test
Found connector in file named: sources/cars.sales.test.json (generic name = cars.sales.test)
---
Turning off the following connectors...
Publishing connect job: "cars.sales.test.v001"...
TEST MODE: curl -X POST -H "Content-Type: application/json" --data @sources/cars.sales.test.json http://localhost:8083/connectors
TEST MODE: Connect job "cars.sales.test.v001" is now active.
#############################################################################
文件1:来源/stanbridge.connect.source.test.v01.json
{
"name": "stanbridge.connect.source.test.v001",
"config": {
"connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
"group.id": "stanbridge.connect.source.test.v01xxyyzza",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"value.converter": "io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url": "https://my.schema.registry",
"value.converter.schema.registry.url": "https://my.schema.registry",
"key.converter.basic.auth.user.info": "someKeyString:someSecretString",
"value.converter.basic.auth.user.info": "SsomeSString:someSecretString",
"errors.log.enable": "true",
"errors.log.include.messages": "true",
"connection.url": "jdbc:postgresql://my.db.host/dbName",
"connection.user": "postgres",
"connection.password": "postgres",
"mode": "incrementing",
"incrementing.column.name": "audit_id",
"query": "SELECT customer_id, customer_name, audit_id FROM cars.customers",
"topic.prefix": "stanbridge.connect.source.test"
}
}
文件2:来源/cars.sales.test.json
{"name": "cars.sales.test.v001",
"config": {
"connector.class": "io.confluent.connect.jdbc.JdbcSourceConnector",
"group.id": "cars.sales.test.v001",
"value.converter": "io.confluent.connect.avro.AvroConverter",
"value.converter.schema.registry.url": "https://my.schema.registry",
"value.converter.basic.auth.credentials.source": "USER_INFO",
"value.converter.schema.registry.basic.auth.user.info": "someKeyString:someSecretString",
"key.converter": "io.confluent.connect.avro.AvroConverter",
"key.converter.schema.registry.url": "https://my.schema.registry",
"key.converter.basic.auth.credentials.source": "USER_INFO",
"key.converter.schema.registry.basic.auth.user.info": "someKeyString:someSecretString",
"errors.log.enable": "true",
"errors.log.include.messages": "true",
"tasks.max": "2",
"batch.max.rows": "100",
"connection.user": "postgres",
"connection.password": "",
"connection.url": "jdbc:postgresql://my.db.host/dbName",
"query": "SELECT car_id, field1, field2, field3, date_modified_utc FROM cars.sales",
"mode": "timestamp",
"timestamp.column.name": "date_modified_utc",
"poll.interval.ms": "5000",
"timeout.ms": "25000",
"topic.prefix": "cars.sales.dummy",
"transforms": "createKey",
"transforms.createKey.type": "org.apache.kafka.connect.transforms.ValueToKey",
"transforms.createKey.fields": "car_id"
}
}
在脚本输出中,请注意第一个循环中的以下内容...
sed 没有删除 .v001 后缀: genericConnectorName=stanbridge.connect.source.test.v001 这在命令行中直接使用时有效
行输出中覆盖的文本以: ) 开头的文件名为
行输出中覆盖的文本以:“...ishing 连接作业:”stanbridge.connect.source.test.v001 开头
行输出中覆盖的文本开头为:“现在是 active.ct 作业”stanbridge.connect.source.test.v001
注意:我认为如果我包含整个脚本会最容易理解,因为它并没有那么大。抱歉,如果这是不好的形式。
非常感谢您对此提供的任何帮助。谢谢!
斯坦布里奇