Tag Archives: Properties

[Solved] java.nio.file.NoSuchFileException: XYZ

NoSuchFileException occurred when trying to attempt access a file or directory which is not exist in given location.

NoSuchFileException is sub class of FileSystemException. This exception  introduced in Java 7.

Example for NoSuchFileException

This example is throwing NoSuchFileException because trying to access a file student_data.json which is not exist on default location.

package com.fiot.json.jackson;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fiot.json.jackson.pojo.Student;

public class ConvertJsonToArrayList {

	public static void main(String[] args) {
		try
		{
		byte[] mapData = Files.readAllBytes(Paths.get("student_data.json"));
		Student[] studentArr = null;

		ObjectMapper objectMapper = new ObjectMapper();
		studentArr = objectMapper.readValue(mapData, Student[].class);
		List studentList=Arrays.asList(studentArr);
		System.out.println("Student 1 \n"+studentList.get(0));
		System.out.println("Student 2 \n"+studentList.get(1));

		}
		catch(JsonMappingException ex)
		{
			ex.printStackTrace();
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}

	}

}

Exception Stacktrace


java.nio.file.NoSuchFileException: student_data.json
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.newByteChannel(Unknown Source)
    at java.nio.file.Files.readAllBytes(Unknown Source)
    at com.fiot.json.jackson.ConvertJsonToArrayList.main(ConvertJsonToArrayList.java:18)

Solutions

There are couple of solutions to handle such situations:

  1. Use fully qualified file path such as “C:/data/student_data.json” but that will help only when your program is going to run on local machine. While working with enterprise application always use relative paths.
  2. If your file in source directory use path as “src/student_data.json”
  3. If your project is maven project, always write script to copy file which you want to access or keep in resource folder to access because maven project create different directory structure after build.

Solution depend on your project requirement. You can use below code to get qualified full path of a file

public static void main(String [] args) {

    String filename="student_data.json";
    //To get path of file
    Path path = Paths.get(filename);
    //To print absolute path of file
    System.out.println(path.getAbsolutePath());

You would like to see

Follow below link to see more Java issues solutions

Kafka Server Properties Configuration

Kafka provide server level properties for configuration of  Broker, Socket, Zookeeper, Buffering, Retention etc.

broker.id :  This broker id which is unique integer value in Kafka cluster.

broker.id:0

Socket Server Settings :

listeners: default value is PLAINTEXT://:9092  where socket servers listens and if not configured will take from java.net.InetAddress.getCanonicalHostName()

Format: security_protocol://host_name:port

listeners:PLAINTEXT://:9092

advertised.listeners: Need to set this value if listeners value is not set. Broker will advertise this listener value to producers and consumers.

Example: PLAINTEXT://your.host.name:9092

advertised.listeners:PLAINTEXT://:9092

num.network.threads: Threads handling network requests.

num.network.threads:3

num.io.threads: Number of threads handling I/O for disk.

num.io.threads:8

socket.send.buffer.bytes: Buffer size used by socket server to keep records for sending.

socket.send.buffer.bytes:102400

socket.receive.buffer.bytes: Buffer size used by socket server to keep records for sending.

socket.receive.buffer.bytes:102400

socket.request.max.bytes: max size of request that the socket server will accept.

socket.request.max.bytes:104857600

Log Basics

log.dirs: a comma separated list of directories under which to store log files.

log.dirs=/tmp/kafka-logs

num.partitions: The default number of logs per topic. More partitions allow greater parallelism for consumption, but this will also result in more files across the brokers.

num.partitions=1

num.recovery.threads.per.data.dir: The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.This value is recommended to be increased for installations with data dirs located in RAID array.

num.recovery.threads.per.data.dir=1

Log Flush Policy

log.flush.interval.messages:  Messages are immediately written to the file system but by default we only fsync() to sync the OS cache lazily. The following configurations control the flush of data to disk.There are a few important trade-offs here:

  1. Durability: Unflushed data may be lost if you are not using replication.
  2. Latency: Very large flush intervals may lead to latency spikes when the flush does occur as there will be a lot of data to flush.
  3.  Throughput: The flush is generally the most expensive operation, and a small flush interval may lead to excessive seeks.

The settings below allow one to configure the flush policy to flush data after a period of time or every N messages (or both). This can be done globally and overridden on a per-topic basis.

The number of messages to accept before forcing a flush of data to disk.

log.flush.interval.messages=10000

The maximum amount of time a message can sit in a log before we force a flush.

log.flush.interval.ms=1000

Log Retention Policy

The following configurations control the disposal of log segments. The policy can be set to delete segments after a period of time, or after a given size has accumulated. A segment will be deleted whenever either of these criteria are met. Deletion always happens from the end of the log.

log.retention.hours:The minimum age of a log file to be eligible for deletion

log.retention.hours=168

log.retention.bytes:A size-based retention policy for logs. Segments are pruned from the log as long as the remaining segments don’t drop below log.retention.bytes.

log.retention.bytes=1073741824

log.segment.bytes:The maximum size of a log segment file. When this size is reached a new log segment will be created.

log.segment.bytes=1073741824

log.retention.check.interval.ms: The interval at which log segments are checked to see if they can be deleted according to the retention policies

log.retention.check.interval.ms=300000

Zookeeper

zookeeper.connect: Zookeeper connection string is a comma separated host:port pairs, each corresponding to a zk server. e.g. “127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002”. We can also append an optional root string to the urls to specify theroot directory for all kafka znodes.

zookeeper.connect=localhost:2181

zookeeper.connection.timeout.ms: Timeout in ms for connecting to zookeeper.

zookeeper.connection.timeout.ms=6000

Read More on Kafka

Integration

Integrate Filebeat, Kafka, Logstash, Elasticsearch and Kibana