[enter image description here][1]I have nifi 1.3.0. Is it possible that groovy version which is implemented in nifi can't recognize lambdas? Here is my code, how can I change it to make work inside nifi processor?
is it possible that processor doesn;t work properly because i don't use session.tranfer i mean don't produce any flowfile?
def static addDays(def date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, 1); // number of days to add
String m = sdf.format(c.getTime()); // dt is now the new date
return m;
}
def flowFile=session.get()
ArrayList<String> value = new ArrayList<>();
if(flowFile!=null){
value.add(flowFile.getAttribute('filename');
session.remove(flowFile);
}
File file = new File("C://Users//user//Desktop//try2//nifi-1.3.0//4//conf2.xml");
String content = "";
String material = "";
BufferedReader s;
BufferedWriter w;
RandomAccessFile ini = new RandomAccessFile(file, "rwd");
FileLock lock = ini.getChannel().lock();
DocumentBuilder dBuilder;
Document document, doc;
String date="",data="";
try {
String sCurrentLine;
s = new BufferedReader(Channels.newReader(ini.getChannel(), "UTF-8"));
while ((sCurrentLine = s.readLine()) != null) {
content += sCurrentLine;
}
ini.seek(0);
def xml = new XmlParser().parseText(content)
for(int i=0;i<value.size();i++) {
date = value.get(i).substring(0, 10);
xml.rs.borderCross.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 stringWriter = new StringWriter()
new XmlNodePrinter(new PrintWriter(stringWriter)).print(xml)
def newXml = stringWriter.toString()
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
InputStream stream1 = new ByteArrayInputStream(newXml.toString().getBytes("UTF-8"));
DocumentBuilderFactory dbFactory1 = DocumentBuilderFactory.newInstance();
dBuilder = dbFactory1.newDocumentBuilder();
document = dBuilder.parse(stream1);
DOMSource source=new DOMSource(document);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamResult result1 = new StreamResult(bos);
try {
transformer.transform(source, result1);
} catch (TransformerException e) {
e.printStackTrace();
}
byte[] array = bos.toByteArray();
data = bos.toString();
if (!data.isEmpty()) {
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
w.write(data);
lock.release();
w.close();
}
else{
ini.setLength(0);
w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
w.write(content);
lock.release();
w.close();
}
}catch (FileNotFoundException e) {
TimeUnit.SECONDS.sleep(50000);
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (OverlappingFileLockException e) {
TimeUnit.SECONDS.sleep(50000);
lock.release(); ;
} catch (Exception e) {
e.printStackTrace();
} finally {
//lock.release();
ini.close();
}
Those aren't Java lambdas, they are Groovy closures. At first glance that looks correct, what error(s) are you getting? You may need to put the closures in parentheses before chaining them:
def xml = new XmlParser().parseText(content)
for(int i=0;i<value.size();i++) {
date = value.get(i).substring(0, 10);
xml.rs.borderCross.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"
})
}
Related
when I try copy a java file to txt file ,It works fine but it prints some additional lating character at end of output file(Note: there is no such latin character in input file).
please refer the code
import java.io.*;
public class File1 {
public static void main(String[] args) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
int c;
String input = "F:\\Java Intellij\\src\\testprint.java";
String output = "F:\\Java Intellij\\src\\new23.txt";
File infile = new File(input);
File outfile = new File(output);
infile.createNewFile();
outfile.createNewFile();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
fin = new FileInputStream(infile);
fout = new FileOutputStream(outfile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("enter :");
if (fin != null && fout != null)
try {
do {
c = fin.read();
fout.write(c);
} while (c != -1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
fin.close();
}
if (fout != null) {
fout.close();
}
}
}
}
can anyone shred some light on the question, why this happens??? and suggest a remedy
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
I want to remove session in case i get certain data from file i have code like this,but i got errors "flowfile has already marked for removal", what should i change to get rid of extra errors?
In case of session rollback flowfile will dissapear in queues also?
2.should i use rollback instead of remove()?
NodeList childNodes = nodeGettingChanged.getChildNodes();
for (int i = 0; i != childNodes.getLength(); ++i) {
Node child = childNodes.item(i);
if (!(child instanceof Element))
continue;
if (child.getNodeName().equals("runAs")) {
if(child.getFirstChild().getTextContent()=="false"){
session.remove(flowFile1);
File deleteExtraFile =new File("C://Users//s.tkhilaishvili//Desktop//try2//nifi-1.3.0//1//conf.xml");
boolean delete=deleteExtraFile.delete();
}
else {
child.getFirstChild().setNodeValue("false");
}
}
}
Document finalXmlDocument = xmlDocument;
session.write(flowFile1, new StreamCallback() {
public void process(InputStream inputStream, OutputStream outputStream) throws IOException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
DOMSource source = new DOMSource(finalXmlDocument);
ffStream.close();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bos);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
byte[] array = bos.toByteArray();
outputStream.write(array);
}
});
session.remove(flowFile);
session.transfer(flowFile1, REL_SUCCESS);
}
If you are executing session.remove(flowFile1) then later trying to transfer it to REL_SUCCESS, you will get that error. It looks like you already have an if-clause checking the firstChild for "false", perhaps you could put the transfer in an else-clause, such that it will only be transferred if it wasn't removed.
As shown in the title, I read owl file which is generated by Protege to Jena, modified it by adding some NamedIndividuals, and I wanted to read the modified file by Protege. Things went on well until I open this owl file with Protege. Protege just can not read it!
I tried every "RDF/XML", "RDF/XML - ABBREV", "N - TRIPLE", "TTL", and still nothing.
One good news is that when I use "RDF/XML - ABBREV" and delete all the NamedIndividual I added, Protege works.
But I want my Individuals!!!
public class Pizza00 {
public static void main(String[] args) throws IOException{
String SOURCE = "http://www.seaice.com/ontologies/seaice.owl";
String NS = SOURCE + "#";
OntModel m = ModelFactory.createOntologyModel();
try {
m.read(new FileInputStream("G:/Protege/owl files/SeaIce.owl"), null);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
reason rec = new reason();
rec = readFileByLines("G:/data/seaice_property.txt");
int i;
OntClass tmp = m.getOntClass(NS + "SeaIceProperty");
for(i = 1; i <= rec.line - 1; i ++){
m.createIndividual(NS + rec.s[i], tmp);
}
m.write(System.out);
OutputStream out = new FileOutputStream("G:/Protege/owl files/SeaIce - by jena.owl");
m.write(out, "RDF/XML-ABBREV", null);
out.close();
}
static class reason{
String[] s= new String[1000];
int line;
}
public static reason readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
reason x = new reason();
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
x.s[x.line] = tempString;
x.line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return x;
}
}
As you mention in the comments, the error is
this line <j.0:SeaIceProperty
rdf:about="seaice.com/ontologies/seaice.owl#Optical Band Imagery"/>
error occurs.
And the error by Protege is
org.semanticweb.owlapi.rdf.syntax.RDFParserException:
[line=30:column=101] IRI 'seaice.com/ontologies/seaice.owl#Optical
Band Imagery' cannot be resolved against curent base IRI
file:/G:/Protege/owl%20files/SeaIce%20-%20by%20jena.owl
The not-quite IRI seaice.com/ontologies/seaice.owl#Optical Band Imagery is relative, and can't be resolved against the current base IRI, which is a file IRI: file:/G:/Protege/owl%20files/SeaIce%20-%20by%20jena.owl. The easiest way to fix this is to use absolute IRIs when you are creating your named individuals.
I am using the following code to retrieve the documents in a list. Its working fine. However, it only returns documents and folders in root of the doc library. Is there any thing wrong I am doing here? I am looking for files in sub folders with recursive mode.
Service service = new Service();
service.setMaintainSession(true);
call = (Call) service.createCall();
call.setTargetEndpointAddress( new java.net.URL("<host>/_vti_bin/lists.asmx") );
call.setOperationName(new QName("http://schemas.microsoft.com/sharepoint/soap/","GetListItems"));
call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean("true"));
call.setProperty(Call.SOAPACTION_URI_PROPERTY,"http://schemas.microsoft.com/sharepoint/soap/GetListItems");
call.addParameter(new javax.xml.namespace.QName("http://schemas.microsoft.com/sharepoint/soap/", "listName"),
new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"),
java.lang.String.class,
javax.xml.rpc.ParameterMode.IN);
MessageElement me =
new MessageElement(new QName("QueryOptions"));
me.addChildElement(new MessageElement(new QName(
"IncludeMandatoryColumns"))).addTextNode("true");
me.addChildElement(new MessageElement(new QName(
"ViewAttributes"))).addAttribute(javax.xml.soap.SOAPFactory.newInstance().createName("Scope"), "Recursive");
MessageElement[] me1 = {me};
String strMyString = ""
+ "<Query>"
+ "<OrderBy><FieldRef Name=\"ows_Modified\" Ascending=\"TRUE\" /></OrderBy>"
+ "</Query>";
MessageElement[] meArray = { getMeFromString(strMyString) };// Array
call.addParameter("query",org.apache.axis.Constants.XSD_SCHEMA,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("queryOptions",org.apache.axis.Constants.XSD_SCHEMA,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(org.apache.axis.Constants.XSD_SCHEMA);
Schema ret = (Schema)call.invoke(new Object[] {"listGUID",meArray, me1 });
public org.apache.axis.message.MessageElement getMeFromString(final String strMyString) {
DocumentBuilder docBuilder = null;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (final ParserConfigurationException e) {
e.printStackTrace();
} catch (final FactoryConfigurationError e) {
e.printStackTrace();
}
final StringReader reader = new StringReader(strMyString);
final InputSource inputsource = new InputSource(reader);
Document doc = null;
try {
doc = docBuilder.parse(inputsource);
} catch (final SAXException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
}
final Element ele = doc.getDocumentElement();
final MessageElement msg = new MessageElement(ele);
return msg;
}
query.ViewAttributes = "Scope='RecursiveAll'"