如何在Spark SQL中执行冗长的多行Hive查询?如下面的查询:
val sqlContext = new HiveContext (sc)
val result = sqlContext.sql ("
select ...
from ...
");
答案 0 :(得分:11)
使用“”代替,例如
val results = sqlContext.sql ("""
select ....
from ....
""");
或者,如果要格式化代码,请使用:
val results = sqlContext.sql ("""
|select ....
|from ....
""".stripMargin);
答案 1 :(得分:2)
您可以在SQL代码的开头/结尾使用三引号,也可以在每行末尾使用反斜杠。
val results = sqlContext.sql ("""
create table enta.scd_fullfilled_entitlement as
select *
from my_table
""");
results = sqlContext.sql (" \
create table enta.scd_fullfilled_entitlement as \
select * \
from my_table \
")
答案 2 :(得分:0)
值得注意的是长度不是问题,只是写作。为此你可以使用"""正如Gaweda建议或只是使用字符串变量,例如通过使用字符串构建器构建它。例如:
val selectElements = Seq("a","b","c")
val builder = StringBuilder.newBuilder
builder.append("select ")
builder.append(selectElements.mkString(","))
builder.append(" where d<10")
val results = sqlContext.sql(builder.toString())
答案 3 :(得分:0)
除了上述方法外,您还可以使用以下提到的方法:
public class ScheduleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Card> scheduleList;
private static final int CONTENT = 0;
private static final int AD = 1;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textDate;
public TextView textSubject;
public Button share;
public MyViewHolder(View view) {
super(view);
textDate = view.findViewById(R.id.textDate);
textSubject = view.findViewById(R.id.textSubject);
share = view.findViewById(R.id.shareBtn);
Typeface custom_font = Typeface.createFromAsset(view.getContext().getAssets(), "Exo2Light.ttf");
textDate.setTypeface(custom_font);
textSubject.setTypeface(custom_font);
}
}
public static class ViewHolderAdMob extends RecyclerView.ViewHolder {
public AdView mAdView;
public ViewHolderAdMob(View view) {
super(view);
mAdView = view.findViewById(R.id.adView);
}
}
public ScheduleAdapter(List<Card> scheduleList) {
this.scheduleList = scheduleList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == CONTENT) {
View v = inflater.inflate(R.layout.recycler_item, parent, false);
viewHolder = new MyViewHolder(v);
} else {
View v = inflater.inflate(R.layout.recycler_item_admob, parent, false);
viewHolder = new ViewHolderAdMob(v);
}
return viewHolder;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == CONTENT) {
MyViewHolder viewHolder = (MyViewHolder) holder;
Card card = scheduleList.get(position);
viewHolder.textDate.setText(card.getDateline());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
viewHolder.textSubject.setText(Html.fromHtml(card.getContentline(), Html.FROM_HTML_MODE_COMPACT));
else
viewHolder.textSubject.setText(Html.fromHtml(card.getContentline()));
((MyViewHolder) holder).share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// handle your click here.
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "\uD83D\uDCC5 " + ((MyViewHolder) holder).textDate.getText().toString()+"\r\n"+((MyViewHolder) holder).textSubject.getText().toString());
v.getContext().startActivity(Intent.createChooser(sharingIntent,"Поділитися..."));
} });
} else {
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("B86C95B89D21A5F8E2C22F0D94470A13")
.build();
((ViewHolderAdMob) holder).mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
((ViewHolderAdMob) holder).mAdView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
((ViewHolderAdMob) holder).mAdView.setVisibility(View.GONE);
}
});
((ViewHolderAdMob) holder).mAdView.loadAd(adRequest);
}
}
@Override
public int getItemViewType(int position) {
if (position == 10) {
return AD;
}
return CONTENT;
}
@Override
public int getItemCount() {
return scheduleList.size();
}
}
答案 4 :(得分:0)
val query = """(SELECT
a.AcctBranchName,
c.CustomerNum,
c.SourceCustomerId,
a.SourceAccountId,
a.AccountNum,
c.FullName,
c.LastName,
c.BirthDate,
a.Balance,
case when [RollOverStatus] = 'Y' then 'Yes' Else 'No' end as RollOverStatus
FROM
v_Account AS a left join v_Customer AS c
ON c.CustomerID = a.CustomerID AND c.Businessdate = a.Businessdate
WHERE
a.Category = 'Deposit' AND
c.Businessdate= '2018-11-28' AND
isnull(a.Classification,'N/A') IN ('Contractual Account','Non-Term Deposit','Term Deposit')
AND IsActive = 'Yes' ) tmp """