我在accumulo中有一个表,每个row_id有几个家族和限定符,它看起来像这样在accumulo shell中。
michaelp@accumulo records> scan
2016-10-17 16:27:55,359 [Shell.audit] INFO : michaelp@accumulo records> scan
E001 department:sales [] 0
E001 hire_date:20160101 [] 0
E001 name:bob [] 0
E001 name:jerry [] 0
E002 department:marketing [] 0
E002 hire_date:20160202 [] 0
E002 name:sarah [] 0
E003 department:engineering [] 0
E003 hire_date:20160303 [] 0
E003 name:joe [] 0
我希望能够扫描这几行与scala连接器。在需要的导入之后,我的代码看起来像这样:
var opts = new ClientOnRequiredTable()
var bsOpts = new BatchScannerOpts()
opts.parseArgs("test", Array("-t", "records","-u", "michaelp", "-p", "****", "-z", "zookeeper:2181", "-i", "accumulo"), bsOpts)
var connector = opts.getConnector()
var batchReader = connector.createBatchScanner("records", opts.auths, bsOpts.scanThreads)
batchReader.setTimeout(bsOpts.scanTimeout, TimeUnit.MILLISECONDS)
var x = new Range()
var y = new LinkedList[Range]
y.add(x)
batchReader.setRanges(y)
传入一个空范围以获得表中的每一行。问题是当我尝试迭代的结果。它粘在第一行
scala> while (batchReader.iterator.hasNext()) {println(batchReader.iterator.next.getKey().toString())}
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
E001 department:sales [] 1476720996135 false
...
那么为什么迭代器没有移动呢?
因为每次调用batchReader.iterator
时都会创建新的迭代器。相反,在
val iterator = batchReader.iterator
while(iterator.hasNext) {
println(iterator.next.getKey().toString())
}