Cassandra read Column - cassandra-2.0

String cqlStatement = "SELECT * FROM local";
for (Row row : session.execute(cqlStatement)) {
System.out.println(row.toString());
}
how to get each Column value from the selected row ?

If you are using cassandra ds-driver the following should work for you.
String cqlStatement = "SELECT * FROM local";
for (Row row : session.execute(cqlStatement))
{
row.getString("columnName"); // for string data type
// row.getBool("columnName"); for boolean data type
// row.getUUID("columnName"); for UUID type
// row.getVarint("columnName"); for int type
// row.getLong("columnName"); for long type
// row.getDate("columnName"); for date type
// row.getBytes("columnName"); for bytes/anonymous type
}

Related

How to infer parquet schema by hive table schema without inserting any records?

Now given a hive table with its schema, namely:
hive> show create table nba_player;
OK
CREATE TABLE `nba_player`(
`id` bigint,
`player_id` bigint,
`player_name` string,
`admission_time` timestamp,
`nationality` string)
ROW FORMAT SERDE
'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe'
STORED AS INPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat'
LOCATION
'hdfs://endpoint:8020/user/hive/warehouse/nba_player'
TBLPROPERTIES (
'transient_lastDdlTime'='1541140811')
Time taken: 0.022 seconds, Fetched: 16 row(s)
How to infer its parquet schema without inserting any records?
The parquet schema is like:
message_meta
{optional int64 id;
optional int64 player_id;
optional binary player_name;
optional timestamp admission_time;
optional binary nationality;}
The code is shown as below
/**
* Generate MessageType by table properties using HiveSchemaConverter
*
* #param tableProperties {#link Properties}
* #return MessageType
*/
public static MessageType getMessageTypeFromTable(final Properties tableProperties) {
final String columnNameProperty = tableProperties.getProperty(IOConstants.COLUMNS);
final String columnTypeProperty = tableProperties.getProperty(IOConstants.COLUMNS_TYPES);
List<String> columnNames;
List<TypeInfo> columnTypes;
if (columnNameProperty.length() == 0) {
columnNames = new ArrayList<String>();
} else {
columnNames = Arrays.asList(columnNameProperty.split(","));
}
if (columnTypeProperty.length() == 0) {
columnTypes = new ArrayList<TypeInfo>();
} else {
columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(columnTypeProperty);
}
MessageType messageType = HiveSchemaConverter.convert(columnNames, columnTypes);
logger.info("messageType is inferred to be: {}", messageType.toString());
return messageType;
}
public class ParquetHelperTest {
#Test
public void testGenerateParquetSchemaFromTableProperties() {
Properties tableProperties = new Properties();
tableProperties.setProperty(IOConstants.COLUMNS, "id,player_id,player_name,admission_time,nationality");
tableProperties.setProperty(IOConstants.COLUMNS_TYPES, "bigint,bigint,string,timestamp,string");
MessageType messageType = ParquetHelper.getMessageTypeFromTable(tableProperties);
String expectedMessageType = "message hive_schema {\n"
+ " optional int64 id;\n"
+ " optional int64 player_id;\n"
+ " optional binary player_name (UTF8);\n"
+ " optional int96 admission_time;\n"
+ " optional binary nationality (UTF8);\n"
+ "}";
String calculatedMessageType = messageType.toString();
calculatedMessageType = calculatedMessageType.replaceAll("\\s", "");
expectedMessageType = expectedMessageType.replaceAll("\\s", "");
Assert.assertTrue(calculatedMessageType.equalsIgnoreCase(expectedMessageType));
}
}

Return a column value with the input ID

I was trying to return a column value in a table called with its ID .
public String PenTypes(int?id, Pen pen)
{
int num;
var query = from d in db.Pens
where d.ID == 1
select d.Type;
num=Convert.ToInt(query);
return num;
I have no clue as to where i'm going wrong. I do know its simple, but I'm really new to using Entity Framework. Any Help would be appreciated.
I suggest you to use DbSet<TEntity>.Find method if you want to get entity by id:
var pen = db.Pens.Find(id);
if (pen == null)
// handle case when pen is not found
return pen.Type; // if type is string, then you possibly need to parse it
Or you can use FirstOrDefault/SingleOrDefault:
var pen = db.Pens.FirstOrDefault(p => p.ID == id);
Your current code has several problems:
query will have type of IQueryable<T> (where T is type of pen.Type property). This value cannot be converted to integer
id should not be nullable if you are searching by primary key
if num is integer, then return type of method should be int instead of string

convert Enumerable row collection <short> to short

I have this error can't implicitly convert Enumerable row collection <short> to short in this line of code:
Month = (from item in query select (short)item.Month);
I want to know why and why I can't find distinct() or count method in query variable.
here is my method:
public bool IsEnableAccPosting(
string CompanyCode, DateTime FromDate, DateTime ToDate, out short Month)
{
try
{
o_dmDebitAccounts = new dmDebitAccounts(sysInfo);
bool IsEnable = false;
DataTable dt = o_dmDebitAccounts.GetDebitInterestAccPeriods(CompanyCode);
var query = from data in dt.AsEnumerable()
where data.Field<DateTime>("StartDate") == FromDate &&
data.Field<DateTime>("EndDate") == ToDate
select new
{
Month = Convert.ToInt16(data.Field<short>("Month")),
Year = Convert.ToInt16(data.Field<short>("Year"))
};
Month = (from item in query select (short)item.Month); //heres the error
The field is already typed as an Int16 in the linq query that is performing the operation. You dont need to cast it.
try the following code taken from here
if (query.Any())
{
var result = query.First();
// Console.WriteLine("Results: {0}", result.Month);
Month = result.Month;
}

How to return DateTimeOffset type with Linq

how to return DateTimeOffset with a Linq query.
I need to get a due date from a table of type DateTimeOffset
public DateTimeOffset GetLastDate(Guid Id, Guid AppId)
{
var q = from k in context.Inspections
where k.Id == Id || k.AppId == AppId
select k.Duedate;
return q;
}
cannot implicitly convert system.Linq.IQueryable to System.DateTimeOffset
The problem is that your query will return an IQueryable<T>, with the type being the type of Duedate.
If Duedate is a DateTimeOffset, you could return the first result (Where can return multiple matches) via:
var q = from k in context.Inspections
where k.Id== Id||k.AppId== AppId
select k.Duedate;
DateTimeOffset? value = q.First();
if (value.HasValue)
return value.Value;
else // found NULL in DB! Do something in this case...
throw new ApplicationException("Null offset found");
// Alternatively, you could use some default value (this uses "Now"):
// return value ?? DateTimeOffset.Now;

linq combined column search

I want to search records which two column combination equals to the parameter, why it does not work?
public RDCheck SearchByUserPlusId(string uid)
{
RDCheckDataContext dc = new RDCheckDataContext(_connString);
var query = (from r in dc.RDChecks
where (r.login + r.id).Equals(uid)
select r).FirstOrDefault();
return query;
}
for example, one record in table is
id:4/login:test
So I pass parameter uid=test4 but it returns null, why?
Use ToString() before the concatenation
public RDCheck SearchByUserPlusId(string uid)
{
RDCheckDataContext dc = new RDCheckDataContext(_connString);
var query = (from r in dc.RDChecks
where (r.login.ToString() + r.id.ToString()).Equals(uid)
select r).FirstOrDefault();
return query;
}

Resources