[英]Unable to persist a 'write' to a specific datanode in HDFS
我正在嘗試創建一個索引策略,該策略要求索引塊與數據塊位於同一數據節點上,以減少數據檢索時的延遲。 我設法編寫了用於讀取與特定文件相關的數據塊的代碼。 為了進行寫入,我打開了到特定數據節點的套接字連接,寫入了數據,然后關閉了套接字。 不幸的是,我不確定使用此方法寫入數據的“位置”或“方式”,因為當我使用hadoop fs -ls
查詢HDFS時,我看不到任何地方寫入的數據(也許是在一些雜散文件中!!)。 ),但我的程序執行時沒有任何錯誤。
這是我的代碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DFSClient;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
import org.apache.hadoop.hdfs.server.datanode.DataNode;
import org.apache.hadoop.hdfs.server.protocol.DatanodeRegistration;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.FileChannel;
import java.nio.file.OpenOption;
import java.nio.file.StandardOpenOption;
import java.util.Random;
import java.nio.ByteBuffer;
import javax.net.SocketFactory;
import org.apache.hadoop.security. UserGroupInformation;
public class CopyOfChunkedIndexes {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: ChunkedIndexes <input path>");
System.exit(-1);
}
Configuration conf = new Configuration();
conf.set("fs.default.name", "hdfs://localhost:9000"); //for defaulting to HDFS rather than local filesystem
conf.set("hadoop.security.authentication", "simple"); //disable authentication
conf.set("hadoop.security.authorization", "false"); //disable authorization
Job job = Job.getInstance(conf, "Chunked Indexes");
job.setJarByClass(CopyOfChunkedIndexes.class);
Path inputPath = new Path("/user/hadoop-user/sample.txt");
FileInputFormat.setInputPaths(job, inputPath);
try{
FileSystem fs = FileSystem.get(conf);
DistributedFileSystem dfs = (DistributedFileSystem) fs;
DFSClient dfsclient = dfs.getClient();
System.out.println("Proceeding for file: " + inputPath.toString());
FileStatus fileStatus = fs.getFileStatus(inputPath);
BlockLocation[] bLocations = fs.getFileBlockLocations(inputPath, 0, fileStatus.getLen());
for(int i = 0; i < bLocations.length; i++)
{
System.out.println("Block[" + + i + "]::");
System.out.println("\nHost(s): ");
String[] temp = bLocations[i].getHosts();
for(int j = 0; j < temp.length; j++)
{
System.out.println(temp[j] + "\t");
}
System.out.println("\nBlock length: " + bLocations[i].getLength() +
"\n\nDataNode(s) hosting this block: ");
temp = bLocations[i].getNames();
for(int j = 0; j < temp.length; j++)
{
System.out.println(temp[j] + "\t");
}
System.out.println("\nOffset: " + bLocations[i].getOffset());
//READING A BLOCK
FSDataInputStream in = fs.open(inputPath);
in.seek(bLocations[i].getOffset());
byte[] buf = new byte[(int)bLocations[i].getLength()];
in.read(buf, (int)bLocations[i].getOffset(), (int)bLocations[i].getLength());
in.close();
System.out.println(new String(buf, "UTF-8"));
System.out.println("--------------------------------------------------------------------------------------------");
}
//WRITE A FILE TO A SPECIFIC DATANODE
for(int i = 0; i < bLocations.length; i++)
{
System.out.println("Block[" + + i + "]::");
String[] temp;
System.out.println("\n\nDataNode(s) hosting this block: "); //Name(s) = datanode addresses
temp = bLocations[i].getNames();
for(int j = 0; j < temp.length; j++)
{
System.out.println(temp[j].split(":")[0] + "\t" + temp[j].split(":")[1]); //host vs. port
}
Socket sock = SocketFactory.getDefault().createSocket();
InetSocketAddress targetAddr = new InetSocketAddress(temp[0].split(":")[0], Integer.parseInt(temp[0].split(":")[1]));
NetUtils.connect(sock, targetAddr, 10000);
sock.setSoTimeout(10000);
OutputStream baseStream = NetUtils.getOutputStream(sock, 10000);
DataOutputStream oStream = new DataOutputStream(new BufferedOutputStream(baseStream, 10000));
oStream.writeBytes("-----------------------------------------Sample text-----------------------------------------------");
sock.close();
System.out.println("Data written, socket closed!");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
對於我要去哪里的任何幫助,將深表感謝! 謝謝!
[ PS:我在Linux VM上使用Hadoop 2.2.0。 我在上面的代碼中禁用了授權/身份驗證,因為我想直接訪問數據節點(沒有身份驗證的“開銷”),因為這是出於測試目的。
所有編輯都將被群集丟棄,因為您沒有通過名稱節點。 您所做的所有修改都被視為文件損壞。
Hadoop已經為您完成了這項工作:當您想在Hadoop集群上執行分布式任務時,將為任務加載最近的數據。 例如,如果您有一個Elasticsearch集群和一個Hadoop集群共享相同的硬件,則只需創建一個mapreduce任務,該任務將使用本地的Elasticsearch節點,僅此而已:數據沒有網絡舞動,所有任務將加載一個部分數據集並將其推送到本地elasticsearch實例。
請享用!
有一個名為BlockPlacementPolicy
的類(理論上可以擴展),以自定義HDFS如何選擇數據節點。 盡管這種做法很笨拙,但對於希望將來做類似事情並且偶然發現此問題的人來說,這種方法可能有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.