如何向 hadoop 导入数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何向 hadoop 导入数据相关的知识,希望对你有一定的参考价值。
参考技术A 1.2使用Hadoop
shell命令导入和导出数据到HDFS
实验准备
实例需要用到的数据-weblog_entries.txt
在namenode创建2个文件夹用来存放实验用的数据
mkdir
/home/data
1
mkdir
/home/data_download1
将weblog_entries.txt上传到namenode里的/home/data文件夹(我使用SecureFXPortable.exe
工具)
注:以下命令都是在namenode节点运行的
实验过程
1.在HDFS中创建一个新的文件夹,用于保存weblog_entries.txt
hadoop
fs
-mkdir
/data/weblogs1
2.将weblog_entries.txt文件从本地文件系统复制到HDFS刚创建的新文件夹下
cd
/home/data1
hadoop
fs
-copyFromLocal
weblog_entries.txt
/data/weblogs1
3.列出HDFS上weblog_entries.txt文件的信息:
hadoop
fs
–ls
/data/weblogs/weblog_entries.txt
1
4.将HDFS上的weblog_entries.txt文件复制到本地系统的当前文件夹下
cd
/home/data_download1
hadoop
fs
-copyToLocal
/data/weblogs/weblog_entries.txt
./weblog_entries.txt
1
最后用
ls
命令检验下时候将weblog_entries.txt
下载到本地
如何通过socket向unity传递数据
Unity 里使用 Socket 需要导入 System.Net.Sockets 名字空间中的类。
下面是一个利用 Socket 接收数据的 Unity 客户端程序例子:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;
// Socket 客户端
public class SocketHelper
private static SocketHelper socketHelper=new SocketHelper();
private Socket socket;
// 单件
public static SocketHelper GetInstance()
return socketHelper;
private SocketHelper()
//采用TCP方式连接
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//服务器IP地址
IPAddress address = IPAddress.Parse("127.0.0.1");
//服务器端口
IPEndPoint endpoint = new IPEndPoint(address,8000);
//异步连接,连接成功调用connectCallback方法
IAsyncResult result = socket.BeginConnect(endpoint, new AsyncCallback(ConnectCallback), socket);
//这里做一个超时的监测,当连接超过5秒还没成功表示超时
bool success = result.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
//超时
Closed();
Debug.Log("connect Time Out");
else
//与socket建立连接成功,开启线程接受服务端数据。
Thread thread = new Thread(new ThreadStart(ReceiveSorket));
thread.IsBackground = true;
thread.Start();
private void ConnectCallback(IAsyncResult asyncConnect)
Debug.Log("connect success");
private void ReceiveSorket()
//在这个线程中接受服务器返回的数据
while (true)
if (!socket.Connected)
//与服务器断开连接跳出循环
Debug.Log("Failed to clientSocket server.");
socket.Close();
break;
try
//接受数据保存至bytes当中
byte[] bytes = new byte[4096];
//Receive方法中会一直等待服务端回发消息
//如果没有回发会一直在这里等着。
int i = socket.Receive(bytes);
if (i <= 0)
socket.Close();
break;
Debug.Log(System.Text.Encoding.Default.GetString(bytes));
catch (Exception e)
Debug.Log("Failed to clientSocket error." + e);
socket.Close();
break;
//关闭Socket
public void Closed()
if (socket != null && socket.Connected)
socket.Shutdown(SocketShutdown.Both);
socket.Close();
socket = null;
//向服务端发送一条字符串
//一般不会发送字符串 应该是发送数据包
public void SendMessage(string str)
byte[] msg = Encoding.UTF8.GetBytes(str);
if (!socket.Connected)
socket.Close();
return;
try
IAsyncResult asyncSend = socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
bool success = asyncSend.AsyncWaitHandle.WaitOne(5000, true);
if (!success)
socket.Close();
Debug.Log("Failed to SendMessage server.");
catch
Debug.Log("send message error");
private void SendCallback(IAsyncResult asyncConnect)
Debug.Log("send success");
参考技术A Unity 里使用 Socket 需要导入 System.Net.Sockets 名字空间中的类。
下面是一个利用 Socket 接收数据的 Unity 客户端程序例子:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.Threading; using UnityEngine; // Socket 客户端public class SocketHelper private static SocketHelper socketHelper=new SocketHelper(); private Socket socket; // 单件 public static SocketHelper GetInstance() return socketHelper; private SocketHelper() //采用TCP方式连接 socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //服务器<a href="http://www.baidu.com/s?wd=IP%E5%9C%B0%E5%9D%80&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhuycLmyczrHDdPHPWPyR10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHTdP1Dzrj6LnHckrjcdnjcvr0" target="_blank" class="baidu-highlight">IP地址</a> IPAddress address = IPAddress.Parse("127.0.0.1"); //<a href="http://www.baidu.com/s?wd=%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF%E5%8F%A3&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dhuycLmyczrHDdPHPWPyR10ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHTdP1Dzrj6LnHckrjcdnjcvr0" target="_blank" class="baidu-highlight">服务器端口</a> IPEndPoint endpoint = new IPEndPoint(address,8000); //异步连接,连接成功调用connectCallback方法 IAsyncResult result = socket.BeginConnect(endpoint, new AsyncCallback(ConnectCallback), socket); //这里做一个超时的监测,当连接超过5秒还没成功表示超时 bool success = result.AsyncWaitHandle.WaitOne(5000, true); if (!success) //超时 Closed(); Debug.Log("connect Time Out"); else //与socket建立连接成功,开启线程接受服务端数据。 Thread thread = new Thread(new ThreadStart(ReceiveSorket)); thread.IsBackground = true; thread.Start(); private void ConnectCallback(IAsyncResult asyncConnect) Debug.Log("connect success"); private void ReceiveSorket() //在这个线程中接受服务器返回的数据 while (true) if (!socket.Connected) //与服务器断开连接跳出循环 Debug.Log("Failed to clientSocket server."); socket.Close(); break; try //接受数据保存至bytes当中 byte[] bytes = new byte[4096]; //Receive方法中会一直等待服务端回发消息 //如果没有回发会一直在这里等着。 int i = socket.Receive(bytes); if (i <= 0) socket.Close(); break; Debug.Log(System.Text.Encoding.Default.GetString(bytes)); catch (Exception e) Debug.Log("Failed to clientSocket error." + e); socket.Close(); break; //关闭Socket public void Closed() if (socket != null && socket.Connected) socket.Shutdown(SocketShutdown.Both); socket.Close(); socket = null; //向服务端发送一条字符串 //一般不会发送字符串 应该是发送数据包 public void SendMessage(string str) byte[] msg = Encoding.UTF8.GetBytes(str); if (!socket.Connected) socket.Close(); return; try IAsyncResult asyncSend = socket.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket); bool success = asyncSend.AsyncWaitHandle.WaitOne(5000, true); if (!success) socket.Close(); Debug.Log("Failed to SendMessage server."); catch Debug.Log("send message error"); private void SendCallback(IAsyncResult asyncConnect) Debug.Log("send success");
以上是关于如何向 hadoop 导入数据的主要内容,如果未能解决你的问题,请参考以下文章