Cassandra本地化 发表于 2015-08-07 | 分类于 Cassandra | 关键是获取token range 1234567891011121314151617181920212223242526272829303132Cluster cluster = Cluster.builder().addContactPoint("192.168.100.1").build();Metadata metadata = cluster.getMetadata();Set<Host> allHosts = metadata.getAllHosts();Session tableSession = cluster.connect(keyspace);ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());int id = 1;for (Host host : allHosts) { if (host.hashCode() == -1062697133) { Set<TokenRange> tokenRanges = metadata.getTokenRanges(keyspace, host); Set<TokenRange> unwrap = new HashSet<TokenRange>(); for (TokenRange tokenRange : tokenRanges) { unwrap.addAll(tokenRange.unwrap()); } for (TokenRange tokenRange : unwrap) { executor.execute(new newthread(tableSession, tokenRange, id)); id++; } break; } else { continue; }}executor.shutdown();while (true) { if (executor.isTerminated()) { tableSession.close(); cluster.close(); break; } Thread.sleep(1000);} 123456789Statement stmt = QueryBuilder.select().all().from("xxxx").where(QueryBuilder.gt(QueryBuilder.token("num"), tokenRange.getStart().getValue())).and(QueryBuilder.lte(QueryBuilder.token("num"), tokenRange.getEnd().getValue()));stmt.setFetchSize(1000);ResultSet rs = tableSession.execute(stmt);Iterator<Row> iterator = rs.iterator();List<String> contentList = new ArrayList<String>();while (iterator.hasNext()) { Row row = iterator.next(); contentList.add(row.toString());}