DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Generating Millions of Objects with Random Values [Snippet]

Generating Millions of Objects with Random Values [Snippet]

When it comes to indexing with Solr, Java's Reflection API might be the solution to your problem when you need to generate objects and marshal them.

Abhishek Shah user avatar by
Abhishek Shah
·
May. 20, 17 · Code Snippet
Like (2)
Save
Tweet
Share
9.40K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous article, I mentioned using Apache Flume and Apache Solr together. While working with Solr Cloud, I felt the need to test the Solr indexing with distributed nodes.

So, I created 4 Solr nodes, with 4 shards and 2 replicas each. My aim was very simple: run a Solr index on 100k XML files every 15 minutes. So, to test this, I needed 100,000 XML files every 15 minutes to run my indexing.

The problem was how to generate so many Objects and marshal them. Then a very simple idea struck my mind, which many of you might have already tried at least once in your life.

Yes, I used the Java Reflection API. And it solved my problem in a minute (actually five minutes — it took a little time to debug). So here is the code snippet from the method, 

public static Object generateRandomObjects(Class clazz) throws InstantiationException, IllegalAccessException,
    IllegalArgumentException, InvocationTargetException, ClassNotFoundException {

        Object obj = clazz.newInstance();

        for (Method method: clazz.getMethods()) {
            if (method.getName().startsWith("set")) {
                for (Type subType: method.getGenericParameterTypes()) {
                    Class parameterClass = Class.forName(subType.getTypeName());
                    if (parameterClass.isAssignableFrom(String.class)) {
                        method.setAccessible(true);
                        method.invoke(obj, getRandomValues());
                    } else if (parameterClass.isAssignableFrom(Date.class)) {
                        method.setAccessible(true);
                        method.invoke(obj, new Date());
                    } else {
                        method.invoke(obj, generateRandomObjects(parameterClass));
                    }
                }
            }

        }
        return obj;
    }


Just pass in the Class of which you want to create an object, and this method will return a completely filled object. I know this method can be optimized and also tweaked, but you get the idea, right? 

I am currently working with Solr indexing on large volume sets, and my next article will definitely be about Solr indexing.

Object (computer science) Snippet (programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Steps for Getting Started in Deep Learning
  • Introduction to Container Orchestration
  • Simulating and Troubleshooting BLOCKED Threads in Kotlin [Video]
  • 3 Main Pillars in ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: