The challenge of setting a property in a Struct (specifically in Nashorn, Kafka Connect transformer) resulting in a TypeError

I am currently using Kafka Connect version 6.1.1 and attempting to utilize Sergey34's kafka-connect-transformers library to modify my Kafka messages before sending them to BigQuery via the BigQuerySink.

Within my connector.properties file, I have set up the ScriptEngineTransformer configuration in the following manner (example shortened for clarity):

transforms=scriptenginetransformer
transforms.scriptenginetransformer.type=seko.kafka.connect.transformer.script.ScriptEngineTransformer
transforms.scriptenginetransformer.scrip_engine_name=javascript
transforms.scriptenginetransformer.value.script=function valueTransform(source){ source.foo = 42; return source;}

During runtime, I encounter the error message:

javax.script.ScriptException: TypeError: Cannot set property "foo" of Struct{a=111,b=222} in <eval> at line number 2

(full stack trace can be found here)

Based on my basic understanding of JavaScript, it seems like modifying or adding properties to a struct should be possible, as demonstrated in this simple sandbox example:

function Foo(x, y) {
    this.x = x;
    this.y = y;
}

foo = new Foo(1, 2);
foo.y = 3;
foo.z = 4;

console.log(foo);

The fact that my Avro-formatted Kafka message ({a=111,b=222}) is successfully passed to the JS script leads me to wonder what could be causing this particular error.

Answer №1

In case you find yourself in need of adding a static field, Kafka offers a convenient built-in transform that can handle this task...

Upon reviewing your problem, it appears that the code does not deal with records containing schemas and does not create a new Struct type

As a result, it seems that your input is restricted to primitive schema types like string/integer/boolean

To illustrate, when attempting to merge "Struct{a=111,b=222}" with "foo", everything may seem to blend smoothly resulting in

"Struct{a=111,b=222}foo"
. However, the Avro record's string representation "Struct{a=111,b=222}" lacks a Javascript property called foo, thus rendering it unmodifiable.

An alternative approach would be to consume data using the standard JSONConverter and then utilize JSON.parse to construct an object where you can freely set JavaScript properties.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Skipping the configuration file during the process of converting Node.js files to an executable using pkg

I am currently working on a node.js application and in order to prevent users from reading the code, I am attempting to convert my JavaScript files to executable files. I have been using the pkg module for this task, but I have encountered an issue. The p ...

Adjust the width of the table to scroll horizontally and size down to fit within the

My webpage is structured with a sidebar and content section, using flex display with the sidebar set to 0.15 flex and the content set to 0.85 flex. I want this page to be full width of the viewport. The issue arises when I try to incorporate a table into ...

Link various data to various text boxes using a common ngModel property in Angular 8

In my project, I am working on creating a time-picker that will open when the user focuses on a text-box. The challenge I'm encountering is that although there are multiple text-boxes on a single page, binding the selected value from the time-picker u ...

Mastering the art of passing props in VueJS

My setup in the App is quite straightforward, with two main components: The HelloWorld component and a dialog component. The dialog component receives props from the HelloWorld component. The data in the HelloWorld component is rendered by looping over an ...

Leveraging Deferred in conjunction with AJAX

I am facing an issue with a series of JavaScript functions that make database calls using AJAX. I need these functions to finish executing and update variables before moving on to the final function. I have attempted to use jQuery $.when but it is not work ...

PHP script automatically resets select option on page load

Consider the following scenario: <form name="limit"> <select name="limiter" onChange="limit(this.value)"> <option selected="selected">&nbsp;</option> <option value="5">5</option> <opti ...

Tips for designing a masonry grid with Bootstrap 4

I'm attempting to achieve a specific layout using Bootstrap 4, JavaScript, CSS, and HTML. I have not been able to find something similar on Stack Overflow, but I did come across the Bootstrap 4 Cards documentation. However, I am unsure if this is the ...

What is the recommended TypeScript type for setting React children?

My current layout is as follows: export default function Layout(children:any) { return ( <div className={`${styles.FixedBody} bg-gray-200`}> <main className={styles.FixedMain}> <LoginButton /> { children } ...

The MongoDB operator "$in" does not recognize the type array that currently exists in the field

When attempting to execute an aggregate query in mongodb, I encountered an issue with the "$in" operator stating that the field passed is not recognized as an array. Here is the aggregate query being used: [ { '$match': { 'isVis ...

Geocomplete Plugin without Google Branding

I've implemented the jQuery Geocomplete Library. This is what I have accomplished so far- $(function() { $("#find_product_location").geocomplete( { map: "#product_location", mapOptions: { mapTypeId : 'roadmap',//roadmap, satellite,hybrid ...

Calculate the result by multiplying each row value with the value provided as input

Check out this HTML code snippet <form><input type="number"></form> <table class="my-table"> <thead> <tr> <td>Header 1</td> <td>Header 2</td> </tr> </thead> &l ...

Sending JSON data containing an IFormFile and a string as parameters to C#

Software Versions: ASP.NET and Web Tools - 17.10.341.11210 C# Tools - 4.10.0-3.24312.19+ JQuery - 3.3.1.js JS - 2.8.3.js Currently, I am attempting to pass an IFormFile and a string from a JSON file select and a string input. After thorough testing, I ha ...

How to iterate through an array using Google Apps Scripts

I've been working on creating a loop for an array, but I keep encountering an error indicating that the string is invalid. Suppose the keyword is "mesothelioma|seo". function json(keyword) { var jsondata = UrlFetchApp.fetch("http://api.grepwords.c ...

Running the test suite in another tab is causing it to fail

I am experiencing an unusual issue in my protractor UI test. During one of the tests, I need to click on a link that opens in a new tab. The test passes when run individually, but fails when run as part of the test suite. I would appreciate it if you coul ...

Enhance your bookmarking experience with Vue mixins that allow you to easily customize the color

Looking for a way to efficiently bookmark pages in my application, I have successfully implemented a feature where I can add pages by their name and URL into bookmarks. Upon clicking the bookmark button, it changes color to indicate that the page has been ...

How to Test React Js API Calls with Jest?

I'm currently in the process of writing test cases for my API call function, but I'm encountering difficulties as I am unable to successfully run my tests. Below is the code for the API call function and the corresponding test cases. export async ...

SQL message comparison error within mysql module is malfunctioning

Currently, I am utilizing the nodejs MySQL module to automatically create a table every month. To ensure this process runs smoothly, I need to check if the table already exists using the condition err.sqlMessage == Table 'tableName' alre ...

What causes the inversion of height and width settings in react-webcam?

Currently utilizing react-webcam with the following configuration. <Webcam audio={false} screenshotFormat="image/jpeg" videoConstraints={{ facingMode: "environment", width: camera ...

Electron fails to display images in the compiled version

I'm currently troubleshooting an issue related to displaying images using CSS in my electron project. In the latest version of the application, the images are not showing up when linked from a CSS file. However, in a previous version, the images disp ...

Is it possible to integrate Vue.js within a web component?

Is it possible to utilize VueJS to control behavior within a web component? In other words, if the VueJS library is included as a script reference, can it be integrated in the same way as on a standard HTML page, but within the confines of a web componen ...