2016-04-18 50 views

回答

1

首先閱讀:

這樣做的,這個腳本奮鬥了近一個星期後,我決定嘗試使用java庫的一個做腳本來代替。我已經在該項目中創建了一個公共回購協議。好處是,您不僅限於〜8000個字符的輸入變量,而且運行速度要快得多。我要離開這裏批處理腳本的人誰絕對需要這樣來做,但我會強烈建議使用Java代碼來代替:

Redis Scripting Project

實際答:

使用批處理文件,我能夠從該文章複製bash腳本。如果你的批處理腳本是不是你的路徑上

batchScriptName keyMatch field luaScriptName.lua host port 
batchScriptName myKey* us luaScriptName.lua localhost 6379 

,那麼你將不得不從您的文件所在的目錄調用命令:

@echo off 
setlocal EnableDelayedExpansion 

echo Starting removal of keys from redis. 
echo KeyMatch: %1 
echo Field: %2 
echo Script: %3 
echo Host: %4 
echo Port: %5 

REM set the cursor to 0 to begin iterating over matching keys 
set cursor=0 

:loop 
REM call redis scan and output the result to temp.txt 
call redis-cli -h %4 -p %5 scan !cursor! match %1 count 180 > temp.txt 

REM set the first line of the temp file to the new cursor variable 
set /p cursor=<temp.txt 

REM outer loop variables 
set /A i=0 
set keyString= 

REM loop through the text file to build the key string 
for /F "usebackq delims=" %%a in ("temp.txt") do (
    set /A i+=1 
    REM if we are not on the first line save the key to a space delimted string 
    if NOT !i! == 1 (
     call set keyString=!keyString! %%a 
    ) 
) 

rem if there is only one line in the file skip the script execution 
if !i! LEQ 1 (
    goto :checkCursor 
) 

rem check that the length of keyString will not likely violate the 8192 character limit to command line calls 
ECHO !keyString!> strlength.txt 
FOR %%? IN (strlength.txt) DO (SET /A strlength=%%~z? - 2) 
if !strlength! GTR 8000 (
    echo. 
    echo. 
    echo ****Error processing script. Key string is too long. Reduce the count in call to scan.**** 
    echo. 
    echo. 
    GOTO :end 
) 

REM call the script with the keys from the scan task, output to result.txt to prevent writing to the command line each iteration. 
call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt 

REM output '.' to the commandline to signify progress 
<nul set /p=. 

:checkCursor 
if not !cursor!==0 (
    goto :loop 
) 

:end 
set fileToDelete=temp.txt 
if exist !fileToDelete! del /F !fileToDelete! 
set fileToDelete=result.txt 
if exist !fileToDelete! del /F !fileToDelete! 
set fileToDelete=strlength.txt 
if exist !fileToDelete! del /F !fileToDelete! 

echo Completed script execution 
endlocal 

,你甚至可以從像在命令行這個腳本。同樣,對於lua腳本,您需要提供完整的文件路徑引用或從lua腳本所在的目錄調用批處理腳本。

此腳本設置爲使用redis中的散列值。如果你需要改變的是,你可能會希望改變這一行:

call redis-cli -h %4 -p %5 --eval %3 !keyString:~1! , %2 > result.txt 

的「%2」的字段值在LUA腳本的argv數組傳遞,如果你沒有,你可以刪除這個需要它。您還可以根據需要添加其他ARGV參數。

相關問題