hsyndicate.hadoop.utils.DFSNodeInfoUtils.java Source code

Java tutorial

Introduction

Here is the source code for hsyndicate.hadoop.utils.DFSNodeInfoUtils.java

Source

/*
   Copyright 2016 The Trustees of University of Arizona
    
   Licensed under the Apache License, Version 2.0 (the "License" );
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package hsyndicate.hadoop.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.hdfs.protocol.HdfsConstants;
import org.apache.hadoop.hdfs.server.namenode.NameNode;

public class DFSNodeInfoUtils {

    public static final Log LOG = LogFactory.getLog(DFSNodeInfoUtils.class);

    public static String[] getDataNodes(Configuration conf) throws IOException {
        List<String> datanodes = new ArrayList<String>();
        DFSClient client = new DFSClient(NameNode.getAddress(conf), conf);
        DatanodeInfo[] datanodeReport = client.datanodeReport(HdfsConstants.DatanodeReportType.LIVE);
        for (DatanodeInfo nodeinfo : datanodeReport) {
            datanodes.add(nodeinfo.getHostName().trim());
        }

        return datanodes.toArray(new String[0]);
    }

    public static String getDataNodesCommaSeparated(Configuration conf) throws IOException {
        StringBuilder sb = new StringBuilder();
        DFSClient client = new DFSClient(NameNode.getAddress(conf), conf);
        DatanodeInfo[] datanodeReport = client.datanodeReport(HdfsConstants.DatanodeReportType.LIVE);
        for (DatanodeInfo nodeinfo : datanodeReport) {
            if (sb.length() != 0) {
                sb.append(",");
            }
            sb.append(nodeinfo.getHostName().trim());
        }

        return sb.toString();
    }
}