关于javaksort的信息

博主:adminadmin 2023-03-19 21:47:07 278

本篇文章给大家谈谈javaksort,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何在Hadoop中使用Streaming编写MapReduce

Michael G. Noll在他的Blog中提到如何在Hadoop中用Python编写MapReduce程序,韩国的gogamza在其Bolg中也提到如何用C编写MapReduce程序(我稍微修改了一下原程序,因为他的Map对单词切分使用tab键)。我合并他们两人的文章,也让国内的Hadoop用户能够使用别的语言来编写MapReduce程序。

首先您得配好您的Hadoop集群,这方面的介绍网上比较多,这儿给个链接(Hadoop学习笔记二 安装部署)。Hadoop Streaming帮 助我们用非Java的编程语言使用MapReduce,Streaming用STDIN (标准输入)和STDOUT (标准输出)来和我们编写的Map和Reduce进行数据的交换数据。任何能够使用STDIN和STDOUT都可以用来编写MapReduce程序,比如 我们用Python的sys.stdin和sys.stdout,或者是C中的stdin和stdout。

我们还是使用Hadoop的例子WordCount来做示范如何编写MapReduce,在WordCount的例子中我们要解决计算在一批文档中每一个单词的出现频率。首先我们在Map程序中会接受到这批文档每一行的数据,然后我们编写的Map程序把这一行按空格切开成一个数组。并对这个数组遍历按" 1"用标准的输出输出来,代表这个单词出现了一次。在Reduce中我们来统计单词的出现频率。

Python Code

Map: mapper.py

#!/usr/bin/env python

import sys

# maps words to their counts

word2count = {}

# input comes from STDIN (standard input)

for line in sys.stdin:

# remove leading and trailing whitespace

line = line.strip()

# split the line into words while removing any empty strings

words = filter(lambda word: word, line.split())

# increase counters

for word in words:

# write the results to STDOUT (standard output);

# what we output here will be the input for the

# Reduce step, i.e. the input for reducer.py

#

# tab-delimited; the trivial word count is 1

print '%s\t%s' % (word, 1)

Reduce: reducer.py

#!/usr/bin/env python

from operator import itemgetter

import sys

# maps words to their counts

word2count = {}

# input comes from STDIN

for line in sys.stdin:

# remove leading and trailing whitespace

line = line.strip()

# parse the input we got from mapper.py

word, count = line.split()

# convert count (currently a string) to int

try:

count = int(count)

word2count[word] = word2count.get(word, 0) + count

except ValueError:

# count was not a number, so silently

# ignore/discard this line

pass

# sort the words lexigraphically;

#

# this step is NOT required, we just do it so that our

# final output will look more like the official Hadoop

# word count examples

sorted_word2count = sorted(word2count.items(), key=itemgetter(0))

# write the results to STDOUT (standard output)

for word, count in sorted_word2count:

print '%s\t%s'% (word, count)

C Code

Map: Mapper.c

#include

#include

#include

#include

#define BUF_SIZE 2048

#define DELIM "\n"

int main(int argc, char *argv[]){

char buffer[BUF_SIZE];

while(fgets(buffer, BUF_SIZE - 1, stdin)){

int len = strlen(buffer);

if(buffer[len-1] == '\n')

buffer[len-1] = 0;

char *querys = index(buffer, ' ');

char *query = NULL;

if(querys == NULL) continue;

querys += 1; /* not to include '\t' */

query = strtok(buffer, " ");

while(query){

printf("%s\t1\n", query);

query = strtok(NULL, " ");

}

}

return 0;

}

hhhh

Reduce: Reducer.c

#include

#include

#include

#include

#define BUFFER_SIZE 1024

#define DELIM "\t"

int main(int argc, char *argv[]){

char strLastKey[BUFFER_SIZE];

char strLine[BUFFER_SIZE];

int count = 0;

*strLastKey = '\0';

*strLine = '\0';

while( fgets(strLine, BUFFER_SIZE - 1, stdin) ){

char *strCurrKey = NULL;

char *strCurrNum = NULL;

strCurrKey = strtok(strLine, DELIM);

strCurrNum = strtok(NULL, DELIM); /* necessary to check error but.... */

if( strLastKey[0] == '\0'){

strcpy(strLastKey, strCurrKey);

}

if(strcmp(strCurrKey, strLastKey)){

printf("%s\t%d\n", strLastKey, count);

count = atoi(strCurrNum);

}else{

count += atoi(strCurrNum);

}

strcpy(strLastKey, strCurrKey);

}

printf("%s\t%d\n", strLastKey, count); /* flush the count */

return 0;

}

hhhh

首先我们调试一下源码:

chmod +x mapper.py

chmod +x reducer.py

echo "foo foo quux labs foo bar quux" | ./mapper.py | ./reducer.py

bar 1

foo 3

labs 1

quux 2

g++ Mapper.c -o Mapper

g++ Reducer.c -o Reducer

chmod +x Mapper

chmod +x Reducer

echo "foo foo quux labs foo bar quux" | ./Mapper | ./Reducer

bar 1

foo 2

labs 1

quux 1

foo 1

quux 1

你可能看到C的输出和Python的不一样,因为Python是把他放在词典里了.我们在Hadoop时,会对这进行排序,然后相同的单词会连续在标准输出中输出.

在Hadoop中运行程序

首先我们要下载我们的测试文档wget .我们把文档存放在/tmp/doc这个目录下.拷贝测试文档到HDFS中.

bin/hadoop dfs -copyFromLocal /tmp/doc doc

运行 bin/hadoop dfs -ls doc 看看拷贝是否成功.接下来我们运行我们的MapReduce的Job.

bin/hadoop jar /home/hadoop/contrib/hadoop-0.15.1-streaming.jar -mapper /home/hadoop/Mapper\

-reducer /home/hadoop/Reducer -input doc/* -output c-output -jobconf mapred.reduce.tasks=1

additionalConfSpec_:null

null=@@@userJobConfProps_.get(stream.shipped.hadoopstreaming

packageJobJar: [] [/home/msh/hadoop-0.15.1/contrib/hadoop-0.15.1-streaming.jar] /tmp/streamjob60816.jar tmpDir=null

08/03/04 19:03:13 INFO mapred.FileInputFormat: Total input paths to process : 1

08/03/04 19:03:13 INFO streaming.StreamJob: getLocalDirs(): [/home/msh/data/filesystem/mapred/local]

08/03/04 19:03:13 INFO streaming.StreamJob: Running job: job_200803031752_0003

08/03/04 19:03:13 INFO streaming.StreamJob: To kill this job, run:

08/03/04 19:03:13 INFO streaming.StreamJob: /home/msh/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=192.168.2.92:9001 -kill job_200803031752_0003

08/03/04 19:03:13 INFO streaming.StreamJob: Tracking URL:

08/03/04 19:03:14 INFO streaming.StreamJob: map 0% reduce 0%

08/03/04 19:03:15 INFO streaming.StreamJob: map 33% reduce 0%

08/03/04 19:03:16 INFO streaming.StreamJob: map 100% reduce 0%

08/03/04 19:03:19 INFO streaming.StreamJob: map 100% reduce 100%

08/03/04 19:03:19 INFO streaming.StreamJob: Job complete: job_200803031752_0003

08/03/04 19:03:19 INFO streaming.StreamJob: Output: c-output

bin/hadoop jar /home/hadoop/contrib/hadoop-0.15.1-streaming.jar -mapper /home/hadoop/mapper.py\

-reducer /home/hadoop/reducer.py -input doc/* -output python-output -jobconf mapred.reduce.tasks=1

additionalConfSpec_:null

null=@@@userJobConfProps_.get(stream.shipped.hadoopstreaming

packageJobJar: [] [/home/hadoop/hadoop-0.15.1/contrib/hadoop-0.15.1-streaming.jar] /tmp/streamjob26099.jar tmpDir=null

08/03/04 19:05:40 INFO mapred.FileInputFormat: Total input paths to process : 1

08/03/04 19:05:41 INFO streaming.StreamJob: getLocalDirs(): [/home/msh/data/filesystem/mapred/local]

08/03/04 19:05:41 INFO streaming.StreamJob: Running job: job_200803031752_0004

08/03/04 19:05:41 INFO streaming.StreamJob: To kill this job, run:

08/03/04 19:05:41 INFO streaming.StreamJob: /home/msh/hadoop/bin/../bin/hadoop job -Dmapred.job.tracker=192.168.2.92:9001 -kill job_200803031752_0004

08/03/04 19:05:41 INFO streaming.StreamJob: Tracking URL:

08/03/04 19:05:42 INFO streaming.StreamJob: map 0% reduce 0%

08/03/04 19:05:48 INFO streaming.StreamJob: map 33% reduce 0%

08/03/04 19:05:49 INFO streaming.StreamJob: map 100% reduce 0%

08/03/04 19:05:52 INFO streaming.StreamJob: map 100% reduce 100%

08/03/04 19:05:52 INFO streaming.StreamJob: Job complete: job_200803031752_0004

08/03/04 19:05:52 INFO streaming.StreamJob: Output: python-output

当Job提交后我们还能够在web的界面看到每一个工作的运行情况。

当Job工作完成后我们能够在c-output和python-output看到一些文件

bin/hadoop dfs -ls c-output

输入下面的命令我们能够看到我们运行完MapReduce的结果

bin/hadoop dfs -cat c-output/part-00000

用Hadoop Streaming运行MapReduce会比较用Java的代码要慢,因为有两方面的原因:

使用 Java API C Streaming Perl Streaming 这样的一个流程运行会阻塞IO.

不像Java在运行Map后输出结果有一定数量的结果集就启动Reduce的程序,用Streaming要等到所有的Map都运行完毕后才启动Reduce

如果用Python编写MapReduce的话,另一个可选的是使用Jython来转编译Pyhton为Java的原生码.另外对于C程序员更好的选择是使用Hadoop新的C++ MapReduce API Pipes来编写.不管怎样,毕竟Hadoop提供了一种不使用Java来进行分布式运算的方法.

下面是从页面中摘下的用php编写的MapReduce程序,供php程序员参考:

Map: mapper.php

#!/usr/bin/php

$word2count = array();

// input comes from STDIN (standard input)

while (($line = fgets(STDIN)) !== false) {

// remove leading and trailing whitespace and lowercase

$line = strtolower(trim($line));

// split the line into words while removing any empty string

$words = preg_split('/\W/', $line, 0, PREG_SPLIT_NO_EMPTY);

// increase counters

foreach ($words as $word) {

$word2count[$word] += 1;

}

}

// write the results to STDOUT (standard output)

// what we output here will be the input for the

// Reduce step, i.e. the input for reducer.py

foreach ($word2count as $word = $count) {

// tab-delimited

echo $word, chr(9), $count, PHP_EOL;

}

?

Reduce: mapper.php

#!/usr/bin/php

$word2count = array();

// input comes from STDIN

while (($line = fgets(STDIN)) !== false) {

// remove leading and trailing whitespace

$line = trim($line);

// parse the input we got from mapper.php

list($word, $count) = explode(chr(9), $line);

// convert count (currently a string) to int

$count = intval($count);

// sum counts

if ($count 0) $word2count[$word] += $count;

}

// sort the words lexigraphically

//

// this set is NOT required, we just do it so that our

// final output will look more like the official Hadoop

// word count examples

ksort($word2count);

// write the results to STDOUT (standard output)

foreach ($word2count as $word = $count) {

echo $word, chr(9), $count, PHP_EOL;

}

?

作者:马士华 发表于:2008-03-05

把这个PHP 版本 修改为java 版本?

public String sign(Object hashkey,List? datas){

Collections.sort((ListInteger) datas); //排序

System.out.println(datas.get(0)); //打印第一个元素

String [] pre = new String[datas.size()]; //创建一个数组

int i = 0;

for (Object data : datas) {

Object key = data;

if(data == null || data == ""){

continue;

}

if (data == "hash"){

continue;

}

pre[i] = data.toString().replaceAll("/","");

}

String arg = "";

int qty = pre.length;

int index = 0;

for (String key : pre) {

Object val = key;

arg = key = val.toString();

if(index++ qty){

arg = "";

}

}

return "md5" + (arg + hashkey);

}

鬼知道对不对,无聊写写玩,我也不知道php啥意思

php里说出数组的常用函数及用法?

PHP常用操作数组的函数

变量和数组的转换

compact() 将变量整合成数组

extract() 将数组中的每个值以键的名分解成变量

变量和字符串转换

explode() 以某个子串分解字符串成数组

implode() 将一维数组根据某个符号拼接成字符串

数组与数组之间关系

array_merge() 合并/并集

array_diff() 差集

array_intersect() 交集

数组值的操作

array_pop() 删除(弹出)数组最后一个值

array_push() 向数组中追加一个值

判断数组是否存在数组中

in_array() 判断一个值是否存在数组中

array_key_exists() 判断键是否存在数组中

数组去重

array_unique() 数组去重

获取二维数组中的值的集合

array_column() 获取二维数组中的值的集合

提取数组的键与值

array_values 提取数组的值构成一维数组

array_keys 提取数组的键构成一维数组

返回数组中的随机的键

array_rand() 返回数组中的随机的键

返回数组中值的数量

count() 返回数组中值的和

查询数组中的值

array_search() 查询数组中的值是否存在/in_array()有点相似

排序

sort() 排序有很多种,按键或值升降序

array_multisort() 多维数组排序

分割数组

array_chunk()

php倒序排列和正序排列

可以在后台对栏目进行排序.栏目管理那里。

使用函数颠倒数组,$arrchildid实际上是个数组。

loop就是php里的foreach。

PHP,是英文超文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛地运用。

PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML文档中去执行,执行效率比完全生成HTML标记的CGI要高许多.

PHP安装PHP原始为Personal Home Page的缩写,已经正式更名为 "PHP: Hypertext Preprocessor"的缩写。这种将名称放到定义中的写法被称作递归缩写。

PHP于19 ISAPI筛选器94年由Rasmus Lerdorf创建,刚刚开始是Rasmus Lerdorf 为了要维护个人网页而制作的一个简单的用Perl语言编写的程序。

这些工具程序用来显示 Rasmus Lerdorf 的个人履历,以及统计网页流量。后来又用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。

PHP代码变成java代码

php代码没几行,信息量很大,翻译成java代码行数量比较大。仅提供思路和php代码解释。

---------------

?php 

$appid = "123"; //数组里面的值,id。

$apikey = "456"; //数组里面的值,为加密密钥。

$secretKey ="789"; //数组里面的值,安全密钥。

$timestamp = time(); ////数组里面的值,获得当前时间。

//UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和。

//echo输出$timestamp变量值,例如输出了1389379960

echo $timestamp;  

//定义数组。以键值对方式存储。

//'appid' 'apikey' 'secretkey' 'timestamp'是key,键。

//$appid $apikey, $secretKey $timestamp是value,值。

$params = array('appid'=$appid, 'apikey'=$apikey, 'secretkey'=$secretKey, 'timestamp'=$timestamp);

//对数组键值进行升序排序。排序结果为apikey appid secretkey timestamp

ksort($params);

//拼接数组中的参数,并且用encoded编码。

//http_build_query -- 生成 url-encoded 之后的请求字符串。当数组没有写下标时,就会用第二个参数结合当前默认下标当前缀。

//$param_uri变量值,结果为apikey=456appid=123secretkey=789×tamp=1389379498

$param_uri = http_build_query($params,'','');

echo $param_uri;   //echo输出结果为apikey=456appid=123secretkey=789×tamp=1389379498

//先使用调用hash_hmac方法加密,HMAC-SHA1算法。

//$secretKey为安全密钥,$param_uri为要加密的明文。'sha1'是HMAC-SHA1算法。

//再调用base64_encode方法加密,base64_encode 使用 MIME base64 对数据进行编码。

$sig = base64_encode(hash_hmac('sha1', $param_uri, $secretKey));

?

java:

1、用hashmap存储元素,键值对方式。

MapString, String hashMap = new HashMapString, String(){

            {

            put("appid", "123");

            put("apikey", "456");

    put("secretKey", "789");

    put("timestamp", "当前UNIX 时间戳,秒数,java中获取");

            }            

};

2、java中可以通过Timestamp获得UNIX 时间戳。

3、然后对hashmap进行升序排序。

4、然后写一个方法遍历hashmap,拼接成字符串格式为apikey=456appid=123secretkey=789timestamp=1389379498

然后对该字符串进行encoded编码,输出格式为apikey=456appid=123secretkey=789×tamp=1389379498

5、通过java中HMAC-SHA1算法加密该字符串,$secretKey为安全密钥。

6、再通过base64_encode加密第5步产生的字符串。这是最终sig结果。

关于javaksort和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。