import java.math.BigInteger
import java.util
import java.util.concurrent.atomic.AtomicLong
import com.ebctech.PAGFeed.updater.Authorization
import com.google.api.services.content.model.{Product,ProductsCustomBatchRequest,ProductsCustomBatchRequestEntry}
import net.sf.cglib.core.KeyFactory
import scala.collection.mutable
import scala.io.Source
object ProductUpdtaeForParticularField {
private val merchantId = new BigInteger("MerchantId")
private val file = "/home/spineor/40TocheckParticularId"
val batchId = new AtomicLong()
val particularId: mutable.Map[String,List[String]] = mutable.Map[String,List[String]]()
def loadCsv(File: String): mutable.Map[String,List[String]] = {
for (line <- Source.fromFile(File).getLines) {
val cols = line.split("\\^").toList
particularId += cols.head -> cols
}
particularId
}
def main(args: Array[String]): Unit = {
val service = Authorization.getShoppingContent
val source: mutable.Map[String,List[String]] = loadCsv(file)
var sourceId = source.map(i => (s"online:en:US:${i._1}",i._2))
var i = 0
while(i < sourceId.size){
val request = service.products().custombatch(createBatch(sourceId.take(20)))
val result = request.execute()
i += 20
sourceId.drop(20)
println("source Id:" + sourceId)
}
def createBatch(products: mutable.Map[String,List[String]]): ProductsCustomBatchRequest = {
val productsBatchRequestEntries = new util.ArrayList[ProductsCustomBatchRequestEntry]
products.map( i => productsBatchRequestEntries
.add(new ProductsCustomBatchRequestEntry()
.setBatchId(batchId.incrementAndGet())
.setProductId(i._1)
.set("brand",i._2(1))
.setMerchantId(merchantId)
.setMethod("insert")
))
println("batch Id " + batchId)
val batchRequest = new ProductsCustomBatchRequest
batchRequest.setEntries(productsBatchRequestEntries)
println("%%%%%%%%%%%%%%%%%%%%%%")
println(batchRequest)
batchRequest
}
}
}
错误:线程“ main”中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:400错误的请求 { “代码”:400, “错误”:[{ “ domain”:“ global”, “ location”:“ entries”, “ locationType”:“ other”, “ message”:“未知字段名称:品牌”, “ reason”:“无效” }], “ message”:“未知字段名称:品牌” } 在com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) com.google.api.client.googleapis.services.AbstractGoogleclientRequest $ 1.interceptResponse(AbstractGoogleclientRequest.java:321) 在com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1089) com.google.api.client.googleapis.services.AbstractGoogleclientRequest.executeUnparsed(AbstractGoogleclientRequest.java:419) com.google.api.client.googleapis.services.AbstractGoogleclientRequest.executeUnparsed(AbstractGoogleclientRequest.java:352) com.google.api.client.googleapis.services.AbstractGoogleclientRequest.execute(AbstractGoogleclientRequest.java:469) 在com.ebctech.PAGFeed.ProductUpdtaeForParticularField $ .main(ProductUpdtaeForParticularField.scala:38) 在com.ebctech.PAGFeed.ProductUpdtaeForParticularField.main(ProductUpdtaeForParticularField.scala)