[ https://issues.apache.org/jira/browse/BEAM-9008?focusedWorklogId=440603&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-440603 ]
ASF GitHub Bot logged work on BEAM-9008: ---------------------------------------- Author: ASF GitHub Bot Created on: 03/Jun/20 05:38 Start Date: 03/Jun/20 05:38 Worklog Time Spent: 10m Work Description: vmarquez commented on a change in pull request #10546: URL: https://github.com/apache/beam/pull/10546#discussion_r434318846 ########## File path: sdks/java/io/cassandra/src/main/java/org/apache/beam/sdk/io/cassandra/CassandraIO.java ########## @@ -370,384 +488,16 @@ private CassandraIO() {} return autoBuild(); } } - } - - @VisibleForTesting - static class CassandraSource<T> extends BoundedSource<T> { - final Read<T> spec; - final List<String> splitQueries; - // split source ached size - can't be calculated when already split - Long estimatedSize; - private static final String MURMUR3PARTITIONER = "org.apache.cassandra.dht.Murmur3Partitioner"; - - CassandraSource(Read<T> spec, List<String> splitQueries) { - this(spec, splitQueries, null); - } - - private CassandraSource(Read<T> spec, List<String> splitQueries, Long estimatedSize) { - this.estimatedSize = estimatedSize; - this.spec = spec; - this.splitQueries = splitQueries; - } - - @Override - public Coder<T> getOutputCoder() { - return spec.coder(); - } - - @Override - public BoundedReader<T> createReader(PipelineOptions pipelineOptions) { - return new CassandraReader(this); - } - - @Override - public List<BoundedSource<T>> split( - long desiredBundleSizeBytes, PipelineOptions pipelineOptions) { - try (Cluster cluster = - getCluster( - spec.hosts(), - spec.port(), - spec.username(), - spec.password(), - spec.localDc(), - spec.consistencyLevel())) { - if (isMurmur3Partitioner(cluster)) { - LOG.info("Murmur3Partitioner detected, splitting"); - return splitWithTokenRanges( - spec, desiredBundleSizeBytes, getEstimatedSizeBytes(pipelineOptions), cluster); - } else { - LOG.warn( - "Only Murmur3Partitioner is supported for splitting, using a unique source for " - + "the read"); - return Collections.singletonList( - new CassandraIO.CassandraSource<>(spec, Collections.singletonList(buildQuery(spec)))); - } - } - } - - private static String buildQuery(Read spec) { - return (spec.query() == null) - ? String.format("SELECT * FROM %s.%s", spec.keyspace().get(), spec.table().get()) - : spec.query().get().toString(); - } - - /** - * Compute the number of splits based on the estimated size and the desired bundle size, and - * create several sources. - */ - private List<BoundedSource<T>> splitWithTokenRanges( - CassandraIO.Read<T> spec, - long desiredBundleSizeBytes, - long estimatedSizeBytes, - Cluster cluster) { - long numSplits = - getNumSplits(desiredBundleSizeBytes, estimatedSizeBytes, spec.minNumberOfSplits()); - LOG.info("Number of desired splits is {}", numSplits); - - SplitGenerator splitGenerator = new SplitGenerator(cluster.getMetadata().getPartitioner()); - List<BigInteger> tokens = - cluster.getMetadata().getTokenRanges().stream() - .map(tokenRange -> new BigInteger(tokenRange.getEnd().getValue().toString())) - .collect(Collectors.toList()); - List<List<RingRange>> splits = splitGenerator.generateSplits(numSplits, tokens); - LOG.info("{} splits were actually generated", splits.size()); - - final String partitionKey = - cluster.getMetadata().getKeyspace(spec.keyspace().get()).getTable(spec.table().get()) - .getPartitionKey().stream() - .map(ColumnMetadata::getName) - .collect(Collectors.joining(",")); - - List<TokenRange> tokenRanges = - getTokenRanges(cluster, spec.keyspace().get(), spec.table().get()); - final long estimatedSize = getEstimatedSizeBytesFromTokenRanges(tokenRanges) / splits.size(); - - List<BoundedSource<T>> sources = new ArrayList<>(); - for (List<RingRange> split : splits) { - List<String> queries = new ArrayList<>(); - for (RingRange range : split) { - if (range.isWrapping()) { Review comment: Hm, let me think about this. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 440603) Time Spent: 13h 10m (was: 13h) > Add readAll() method to CassandraIO > ----------------------------------- > > Key: BEAM-9008 > URL: https://issues.apache.org/jira/browse/BEAM-9008 > Project: Beam > Issue Type: New Feature > Components: io-java-cassandra > Affects Versions: 2.16.0 > Reporter: vincent marquez > Assignee: vincent marquez > Priority: P3 > Time Spent: 13h 10m > Remaining Estimate: 0h > > When querying a large cassandra database, it's often *much* more useful to > programatically generate the queries needed to to be run rather than reading > all partitions and attempting some filtering. > As an example: > {code:java} > public class Event { > @PartitionKey(0) public UUID accountId; > @PartitionKey(1)public String yearMonthDay; > @ClusteringKey public UUID eventId; > //other data... > }{code} > If there is ten years worth of data, you may want to only query one year's > worth. Here each token range would represent one 'token' but all events for > the day. > {code:java} > Set<UUID> accounts = getRelevantAccounts(); > Set<String> dateRange = generateDateRange("2018-01-01", "2019-01-01"); > PCollection<TokenRange> tokens = generateTokens(accounts, dateRange); > {code} > > I propose an additional _readAll()_ PTransform that can take a PCollection > of token ranges and can return a PCollection<T> of what the query would > return. > *Question: How much code should be in common between both methods?* > Currently the read connector already groups all partitions into a List of > Token Ranges, so it would be simple to refactor the current read() based > method to a 'ParDo' based one and have them both share the same function. > Reasons against sharing code between read and readAll > * Not having the read based method return a BoundedSource connector would > mean losing the ability to know the size of the data returned > * Currently the CassandraReader executes all the grouped TokenRange queries > *asynchronously* which is (maybe?) fine when all that's happening is > splitting up all the partition ranges but terrible for executing potentially > millions of queries. > Reasons _for_ sharing code would be simplified code base and that both of > the above issues would most likely have a negligable performance impact. > > > -- This message was sent by Atlassian Jira (v8.3.4#803005)