Unable to create or search index using groovy - groovy

I'm using below method to check and create elasticsearch index. This script works perfectly for Elasticsearch 2.3 and 2.4 versions. I'm trying things with Elasticsearch version 5.0 but it isn't working. All i'm trying is to create an index and search index dynamically using groovy script.
static def checkOrCreateESIndex(String baseUrl, String path)
{
try
{
def res_GET = null
def res_PUT = null
def status_message = null
def http = new HTTPBuilder(baseUrl)
println "New ES Check Index : "+baseUrl+path
http.request(Method.GET, ContentType.JSON)
{
uri.path = path
requestContentType = ContentType.XML
headers.'Accept-Encoding' = 'gzip,deflate'
headers.Accept = 'application/json';
response.success = { resp ->
res_GET = 200
println "SUCCESS! ${resp.status}"
}
response.failure = { resp ->
res_GET = 400
println "Failure! ${resp.status}"
}
}
if (res_GET != 200)
{
String params = "{\"settings\":{\"number_of_shards\":2,\"number_of_replicas\":0},\"mappings\":{\"run\":{\"_timestamp\":{\"enabled\":true},\"properties\":{\"70 Percentile\":{\"type\":\"float\"},\"80 Percentile\":{\"type\":\"float\"},\"85 Percentile\":{\"type\":\"float\"},\"95 Percentile\":{\"type\":\"float\"},\"90 Percentile\":{\"type\":\"float\"},\"Average\":{\"type\":\"float\"},\"Fail\":{\"type\":\"string\"},\"Maximum\":{\"type\":\"float\"},\"Minimum\":{\"type\":\"float\"},\"Pass\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"ProjectName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"RunID\":{\"type\":\"string\"},\"VirtualUsers\":{\"type\":\"string\"},\"Release\":{\"type\":\"string\"},\"BuildNumber\":{\"type\":\"string\"},\"StartTime\":{\"type\":\"string\"},\"EndTime\":{\"type\":\"string\"},\"StdDeviation\":{\"type\":\"string\"},\"TestName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"TransactionName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"Baseline\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"SLAviolationcount\":{\"type\":\"float\"}}}}}"
def bodyMap2 = new JsonSlurper().parseText(params)
def response_body = null
def response_header = null
def http2 = new HTTPBuilder(baseUrl)
println "New ES Create Index : "+baseUrl+path
println "New ES Mapping : "+params
http2.request(Method.PUT)
{
uri.path = path
requestContentType = ContentType.JSON
headers.'Accept' = 'application/json';
body = bodyMap2
headers.'Accept-Encoding' = 'gzip,deflate'
headers.'Cookie' = 'JSESSIONID=934ED773C47D81C74C63BEAFE1D6CA1B'
response.success = { resp ->
res_PUT = 200
println "SUCCESS! ${resp.status}"
}
response.failure = { resp ->
res_PUT = 400
println "Failure! ${resp.status}"
}
}
}
if (res_GET == 200)
{
status_message = "IDX_EXISTS"
}
else if (res_GET != 200 && res_PUT == 200)
{
status_message = "IDX_CREATED"
}
else
{
status_message = "IDX_FAIL"
}
return status_message
}
catch (groovyx.net.http.HttpResponseException ex)
{
ex.printStackTrace()
return null
}
catch (java.net.ConnectException ex)
{
ex.printStackTrace()
return null
}
}
static def postElasticSearchMessage(String baseUrl, String path,String params)
{
try
{
def res_ES = null
def bodyMap = new JsonSlurper().parseText(params)
def response_body = null
def response_header = null
def http = new HTTPBuilder(baseUrl)
http.request(Method.POST)
{
uri.path = path
requestContentType = ContentType.JSON
body = bodyMap
headers.'Accept-Encoding' = 'gzip,deflate'
headers.'Cookie' = 'JSESSIONID=934ED773C47D81C74C63BEAFE1D6CA1B'
response.success = { resp ->
res_ES = 'Y'
println "SUCCESS! ${resp.status}"
}
response.failure = { resp ->
res_ES = 'N'
println "FAILURE! ${resp.status}"
}
}
return res_ES
}
catch (groovyx.net.http.HttpResponseException ex)
{
ex.printStackTrace()
return 'N'
}
catch (java.net.ConnectException ex)
{
ex.printStackTrace()
return 'N'
}
}
Below is my index structure:
{\"settings\":{\"number_of_shards\":2,\"number_of_replicas\":0},\"mappings\":{\"run\":{\"_timestamp\":{\"enabled\":true},\"properties\":{\"70 Percentile\":{\"type\":\"float\"},\"80 Percentile\":{\"type\":\"float\"},\"85 Percentile\":{\"type\":\"float\"},\"95 Percentile\":{\"type\":\"float\"},\"90 Percentile\":{\"type\":\"float\"},\"Average\":{\"type\":\"float\"},\"Fail\":{\"type\":\"string\"},\"Maximum\":{\"type\":\"float\"},\"Minimum\":{\"type\":\"float\"},\"Pass\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"ProjectName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"RunID\":{\"type\":\"string\"},\"VirtualUsers\":{\"type\":\"string\"},\"Release\":{\"type\":\"string\"},\"BuildNumber\":{\"type\":\"string\"},\"StartTime\":{\"type\":\"string\"},\"EndTime\":{\"type\":\"string\"},\"StdDeviation\":{\"type\":\"string\"},\"TestName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"TransactionName\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"Baseline\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"SLAviolationcount\":{\"type\":\"float\"}}}}}
how would i make this work for elasticsearch version 5.0. Kindly help me with the index structure and search query for 5.0. That would be really helpful.Thanks in advance.

Related

Alert dialog before a new thread

I have the code.
After clicking on the button, an alertdialog of the download warning should appear. And request a json file from the server.
btnEnter = findViewById(R.id.btn_enter)
btnEnter.setOnClickListener {
val gson = Gson().toJson(authorization)
val message = gson.toByteArray(StandardCharsets.UTF_8)
val url = URL("my URL")
val responseServer: String?
val progressBar = AlertDialog.Builder(this)
progressBar.setCancelable(false)
progressBar.setView(R.layout.progress_bar)
dialog = progressBar.create()
dialog.show()
if (isOnline(this)) {
responseServer = sendJson(url, message)
if (responseServer != null){
responseAuthorization = Gson().fromJson(responseServer, ResponseAuthorization::class.java)
}else{
Toast.makeText(applicationContext, "Error", Toast.LENGTH_SHORT).show()
val vibration = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vibration.vibrate(VibrationEffect.createOneShot(200, VibrationEffect.DEFAULT_AMPLITUDE))
}
} else {
AlertDialog.Builder(this)
.setTitle("No connection")
.setMessage("No connection")
.setPositiveButton("OK"){ _, _ -> finish() }.show()
}
}
and
private fun sendJson(url: URL, message: ByteArray): String?{
var str: String? = null
val t1 = Thread(Runnable {
try {
with(url.openConnection() as HttpURLConnection) {
doInput = true
doOutput = true
requestMethod = "POST"
setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded"
)
setRequestProperty("Accept", "application/json")
DataOutputStream(outputStream).write(message)
if (inputStream != null) {
str = BufferedReader(InputStreamReader(inputStream)).use {
it.readText()
}
}
}
} catch (ex: Exception) {
str = null
println(ex)
}
})
t1.start()
t1.join()
return str
}
Alert dialog appears after the thread is executed. How to make it appear before the thread

Nexus3 only downloads 50 in list

So I have a code that downloads the version nr of each nuget package but it all stops after 50 in list.
I use jenkins with groovy code and get out a list of versions.
import groovy.json.JsonSlurperClassic
import groovy.json.JsonBuilder
import wslite.rest.*
def data = new URL("http://nexus.xx.xx.se:8081/service/rest/v1/search?repository=xx-sx-nuget&name=XXXFrontend").getText()
println data
/**
* 'jsonString' is the input json you have shown
* parse it and store it in collection
*/
Map convertedJSONMap = new JsonSlurperClassic().parseText(data)
//If you have the nodes then fetch the first one only
if(convertedJSONMap."items"){
println "Version : " + convertedJSONMap."items"[0]."version"
}
def list = convertedJSONMap.items.version
Collections.sort(list)
list
So the problem is that it only get 50 of the versions. How can I get more than 50? I have read about a continuetoken but I dont understand how to use that?
UPDATE
I have added this but still dont work
while(convertedJSONMap."continuesToken" != null){
def token = convertedJSONMap."continuationToken"
def data2 = new URL("http://nexus.xxx.xxx.se:8081/service/rest/v1/search?repository=xxx-xx-nuget&name=xxxxxx&continuationToken=" +token).getText()
convertedJSONMap = JsonSlurperClassic().parseText(data2)
}
This is how I solved it for me. It is just a snippet of the code that I use
def json = sendRequest(url)
addResultToMap(map2, json, release) //I do something here with the received result
def continuationToken = json.continuationToken
if (continuationToken != null) {
while (continuationToken != null) {
json = sendRequest(url + "&continuationToken=" + continuationToken)
addResultToMap(map2, json, release) //I do something here with the received result as above
continuationToken = json.continuationToken
}
}
And my sendRequest method looks like this
def sendRequest(def url, String method = "GET") {
String userPass = "${nexus.username}:${nexus.password}"
String basicAuth = "Basic " + "${printBase64Binary(userPass.getBytes())}"
def connection = new URL( url ).openConnection() as HttpURLConnection
connection.setRequestProperty('Accept', 'application/json' )
connection.setRequestProperty('Authorization', basicAuth)
connection.setRequestMethod(method)
try {
if ( connection.responseCode <= 299 ) {
if (connection.responseCode == 200) {
return connection.inputStream.withCloseable { inStream -> new JsonSlurper().parse( inStream as InputStream ) }
}
} else {
displayAndLogError(connection.responseCode + ": " + connection.inputStream.text, loglevel.DEBUG)
}
} catch(Exception exc) {
displayAndLogError(exc.getMessage())
}
}
Here is an alternative :
import groovy.json.JsonSlurper
try {
N_PAGES_MAX = 10
List<String> versions = new ArrayList<String>()
continuationToken = "123"
artifactsUrl = "http://nexus.zzz.local/service/rest/v1/components?repository=releases-super-project"
currentPage = 1
while (true) {
artifactsObjectRaw = ["curl", "-s", "-H", "accept: application/json", "-k", "--url", "${artifactsUrl}"].execute().text
artifactsJsonObject = (new JsonSlurper()).parseText(artifactsObjectRaw)
continuationToken = artifactsJsonObject.continuationToken
if (continuationToken!=null && continuationToken!='123') {
artifactsUrl = artifactsUrl + "&continuationToken=$continuationToken"
}
def items = artifactsJsonObject.items
for(item in items){
versions.add(item.name)
}
currentPage += 1
if (continuationToken==null || currentPage>N_PAGES_MAX) break
}
return versions.sort().reverse()
}
catch (Exception e) {
print "There was a problem fetching the versions"
}

Nifi: How to make write and check operations in file in one thread (processor)?

How to make only one point (Processor or Service) that will write the file and make it working in single thread, in my case i have workflow like this executescript1(single thread processor with write operations)->updateAttribute->InvokeHttpProcessor->executescript1(single thread processor with check operations (it is the first processor)) i have tried the code below but it nor fulfills sucessfully neither trows exception,
WHAT SHOULD I CHANGE?
here is my code:
File file = new File("C:/Users/Desktop/test/conf.xml");
String content = "";
BufferedReader s;
BufferedWriter w;
RandomAccessFile ini= new RandomAccessFile(file, "rwd");
FileLock lock= ini.getChannel().lock();
try {
def flowFile=session.get();
if(flowFile==null){
String sCurrentLine;
s = new BufferedReader(Channels.newReader(ini.getChannel(), "UTF-8"));
while ((sCurrentLine = s.readLine()) != null) {
content += sCurrentLine;
}
ini.seek(0);
def flowFile1=session.create()
flowFile1 = session.putAttribute(flowFile1, "filename", "conf.xml");
session.write(flowFile1, new StreamCallback() {
#Override
public void process(InputStream inputStream1, OutputStream outputStream) throws IOException {
outputStream.write(content.getBytes(StandardCharsets.UTF_8))
}
});
session.transfer(flowFile1,REL_SUCCESS);
def xml = new XmlParser().parseText(content);
xml.'**'.findAll{it.name() == 'run'}.each{ it.replaceBody 'false'}
def newxml=XmlUtil.serialize(xml);
String data =newxml;
if (!data.isEmpty()) {
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
w.write(data);
lock.release();
w.close();
}
}
else{
def serviceName=flowFile.getAttribute('serviceName');
def date=flowFile.getAttribute('filename').substring(0,10);
if(serviceName=='Decl'){
def xml = new XmlParser().parseText(content)
for(int i=0;i<names.size();i++) {
date = names.get(i).substring(0, 10);
xml.RS.Decl.details.findAll({ p ->
p.runAs[0].text() == "false" && p.start[0].text() == date.toString()
}).each({ p ->
p.start[0].value = addDays( p.start[0].text())
p.runAs[0].value = "true"
})
}
def newXml= groovy.xml.XmlUtil.serialize( xml )
data = newXml.toString()
if (!data.isEmpty()) {
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
w.write(data);
lock.release();
w.close();
}
}
else if(serviceName=='TaxyFee'){
def xml = new XmlParser().parseText(content)
for(int i=0;i<names.size();i++) {
date = names.get(i).substring(0, 10);
xml.RS.TaxyFee.details.findAll({ p ->
p.runAs[0].text() == "false" && p.start[0].text() == date.toString()
}).each({ p ->
p.start[0].value = addDays( p.start[0].text())
p.runAs[0].value = "true"
})
}
def newXml= groovy.xml.XmlUtil.serialize( xml )
data = newXml.toString()
if (!data.isEmpty()) {
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
w.write(data);
lock.release();
w.close();
}
}
}
} catch (FileNotFoundException e) {
//e.printStackTrace();
TimeUnit.SECONDS.sleep(50000);
} catch (IOException e) {
e.printStackTrace();
} catch(OverlappingFileLockException e){ TimeUnit.SECONDS.sleep(50000);
lock.release();
} catch (Exception e) {
e.printStackTrace();
} finally {
//lock.release();
ini.close();
}
set on the "scheduling" tab of the processor properties Concurrent Tasks = 1.
so only one instance of the processor will be running at the same time.
https://nifi.apache.org/docs/nifi-docs/html/user-guide.html#scheduling-tab

Use Groovy To Search A File And Grab Contents Below A String

I'm trying to use Groovy to open a file to search for a specific substring and then grab a different substring that occurs below the first one.
For example the substring I'm searching for is "Charger is enabled. Checking charge parameters..."
and if it is found I want to get a specific string that occurs after this.
Is the best way to do this read the file into memory and search for the index of the first string?
// With Java
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
def localDirectory = "";
def fileName = "";
def searchKey = "Charger is enabled. Checking charge parameters";
def searchKey2 = "";
def errorMessage = "";
FileReader fr;
BufferedReader br;
try
{
File f1 = new File(localDirectory+"/"+fileName);
fr = new FileReader(f1);
br = new BufferedReader(fr);
def keyFound = false;
// Go through line by line.
def line;
while ((line = br.readLine()) != null)
{
// If first string is found, process the second string.
if(line.contains(searchKey))
{
while ((line = br.readLine()) != null)
{
// Do something with the second string.
if(line.contains(searchKey2))
{
keyFound=true;
break;
}
}
}
if(keyFound)
{
break;
}
}
}
catch (Exception e)
{
errorMessage += "\nUnexpected Exception: " + e.getMessage();
for (trace in e.getStackTrace())
{
errorMessage += "\n\t" + trace;
}
}
finally
{
fr?.close();
br?.close();
}
// With Groovy
def localDirectory = "";
def fileName = "";
def searchKey = "Charger is enabled. Checking charge parameters";
def searchKey2 = "";
def errorMessage = "";
try
{
File f1 = new File(localDirectory+"/"+fileName);
def keyFound = false;
// Go through line by line.
def line;
f1.withReader
{
reader ->
while((line = reader.readLine()) != null)
{
// If first string is found, process the second string.
if(line.contains(searchKey))
{
while((line = reader.readLine()) != null)
{
// Do something with the second string.
if(line.contains(searchKey2))
{
keyFound=true;
break;
}
}
}
if(keyFound)
{
break;
}
}
}
}
catch (Exception e)
{
errorMessage += "\nUnexpected Exception: " + e.getMessage();
for (trace in e.getStackTrace())
{
errorMessage += "\n\t" + trace;
}
}

SharePoint SPWeb.GetFile method with using server url and not using it at all

I notice that when we use SPWeb.GetFile method, we can pass the whole URL or only the part of the url.
Let's say my file exists in servername/sites/SiteA/DocumentLibrary/Folder/file.txt.
(i omit http)
SiteA = servername/sites/SiteA
using (SPWeb oWebsiteFrom = new SPSite(SiteA).OpenWeb())
{
SPFile oSrcSPFile = oWebsiteFrom.GetFile(ServerURL + "/" + DocLibrary+ "/" + Folder + "/" + fileName);
}
This one is also OK to use without ServerURL in GetFile.
using (SPWeb oWebsiteFrom = new SPSite(SiteA).OpenWeb())
{
SPFile oSrcSPFile = oWebsiteFrom.GetFile(DocLibrary+ "/" + Folder + "/" + fileName);
}
What is the difference between using serverURL and not using serverURL in GetFile method?
Basically it is the same.
Lot of SharePoint methods(not all) using Url as parameter will call an internal method named "GetWebRelativeUrlFromUrl" to handle the Url
And below are the code of the methods invoked when you call SPWeb.GetFile
As you can see, the string will parse as UriScheme object and if your string is a ServerRelative uriScheme, the method will "convert" it to absolute url.
internal SPFile GetFile(string strUrl, byte iLevel)
{
string webRelativeUrlFromUrl = this.GetWebRelativeUrlFromUrl(strUrl);
if (webRelativeUrlFromUrl.Length == 0)
{
throw new ArgumentException();
}
return new SPFile(this, webRelativeUrlFromUrl, iLevel);
}
internal string GetWebRelativeUrlFromUrl(string strUrl)
{
return this.GetWebRelativeUrlFromUrl(strUrl, true, true);
}
internal string GetWebRelativeUrlFromUrl(string strUrl, bool includeQueryString, bool canonicalizeUrl)
{
string str;
char[] chrArray;
if (strUrl == null)
{
throw new ArgumentNullException();
}
if (strUrl.Length == 0)
{
return strUrl;
}
if (canonicalizeUrl || !includeQueryString)
{
strUrl = Utility.CanonicalizeFullOrRelativeUrl(strUrl, includeQueryString, out flag);
canonicalizeUrl = 0;
includeQueryString = 1;
}
UriScheme uriScheme = SPWeb.GetUriScheme(strUrl);
if (uriScheme == UriScheme.ServerRelative)
{
string serverRelativeUrl = this.ServerRelativeUrl;
if (!SPUtility.StsStartsWith(strUrl, serverRelativeUrl))
{
throw new ArgumentException();
}
str = strUrl.Substring(serverRelativeUrl.Length);
if (str.Length > 0)
{
if (str.get_Chars(0) == 47)
{
return str.Substring(1);
}
if (uriScheme == UriScheme.Http || uriScheme == UriScheme.Https)
{
if (uriScheme == UriScheme.Http && strUrl.Contains(":80/"))
{
strUrl = strUrl.Remove(strUrl.IndexOf(":80/"), ":80/".Length - 1);
}
bool flag2 = false;
if (!SPUtility.StsStartsWith(strUrl, this.Url))
{
using (SPSite sPSite = new SPSite(strUrl))
{
if (sPSite.ID != this.Site.ID)
{
throw new ArgumentException();
}
UriBuilder uriBuilder = new UriBuilder(new Uri(strUrl));
uriBuilder.Scheme = sPSite.Protocol.TrimEnd(new char[] { 58 });
uriBuilder.Host = sPSite.HostName;
uriBuilder.Port = sPSite.Port;
strUrl = uriBuilder.Uri.ToString();
}
flag2 = SPUtility.StsStartsWith(strUrl, this.Url);
}
else
{
flag2 = true;
}
if (flag2)
{
str = strUrl.Substring(this.Url.Length);
if (str.Length > 0)
{
if (str.get_Chars(0) == 47)
{
return str.Substring(1);
}
throw new ArgumentException();
if (!strUrl.StartsWith("_"))
{
bool flag3 = true;
try
{
if (false || -1 == strUrl.IndexOf(58) || null != new Uri(strUrl))
{
flag3 = false;
}
}
catch
{
}
if (!flag3)
{
throw new ArgumentException();
}
}
str = strUrl;
}
}
}
}
}
return str;
}

Resources