python日期操作

 

from datetime import datetime,date

dayOfWeek = datetime.now().weekday()
print dayOfWeek

******************************************************************

import time
import datetime

dtime = datetime.datetime.now()
ans_time = time.mktime(dtime.timetuple())
unix_ts = 1439111214.0
time = datetime.datetime.fromtimestamp(unix_ts)

spark streaming调用http get存储数据

环境:spark 1.6, 存储是一个http get的服务

在build.sbt中添加”org.apache.httpcomponents” % “httpclient” % “4.5.2”  ,记得第一个分隔符是%,而不是%%。

经过多次尝试,最终代码如下:

agg_wd_business.foreach(d => {
val httpParams = new BasicHttpParams()

HttpConnectionParams.setConnectionTimeout(httpParams, 50)
HttpConnectionParams.setSoTimeout(httpParams, 50)
val client = new DefaultHttpClient(httpParams)
val request = new HttpGet(“http://xxx.xxx.xxx.xxx:9010/rt?” + URLEncoder.encode(d, “UTF-8”))
request.addHeader(“Connection”, “close”)
try{
val response = client.execute(request)
val handler = new BasicResponseHandler()
handler.handleResponse(response).trim.toString
}catch{
case ex: SocketTimeoutException => None
case ex: Exception => None
}
})

发送一个http get请求,设置超时,设置为短连接,并不保证请求一定成功。由于生成的数据有30万左右,得调用http get这么次,而nginx搭配的服务并不能快速地响应。

好记忆不如烂笔头

之前解决问题的时候忘了做笔记,后来遇到相同的问题的时候,不记得之前有个解决方案。年纪大了好多东西都得记得做,有时候忘了这忘了呢,以后做这种技术的话最好还是自己做一下笔记,用wordpress

ScalaPB生成scala的protobuf文件

环境: sbt, scala 2.10.4

 

1.

vi project/scalapb.sbt

addSbtPlugin(“com.thesamet” % “sbt-protoc” % “0.99.1”)

libraryDependencies += “com.trueaccord.scalapb” %% “compilerplugin” % “0.5.43”

 

2.

vi build.sbt

PB.targets in Compile := Seq(
scalapb.gen() -> (sourceManaged in Compile).value
)

// If you need scalapb/scalapb.proto or anything from google/protobuf/*.proto
//ScalaPB looks for protocol buffer files in src/main/protobuf, but this can be customized. Running the compile command in sbt will both generate Scala sources from your protos and compile them.

libraryDependencies += “com.trueaccord.scalapb” %% “scalapb-runtime” % com.trueaccord.scalapb.compiler.Version.scalapbVersion % “protobuf”

 

3.
mkdir src/main/protobuf

vi src/main/protobuf/hello.proto

syntax = “proto3”;
package example;

message HelloRequest {
string name = 1;
}

sbt assembly
生成的scala文件放在 target/scala-2.10/src_managed/main/example/hello/HelloRequest.scala

4.

使用如下
import hello._

val h = HelloRequest().withName(“hq”)
val hba = h.toByteArray
println(hba) //serialize
println(HelloRequest.parseFrom(hba).name) //unserialize

spark streaming读取kafka上的protobuf格式的数据

1. 通过proto文件生成java文件夹

vi test1.proto

syntax = “proto2”;
package example;

message Hello{
required string name = 1;
required int32 id = 2;
}

生成Test1.java
protoc –java_out=pbdir test1.proto

 

2. 将Test1.java拷贝到src/main/java/example目录下

 

3. 通过spark streaming读取kafka上的pb数据
import Test1._

createKafkaStream(ssc, pb_topic, kafkaParams1).map(r => r._2).map(r => {val p = Hello.parseFrom(r.getBytes); p.getId + “\\t” + p.getName})

sbt.ResolveException: unresolved dependency: org.apache.httpcomponents#httpclient_2.10;4.5.2: not found

在build.sbt中添加”org.apache.httpcomponents” %% “httpclient” % “4.5.2”

编译的时候出现报错:

sbt.ResolveException: unresolved dependency: org.apache.httpcomponents#httpclient_2.10;4.5.2: not found

[error] (*:update) sbt.ResolveException: unresolved dependency: org.apache.httpcomponents#httpclient_2.10;4.5.2: not found

 

在stackoverflow找到一个解决方法

Change the first %% to a single %. The double character version is for fetching cross-built libraries, and yours isn’t.

去掉一个%,修改如下:

“org.apache.httpcomponents” % “httpclient” % “4.5.2”

 

spark kafka.common.ConsumerRebalanceFailedException

方法1.配置zk问题(kafka的consumer配置)
zookeeper.session.timeout.ms=5000
zookeeper.connection.timeout.ms=10000
rebalance.backoff.ms=2000
rebalance.max.retries=10

 

方法2. 在spark读取kafka的代码修改

val kafkaParams = Map(
“zookeeper.connect” -> zkQuorum,
“group.id” -> “default”,
“auto.offset.reset” -> “largest”,
“zookeeper.session.timeout.ms” -> “6000”,
“zookeeper.connection.timeout.ms” -> “6000”,
“zookeeper.sync.time.ms” -> “2000”,
“rebalance.backoff.ms” -> “10000”,
“rebalance.max.retries” -> “20”
)

KafkaUtils.createStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topic, StorageLevel.MEMORY_ONLY_SER).map(_._2)

hadoop streaming对数据进行排序

环境:  hadoop 1.2, python

有这样的数据,两列,中间是用\\t分隔的,需要按第二列从大到小的顺序排列。

02款雅阁 0.00611111111111
04款奥德赛 0.00813131313131
06ms201 0.000866666666667
06ms201图集 0.00704678362573
06雅阁 0.0145098039216
07常服大衣 0.00915032679739
08年本田思域 0.00111111111111
1.5d弯头 0.0211538461538
1.5匹空调 0.00929292929293
1.5米衣柜设计图 0.01640625

hadoop streaming的python程序写法如下:

hadoop streaming -input datain  -output dataout -mapper cat -reducer cat -jobconf mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator -jobconf stream.num.map.output.key.fieds=2 -jobconf stream.map.output.field.separator=”\\t” -jobconf mapred.text.key.comparator.options=”-k2,2nr”  -jobconf mapred.reduce.tasks=1

其中map, reduce都是操作系统命令cat,org.apache.hadoop.mapred.lib.KeyFieldBasedComparator 来自定义使用key中的部分字段做比较,stream.map.output.field.separator指定map出来的数据按\\t来分隔,stream.num.map.output.key.fieds是指map出来的数据的key/value分隔符在哪,这里将第一列和第二列都作为key部分,mapred.text.key.comparator.options指key中对比的方式, -k2, 2nr是指从第二列到第二列按数值反转排序。