首页 文章 精选 留言 我的

精选列表

搜索[linux编程],共10000篇文章
优秀的个人博客,低调大师

大数据||MapReduce编程模板

标准模板代码 package com.lizh.hadoop.mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import com.lizh.hadoop.mapreduce.WordCountMapReduce.WordCountMapper; import com.lizh.hadoop.mapreduce.WordCountMapReduce.WordCountReduces; public class MouldMapReduce extends Configured implements Tool{ public class MouldMap extends Mapper<LongWritable, Text, Text, IntWritable>{ @Override protected void setup(Context context) throws IOException, InterruptedException { // TODO 读取数据前的一些初始化工作或者读取文件前的一些初始化工作 super.setup(context); } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // TODO super.map(key, value, context); } @Override protected void cleanup(Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub super.cleanup(context); } } public class MouldReduce extends Reducer<Text, IntWritable, Text,IntWritable>{ @Override protected void setup(Context context) throws IOException, InterruptedException { // TODO 读取数据前的一些初始化工作或者读取文件前的一些初始化工作 super.setup(context); } @Override protected void reduce(Text arg0, Iterable<IntWritable> arg1,Context arg2) throws IOException, InterruptedException { // TODO Auto-generated method stub super.reduce(arg0, arg1, arg2); } @Override protected void cleanup( org.apache.hadoop.mapreduce.Reducer.Context context) throws IOException, InterruptedException { // TODO Auto-generated method stub super.cleanup(context); } } private Job getJob(String[] args){ Configuration configuration = this.getConf(); Job job = null; try { job = Job.getInstance(configuration, this.getClass().getSimpleName()); job.setJarByClass(this.getClass()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return job; } public int run(String[] args) throws Exception { // TODO Auto-generated method stub //input-->map--reduce--output //getjob Job job = getJob(args); //setjob Path path = new Path(args[0]); FileInputFormat.addInputPath(job, path); // map job.setMapperClass(WordCountMapper.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); // reduce job.setReducerClass(WordCountReduces.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); //output Path outputpath = new Path(args[1]); FileOutputFormat.setOutputPath(job, outputpath); // submit job boolean rv = job.waitForCompletion(true);//true的时候打印日志 return rv ? 0:1; } public static void main(String[] args) throws Exception{ Configuration conf = new Configuration(); ToolRunner.run(conf, new MouldMapReduce(), args); } }

优秀的个人博客,低调大师

Hadoop里如何Mysql编程

mysql数据库读写 hadoop技术推出一度曾遭到关系数据库研究者的挑衅和批评,认为MapReduce不具有关系数据库中的结构化数据存储和处理能力。为此,hadoop社区和研究人员做了多的努力,在hadoop0.19版支持MapReduce访问关系数据库,如:MySQL、Mongodb、PostgreSQL、Oracle等几个数据库系统。Hadoop 访问关系数据库主要通过DBInputFormat类实现的,包的位置在 org.apache.hadoop.mapred.lib.db。以 Mysql为例来学习 MapReduce读写数据。 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于Oracle旗下公司。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。 Mongodb百度百科: http://baike.baidu.com/subview/3385614/9338180.htm PostgreSQL百度百科: http://baike.baidu.com/view/28196.htm Oracle百度百科: 读数据 DBInputFormat 在 Hadoop 应用程序中通过数据库供应商提供的 JDBC接口来与数据库进行交互,并且可以使用标准的 SQL 来读取数据库中的记录。学习DBInputFormat首先必须知道二个条件。 第一、在使用 DBInputFormat 之前,必须将要使用的JDBC驱动拷贝到分布式系统各个节点的$HADOOP_HOME/lib/目录下。 第二、MapReduce访问关系数据库时,大量频繁的从MapReduce程序中查询和读取数据,这大大的增加了数据库的访问负载,因此,DBInputFormat接口仅仅适合读取小数据量的数据,而不适合处理数据仓库。 提示处理数据仓库的方法有:利用数据库的 Dump 工具将大量待分析的数据输出为文本,并上传到 HDFS 中进行处理。 http://baike.baidu.com/view/22424.htm?fromtitle=Oracle&fromid=301207&type=syn 下面我们来看看 DBInputFormat类的内部结构,DBInputFormat 类中包含以下三个内置类。 1、protected class DBRecordReader implements RecordReader< LongWritable, T>:用来从一张数据库表中读取一条条元组记录。 2、public static class NullDBWritable implements DBWritable,Writable:主要用来实现 DBWritable 接口。DBWritable接口要实现二个函数,第一是write,第二是readFileds,这二个函数都不难理解,一个是写,一个是读出所有字段。原型如下: public void write(PreparedStatement statement) throwsSQLException; public void readFields(ResultSet result); 3、protected static class DBInputSplit implements InputSplit:主要用来描述输入元组集合的范围,包括 start 和 end 两个属性,start 用来表示第一条记录的索引号,end 表示最后一条记录的索引号。 下面对怎样使用 DBInputFormat 读取数据库记录进行详细的介绍,具体步骤如下: 步骤一 、配置 JDBC 驱动、数据源和数据库访问的用户名和密码。代码如下。 DBConfiguration.configureDB(Job job, StringdriverClass, String dbUrl, String userName, String passwd) MySQL 数据库的 JDBC 的驱动为“com.mysql.jdbc.Driver”, 数据源为“jdbc:mysql://localhost/testDB”, 其中testDB为访问的数据库。useName一般为“root”,passwd是你数据库的密码。 步骤二、使用 setInput 方法操作 MySQL 中的表,setInput 方法的参数如下。 DBInputFormat.setInput(Job job, Class< extends DBWritable> inputClass, String tableName, String conditions,String orderBy, String... fieldNames) 这个方法的参数很容易看懂,inputClass实现DBWritable接口。string tableName表名, conditions表示查询的条件,orderby表示排序的条件,fieldNames是字段,这相当与把sql语句拆分的结果。当然也可以用sql语句进行重载,代码如下。 setInput(Job job, Class< extends DBWritable> inputClass, String inputQuery, StringinputCountQuery)。 步骤三、编写MapReduce函数,包括Mapper 类、Reducer 类、输入输出文件格式等,然后调用job.waitForCompletion(true)。 本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/5659460.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

Hadoop MapReduce(FlowCount) Java编程

编写PhoneFlow程序,计算手机上行流量、下行流量以及总流量,数据如下: 13685295623 122 201 13985295600 102 11 13885295622 22 101 13785295633 120 20 1、FlowMapper: package com.hadoop.flow; import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import org.apache.commons.lang.StringUtils; public class FlowMapper extends Mapper<LongWritable, Text, Text, FlowBean>{ /** * 数据格式: * 13685295623 122 201 * 13985295600 102 11 * 13885295622 22 101 * 13785295633 120 20 */ @Override protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException { String line=value.toString(); String [] fields=StringUtils.split(line,"\t"); String phoneNB=fields[0]; long up_flow=Long.valueOf(fields[1]); long d_flow=Long.valueOf(fields[2]); context.write(new Text(phoneNB), new FlowBean(phoneNB,up_flow,d_flow)); } } 2、FlowReducer: package com.hadoop.flow; import java.io.IOException; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class FlowReducer extends Reducer<Text, FlowBean,Text, FlowBean> { @Override protected void reduce(Text key, Iterable<FlowBean> values,Context context) throws IOException, InterruptedException { long upflowC=0; long dflowD=0; for(FlowBean bean:values){ upflowC+=bean.getUp_flow(); dflowD+=bean.getD_flow(); } context.write(key,new FlowBean(key.toString(),upflowC,dflowD)); } } 3、FlowRunner package com.hadoop.flow; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.apache.hadoop.io.Text; public class FlowRunner extends Configured implements Tool{ public int run(String[] args) throws Exception { Configuration conf=new Configuration(); Job job=Job.getInstance(conf); job.setJarByClass(FlowRunner.class); job.setMapperClass(FlowMapper.class); job.setReducerClass(FlowReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(FlowBean.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(FlowBean.class); FileInputFormat.setInputPaths(job,new Path(args[0])); FileOutputFormat.setOutputPath(job,new Path(args[1])); return job.waitForCompletion(true)?0:1; } public static void main(String[] args) throws Exception { ToolRunner.run(new Configuration(), new FlowRunner(), args); } } 4、FlowBean : package com.hadoop.flow; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.io.Writable; public class FlowBean implements Writable{ private String phoneNB; private long up_flow; private long d_flow; private long s_flow; public FlowBean(){ } public FlowBean (String phoneNB,long up_flow,long d_flow){ this.phoneNB=phoneNB; this.up_flow=up_flow; this.d_flow=d_flow; this.s_flow=up_flow+d_flow; } public String getPhoneNB() { return phoneNB; } public void setPhoneNB(String phoneNB) { this.phoneNB = phoneNB; } public long getUp_flow() { return up_flow; } public void setUp_flow(long up_flow) { this.up_flow = up_flow; } public long getD_flow() { return d_flow; } public void setD_flow(long d_flow) { this.d_flow = d_flow; } public long getS_flow() { return s_flow; } public void setS_flow(long s_flow) { this.s_flow = s_flow; } // public void write(DataOutput out) throws IOException { out.writeUTF(phoneNB); out.writeLong(up_flow); out.writeLong(d_flow); out.writeLong(s_flow); } public void readFields(DataInput in) throws IOException { phoneNB= in.readUTF(); up_flow=in.readLong(); d_flow=in.readLong(); s_flow=in.readLong(); } @Override public String toString() { return up_flow+" "+d_flow+" "+" "+s_flow; } } 本文转自lzf0530377451CTO博客,原文链接:http://blog.51cto.com/8757576/1839299,如需转载请自行联系原作者

优秀的个人博客,低调大师

Hadoop MapReduce(WordCount) Java编程

编写WordCount程序数据如下: hello beijing hello shanghai hello chongqing hello tianjin hello guangzhou hello shenzhen ... 1、WCMapper: package com.hadoop.testHadoop; import java.io.IOException; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; //4个泛型中,前两个是指定mapper输入数据的类型,KEYIN是输入的key的类型,VALUEIN是输入的value的类型 //map 和 reduce 的数据输入输出都是以 key-value对的形式封装的 //默认情况下,框架传递给我们的mapper的输入数据中,key是要处理的文本中一行的起始偏移量,value为这一行的内容 //LongWritable Text 是hadoop为了序列化 定义的数据类型 public class WCMapper extends Mapper<LongWritable,Text,Text,LongWritable>{ //mapreduce框架每读一行数据就调用一次该方法 @Override protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException { String line=value.toString(); String [] words = line.split(" "); for(String word:words){ context.write(new Text(word), new LongWritable(1)); } } } 2、WCReducer: package com.hadoop.testHadoop; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable>{ //框架在map处理完成之后,将所有kv对缓存起来,进行分组,然后传递一个组<key,valus{}>,调用一次reduce方法 //<hello,{1,1,1,1,1,1.....}> @Override protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws java.io.IOException ,InterruptedException { long count=0; for(LongWritable value:values){ count+=value.get(); } context.write(key, new LongWritable(count)); } } 3、WCRunner: package com.hadoop.testHadoop; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; 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; public class WCRunner { public static void main(String[] args) throws Exception { Configuration conf=new Configuration(); Job job = Job.getInstance(conf); //设置整个job所用的那些类在哪个jar包 job.setJarByClass(WCRunner.class); job.setMapperClass(WCMapper.class); job.setReducerClass(WCReducer.class); //map输出数据kv类型 job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(LongWritable.class); //reduce输出数据kv类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(LongWritable.class); //执行输入数据的路径 FileInputFormat.setInputPaths(job, new Path("/wordcount/inpput")); //执行输出数据的路径 FileOutputFormat.setOutputPath(job, new Path("/wordcount/outputmy")); //将job提交给集群运行 job.waitForCompletion(true); } } 本文转自lzf0530377451CTO博客,原文链接:http://blog.51cto.com/8757576/1839294,如需转载请自行联系原作者

优秀的个人博客,低调大师

android网络编程——http post

前一篇给出了HttpGet的实例,这一篇给出HttpPost的实例。 public class HttpPostDemo extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost("http://mysomewebsite.com/services/doSomething.do"); List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); postParameters.add(new BasicNameValuePair("username", "test")); postParameters.add(new BasicNameValuePair("password", "test1234")); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( postParameters); request.setEntity(formEntity); HttpResponse response = client.execute(request); in = new BufferedReader( new InputStreamReader( response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; String NL = System.getProperty("line.separator"); while ((line = in.readLine()) != null) { sb.append(line + NL); } in.close(); String result = sb.toString(); System.out.println(result); } catch(Exception e) { // Do something about exceptions } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } } 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/1230791,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android学习笔记--Scoket编程

MicrosoftInternetExplorer402DocumentNotSpecified7.8Normal0 1.什么是Socket Socket英文意为"插座" 所谓Scoket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄 应用程序通常通过"套接字"向网络发出请求或者应答网络请求 2.Socket基本通信模型 3.使用基于TCP协议的Socket ServerSocketActivity代码例: 1.声明控件对象(略) 2.获得控件对象(略) 3.绑定事件(略) 4.创建监听器对象 PublicvoidonClick(Viewv){ NewServerThread().start(); } //TCPserver ClassServerThreadextendsThread{ Publicvoidrun(){ //声明一个serverSocket对象 ServerSocketserverSocket=null; Try{ //创建serverSocket对象并在4567端口监听 serverSocket=newServerSocket(4567); //调用serverSocket的accept方法,接受客户端所发送的请求 Socketsocket=serverSocket.accept();//阻塞函数 //从socket当中得到inputstream对象 InputStreaminputStream=socket.getInputStream(); Bytebuffer[]newbute[1024*4]; Inttemp=0; //从inputStream当中读取客户端所发送的数据 While((temp=inputStream.read(buffer))!=-1){ System.out,println(newString(buffer,0,temp)); } }catch(Execptione){ e.printStackTrace(); }finally{ Try{ serverSocket.close(); }catch(IOExecptione){ e.printStackTrace(); } } } } TCPClient代码例: Publicstaticvoidmain(String[]args){ Try{ //创建一个Socket对象,指定服务器端的ip地址和端口号 Socketsocket=newSocket("192.168.1.1",4567); //使用InputStream读取硬盘上的文件 InputStreaminputStream=newFileInputStream("F://file/words.txt"); //从Socket上得到outputStream outputStreamoutputStream=socket.getOutputStream(); Bytebuffer[]=newbyte[4*1024]; Inttemp=0; //将inputStream的数据取出并写入到outputStream While((temp=inputStream.read(buffer))!=-1){ outputStream.write(buffer,0,temp); } outpurStream.flush(); }catch(Execptione){ e.printStackTrace(); } } //UDPServer ClassServerThreadextendsThread{ Publicvoidrun(){ Try{ //创建一个DatagramSocket对象,并制定监听端口号 DatagramSocketsocket=newDatagramSocket(4567); Bytedata[]=newbyte[1024]; //创建一个空的DatagramPacket对象 DatagramPacketpacket=newDatagramPacket(data,data.length); //使用receive方法接受客户端所发送的数据 Socket.receive(packet);//阻塞方法 Stringresult=newString(packet.getData(),packet.getOffset(),packet.getLength());//设置数据偏移量,得到该次数据的长度 System.out.println("result-->"+result); }catch(Execptione){ e.printStackTrace(); } } } //UDPClient Publicstaticvoidmain(String[]args){ Try{ //创建一个DatagramSocket对象,并制定监听端口号 DatagramSocketsocket=newDatagramSocket(456 7); InetAddressserverAddress=InetAddress.getByName("192.168.1.1"); Stringstr="hello"; Bytedata[]=str.getBytes(); DatagramPacketpacket=newDatagramPacket(data,data.length,serverAddress,4567); Socket.send(packet); }catch(Execptione){ e.printStackTrace(); } } 4.使用基于UDP协议的Socket 本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360562,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码